iOS富文本属性

926 阅读2分钟

iOS富文本属性使用方法

个人开发经常用到的一些富文本属性

  • 字体属性

    NSFontAttributeName

[attStr addAttribute:NSFontAttributeName value:CLFont(14) range:NSMakeRange(0, text.length)];

  • 段落属性

    NSParagraphStyleAttributeName

NSMutableParagraphStyle *style = [NSMutableParagraphStyle new];
//字体间距
style.lineSpacing = 10;
//首行缩进
style.firstLineHeadIndent = 20.0f;
//文本对齐方式
style.alignment = NSTextAlignmentJustified;
//整体缩进(首行除外)
style.headIndent = 20;
//尾部缩进
style.tailIndent = 20;
//最低行高
style.minimumLineHeight = 20;
style.maximumLineHeight = 20;
//段落间距
style.paragraphSpacing = 20;
//段首间距
style.paragraphSpacingBefore = 20;
//书写方式
style.baseWritingDirection = NSWritingDirectionLeftToRight;

[attStr addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, text.length)];
  • 文本颜色

    NSForegroundColorAttributeName

[attStr addAttribute:NSForegroundColorAttributeName value:CLRGBAColor(0, 0, 0, 1) range:NSMakeRange(0, text.length)];
  • 背景颜色

    NSBackgroundColorAttributeName

[attStr addAttribute:NSBackgroundColorAttributeName value:CLRGBAColor(0, 0, 0, 1) range:NSMakeRange(0, text.length)];

  • 字体间隔

    NSKernAttributeName

[attStr addAttribute:NSKernAttributeName value:@(10) range:NSMakeRange(0, text.length)];
  • 文本添加删除线(单删除线、双删除线)

    NSStrikethroughStyleAttributeName

[attStr addAttribute:NSStrikethroughStyleAttributeName value:@(10) range:NSMakeRange(0, text.length)];

  • 文本设置下划线

    NSUnderlineStyleAttributeName

[attStr addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:NSMakeRange(0, text.length)];

  • 设置文本描边颜色

    NSStrokeColorAttributeName(value是UIColor对象)

[attStr addAttribute:NSUnderlineStyleAttributeName value:CLRGBAColor(0, 0, 0, 1) range:NSMakeRange(0, text.length)];
  • 设置描边宽度

    NSStrokeColorAttributeName同时使用能使文字空心 NSStrokeWidthAttributeName (value是NSNumber对象):

[attStr addAttribute:NSUnderlineStyleAttributeName value:@(-2) range:NSMakeRange(0, text.length)];

  • 设置文本阴影,单独设置好用的(11.4系统),下面的观点是查资料的,可能和系统有关吧 NSShadowAttributeName(value是NSShadow对象) :
/*
(单独设置不好使,必须和其他属性搭配才好使)
和这三个任一个都好使,NSVerticalGlyphFormAttributeName,NSObliquenessAttributeName,NSExpansionAttributeName)

*/
NSShadow *shadow = [NSShadow new];
//偏移量
shadow.shadowOffset = CGSizeMake(0, 5);
//阴影颜色
shadow.shadowColor = UIColor.redColor;
//模糊度
shadow.shadowBlurRadius = 3;
[attStr addAttribute:NSShadowAttributeName value:shadow range:NSMakeRange(0, text.length)];
  • 设置文本特殊效果

    NSTextEffectAttributeName(value是NSString)目前只有一个可用效果NSTextEffectLetterpressStyle(凸版印刷效果)

NSShadow *shadow = [NSShadow new];
//偏移量
shadow.shadowOffset = CGSizeMake(0, 5);
//阴影颜色
shadow.shadowColor = UIColor.redColor;
//模糊度
shadow.shadowBlurRadius = 3;
//凸版印刷效果
[attStr addAttribute:NSTextEffectAttributeName value:NSTextEffectLetterpressStyle range:NSMakeRange(0, text.length)];
//阴影
[attStr addAttribute:NSShadowAttributeName value:shadow range:NSMakeRange(0, text.length)];
//背景颜色
[attStr addAttribute:NSBackgroundColorAttributeName value:UIColor.redColor range:NSMakeRange(0, text.length)];
//字体大小
[attStr addAttribute:NSFontAttributeName value:CLFont(14) range:NSMakeRange(0, text.length)];

  • 设置文本附件,常用于图文混排

    NSAttachmentAttributeName (value是NSTextAttachment对象)

NSTextAttachment *achment = [NSTextAttachment new];
achment.image = [UIImage imageNamed:@""];
achment.bounds = CGRectMake(0, 0, 20, 20);
NSAttributedString *imageStr = [NSAttributedString attributedStringWithAttachment:achment];
[attStr appendAttributedString:imageStr];

  • 链接

    NSLinkAttributeName (value是NSURL or NSString)

    不能在UILabel和UITextField使用,只能用UITextView来进行,实现他的代理,在代理方法里面进行URL跳转 实现代理

    注释: -(BOOL)textView:(UITextView*)textView shouldInteractWithURL:(NSURL*)URL inRange:(NSRange)characterRange interaction:(UITextItemInteraction)interaction

    1.一定要实现UITextView的代理才能进行URL跳转

    2.textView的editable属性修改为NO,在编辑时不可点击

[attStr addAttribute:NSLinkAttributeName value:[NSURL URLWithString:@""] range:NSMakeRange(0, text.length)];

  • 文字基线偏移

    NSBaselineOffsetAttributeName (value是NSNumber对象):

[attStr addAttribute:NSBaselineOffsetAttributeName value:@(2) range:NSMakeRange(0, text.length)];

  • 下划线颜色

    NSUnderlineColorAttributeName (value是UIColor对象)

[attStr addAttribute:NSUnderlineColorAttributeName value:UIColor.redColor range:NSMakeRange(0, text.length)];

  • 删除线颜色

    NSStrikethroughColorAttributeName (value是UIColor对象)

[attStr addAttribute:NSStrikethroughStyleAttributeName value:@(10) range:NSMakeRange(0, text.length)];
[attStr addAttribute:NSStrikethroughColorAttributeName value:UIColor.redColor range:NSMakeRange(0, text.length)];
  • 设置字体倾斜度

    NSObliquenessAttributeName (value是NSNumber对象)

[attStr addAttribute:NSObliquenessAttributeName value:@(10) range:NSMakeRange(0, text.length)];

  • 设置字体的横向拉伸

    NSExpansionAttributeName(value是NSNumber对象)

[attStr addAttribute:NSExpansionAttributeName value:@(1) range:NSMakeRange(0, text.length)];