let textLabel = UILabel.init();
textLabel.frame = CGRect.init(x: 100, y: 100, width: 200, height: 100);
self.view.addSubview(textLabel)
textLabel.textAlignment = .center
let attributeString = NSMutableAttributedString.init(string: "你好,我是你哥")
attributeString.addAttributes([NSAttributedStringKey.paragraphStyle: NSParagraphStyle()], range: NSMakeRange(0, 2))
attributeString.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColor.red, range:NSMakeRange(0, 3))
attributeString.addAttributes([NSAttributedStringKey.font:UIFont.systemFont(ofSize: 14)], range: NSMakeRange(3, 4));
textLabel.attributedText = attributeString;
复制代码
let attach = NSTextAttachment.init()
attach.image = UIImage.init(named: "abc_btn_check_to_on_mtrl_015");
attach.bounds = CGRect.init(x: 0, y: -3, width: 20, height: 20)
let att = NSAttributedString.init(attachment: attach);
attributeString.insert(att, at:0);
textLabel.attributedText = attributeString;
复制代码
关于NSAttributedStringKey
内容如下
public static let font: NSAttributedStringKey
public static let paragraphStyle: NSAttributedStringKey // NSParagraphStyle, default defaultParagraphStyle
public static let foregroundColor: NSAttributedStringKey // UIColor, default blackColor
public static let backgroundColor: NSAttributedStringKey // UIColor, default nil: no background
public static let ligature: NSAttributedStringKey // NSNumber containing integer, default 1: default ligatures, 0: no ligatures
public static let kern: NSAttributedStringKey // NSNumber containing floating point value, in points; amount to modify default kerning. 0 means kerning is disabled.
public static let strikethroughStyle: NSAttributedStringKey // NSNumber containing integer, default 0: no strikethrough
public static let underlineStyle: NSAttributedStringKey // NSNumber containing integer, default 0: no underline
public static let strokeColor: NSAttributedStringKey // UIColor, default nil: same as foreground color
public static let strokeWidth: NSAttributedStringKey // NSNumber containing floating point value, in percent of font point size, default 0: no stroke; positive for stroke alone, negative for stroke and fill (a typical value for outlined text would be 3.0)
public static let shadow: NSAttributedStringKey // NSShadow, default nil: no shadow
public static let textEffect: NSAttributedStringKey // NSString, default nil: no text effect
public static let attachment: NSAttributedStringKey // NSTextAttachment, default nil
public static let link: NSAttributedStringKey // NSURL (preferred) or NSString
public static let baselineOffset: NSAttributedStringKey // NSNumber containing floating point value, in points; offset from baseline, default 0
public static let underlineColor: NSAttributedStringKey // UIColor, default nil: same as foreground color
public static let strikethroughColor: NSAttributedStringKey // UIColor, default nil: same as foreground color
public static let obliqueness: NSAttributedStringKey // NSNumber containing floating point value; skew to be applied to glyphs, default 0: no skew
public static let expansion: NSAttributedStringKey // NSNumber containing floating point value; log of expansion factor to be applied to glyphs, default 0: no expansion
public static let writingDirection: NSAttributedStringKey // NSArray of NSNumbers representing the nested levels of writing direction overrides as defined by Unicode LRE, RLE, LRO, and RLO characters. The control characters can be obtained by masking NSWritingDirection and NSWritingDirectionFormatType values. LRE: NSWritingDirectionLeftToRight|NSWritingDirectionEmbedding, RLE: NSWritingDirectionRightToLeft|NSWritingDirectionEmbedding, LRO: NSWritingDirectionLeftToRight|NSWritingDirectionOverride, RLO: NSWritingDirectionRightToLeft|NSWritingDirectionOverride,
public static let verticalGlyphForm: NSAttributedStringKey // An NSNumber containing an integer value. 0 means horizontal text. 1 indicates vertical text. If not specified, it could follow higher-level vertical orientation settings. Currently on iOS, it's always horizontal. The behavior for any other value is undefined.
复制代码