作者搜了一下网上的富文本,看起来都非常的凌乱,作为一个有严重强迫症的程序猿,最后还是决定自己整理一下,有不足的地方可以补充。
富文本的用法
首先,先看一个简单富文本的例子,了解一下富文本的用法。
** 下面是代码:**
NSDictionary *attributes = @{ NSForegroundColorAttributeName:[UIColor redColor], NSBackgroundColorAttributeName:[UIColor greenColor], NSFontAttributeName:[UIFont fontWithName:@"TimesNewRomanPS-BoldItalicMT" size:19.0], NSKernAttributeName:@1.0 };
NSAttributedString *attributeText = [[NSAttributedString alloc] initWithString:@"This is an attributes string" attributes:attributes];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 500, 40)]; [label setAttributedText:attributeText]; [self.view addSubview:label];
|
** 效果图:**
用法很简单,就是先将富文本属性都放在一个 NSDictionary
字典中,然后定义一个 NSAttributedString
,设置文字和富文本属性,然后设置 UILabel
的 AttributedText
属性就可以了。
富文本的属性
用法就是上面说的那样,下面主要开始介绍放在 NSDictionary
字典中的不同富文本属性的用法及效果。
** 下面是系统提供的所有富文本属性:**
NSFontAttributeName NSParagraphStyleAttributeName NSForegroundColorAttributeName NSBackgroundColorAttributeName NSLigatureAttributeName NSKernAttributeName NSStrikethroughStyleAttributeName NSUnderlineStyleAttributeName NSStrokeColorAttributeName NSStrokeWidthAttributeName NSShadowAttributeName NSTextEffectAttributeName NSAttachmentAttributeName NSLinkAttributeName NSBaselineOffsetAttributeName NSUnderlineColorAttributeName NSStrikethroughColorAttributeName NSObliquenessAttributeName NSExpansionAttributeName NSWritingDirectionAttributeName NSVerticalGlyphFormAttributeName
|
1. NSFontAttributeName 设置字体
NSFontAttributeName:[UIFont systemFontOfSize:(CGFloat)] NSFontAttributeName:[UIFont fontWithName:(nonnull NSString *) size:(CGFloat)]
|
2. NSParagraphStyleAttributeName 设置段落风格
NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init]; paragraph.alignment = NSTextAlignmentCenter; paragraph.lineSpacing = 10;
NSParagraphStyleAttributeName : paragraph
|
3. NSForegroundColorAttributeName、NSBackgroundColorAttributeName 设置字体和背景颜色
NSForegroundColorAttributeName:[UIColor redColor] NSBackgroundColorAttributeName:[UIColor greenColor]
|
4. NSKernAttributeName 设置字符间距
NSKernAttributeName:@-1.0
|
5. NSStrikethroughStyleAttributeName、NSUnderlineStyleAttributeName 添加删除线和下划线
NSStrikethroughStyleAttributeName:@(NSUnderlineStyleSingle) NSStrikethroughColorAttributeName:[UIColor redColor]
NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle) NSUnderlineColorAttributeName:[UIColor redColor]
|
6. NSStrokeColorAttributeName、NSStrokeWidthAttributeName 设置文字描边颜色和宽度
NSStrokeColorAttributeName:[UIColor redColor] NSStrokeWidthAttributeName:@3
|
7. NSShadowAttributeName 设置阴影
NSShadow * shadow = [[NSShadow alloc]init]; shadow.shadowBlurRadius = 5; shadow.shadowColor = [UIColor grayColor]; shadow.shadowOffset = CGSizeMake(1, 3);
NSShadowAttributeName:shadow
|
** 效果图:**
NSVerticalGlyphFormAttributeName:@(0) NSObliquenessAttributeName:@1 NSExpansionAttributeName:@1
|
9. NSLigatureAttributeName 设置连体属性
NSLigatureAttributeName:@0 NSLigatureAttributeName:@1
|
** 效果图:**
10. NSTextEffectAttributeName 设置文本特殊效果
NSTextEffectAttributeName: NSTextEffectLetterpressStyle
|
** 效果图:**
11. NSLinkAttributeName 设置链接属性
NSLinkAttributeName:[NSURL URLWithString:@"http://www.baidu.com"]
|
12. NSBaselineOffsetAttributeName 设置基线偏移量
NSBaselineOffsetAttributeName:@3
|
13. NSAttachmentAttributeName 设置文本附件
NSTextAttachment *textAttachment = [[NSTextAttachment alloc] initWithData:(nullable NSData *) ofType:(nullable NSString *)];
NSAttachmentAttributeName:textAttachment
|
14. NSWritingDirectionAttributeName 设置文字书写方向
@[@(NSWritingDirectionLeftToRight | NSTextWritingDirectionEmbedding)] @[@(NSWritingDirectionLeftToRight | NSTextWritingDirectionOverride)]
@[@(NSWritingDirectionRightToLeft | NSTextWritingDirectionEmbedding)] @[@(NSWritingDirectionRightToLeft | NSTextWritingDirectionOverride)]
NSWritingDirectionAttributeName:@[@(NSWritingDirectionRightToLeft|NSWritingDirectionOverride)]
|
** 效果图:**
好了,大概就是那么多,有需要补充的后续还会再补充,最后附上本文参考的文章地址。