NSMutableAttributedString/NSAttributedString
相當(dāng)于帶屬性的字符串,實(shí)現(xiàn)圖文混排
使用步驟:
1.初始化字符串
2.初始化字符串的屬性
3.將屬性賦值給字符串
1.初始化字符串的常用方法:
- (instancetype)initWithString:(NSString *)str
- (instancetype)initWithString:(NSString *)str attributes:(nullable NSDictionary *)attrs;
- (instancetype)initWithAttributedString:(NSAttributedString *)attrStr;
+ (NSAttributedString *)attributedStringWithAttachment:(NSTextAttachment *)attachment;
2.常見(jiàn)的初始化屬性:
NSString * const NSFontAttributeName NS_AVAILABLE(10_0, 6_0); // UIFont,設(shè)置字體
NSString * const NSParagraphStyleAttributeName NS_AVAILABLE(10_0, 6_0); // NSParagraphStyle, 設(shè)置段落
NSString * const NSForegroundColorAttributeName NS_AVAILABLE(10_0, 6_0); // UIColor, 設(shè)置字體顏色
NSString * const NSBackgroundColorAttributeName NS_AVAILABLE(10_0, 6_0); // UIColor, 設(shè)置背景顏色
NSString * const NSLigatureAttributeName NS_AVAILABLE(10_0, 6_0); // NSNumber(整型)霞溪,連字屬性妇斤,一般中文用不到惹资,在英文中可能出現(xiàn)相鄰字母連筆的情況扼脐。0為不連筆纹腌;1為默認(rèn)連筆幌缝,也是默認(rèn)值灸促;2在ios 上不支持。
NSString * const NSKernAttributeName NS_AVAILABLE(10_0, 6_0); // NSNumber (浮點(diǎn)型)涵卵,設(shè)置字間距浴栽,字母緊排指定了用于調(diào)整字距的像素點(diǎn)數(shù)。字母緊排的效果依賴于字體轿偎。值為 0 表示不使用字母緊排典鸡。默認(rèn)值為0
NSString * const NSStrikethroughStyleAttributeName NS_AVAILABLE(10_0, 6_0); // NSNumber(整型),設(shè)置刪除線
NSString * const NSUnderlineStyleAttributeName NS_AVAILABLE(10_0, 6_0); // NSNumber (整型)贴硫,設(shè)置下劃線
NSString * const NSStrokeColorAttributeName NS_AVAILABLE(10_0, 6_0); // UIColor,設(shè)置邊線顏色椿每,如果該屬性不指定(默認(rèn)),則等同于NSForegroundColorAttributeName英遭。否則间护,指定為刪除線或下劃線顏色。
NSString * const NSStrokeWidthAttributeName NS_AVAILABLE(10_0, 6_0); // NSNumber(浮點(diǎn)型)挖诸,設(shè)置邊線寬度
NSString * const NSShadowAttributeName NS_AVAILABLE(10_0, 6_0); // NSShadow, 設(shè)置陰影汁尺,默認(rèn)為nil
NSString * const NSAttachmentAttributeName NS_AVAILABLE(10_0, 7_0); // NSTextAttachment, 默認(rèn)為nil
NSString * const NSLinkAttributeName NS_AVAILABLE(10_0, 7_0); // NSURL (preferred) or NSString
NSString * const NSBaselineOffsetAttributeName NS_AVAILABLE(10_0, 7_0); // NSNumber (浮點(diǎn)型),設(shè)置行距
NSString * const NSUnderlineColorAttributeName NS_AVAILABLE(10_0, 7_0); // UIColor
NSString * const NSStrikethroughColorAttributeName NS_AVAILABLE(10_0, 7_0); // UIColor
UIKIT_EXTERN NSString * const NSVerticalGlyphFormAttributeName NS_AVAILABLE(10_7, 6_0); //NSNumber(整型)多律,0為水平排版的字痴突,1為垂直排版的字搂蜓。
3.將屬性賦值給字符串
簡(jiǎn)單實(shí)例:
NSMutableDictionary* arr = [NSMutableDictionary dictionary];
arr[NSForegroundColorAttributeName] = [UIColor redColor];
arr[NSBackgroundColorAttributeName] = [UIColor greenColor];
arr[NSKernAttributeName] = @10;
arr[NSUnderlineStyleAttributeName] = @1;
NSMutableAttributedString* str = [[NSMutableAttributedString alloc]initWithString:@"我的大刀早已饑渴難耐了" attributes:arr];
self.oneLabel.attributedText = str;
效果:
效果.png
實(shí)現(xiàn)簡(jiǎn)單的圖文混排:
NSMutableAttributedString *str = [[NSMutableAttributedString alloc]init];
NSMutableAttributedString *childStr = [[NSMutableAttributedString alloc]initWithString:@"菜鳥(niǎo)真多,"];
[str appendAttributedString:childStr];
NSTextAttachment *attach = [[NSTextAttachment alloc]init];
attach.image = [UIImage imageNamed:@"2"];
attach.bounds = CGRectMake(0, -5, 20, 20);
NSAttributedString *picStr = [NSAttributedString attributedStringWithAttachment:attach];
[str appendAttributedString:picStr];
NSAttributedString *childStr2 = [[NSAttributedString alloc]initWithString:@"匹配系統(tǒng)能找到真正的平衡么"];
[str appendAttributedString:childStr2];
self.twoLabel.attributedText = str;
效果:
圖文混排.png
自己總結(jié)的一些辽装,有不對(duì)的地方請(qǐng)批評(píng)指正帮碰!