iOS-富文本应用 attributedText
2017-12-08 本文已影响207人
iOS_Gato_老猫
首先 讲一下什么是富文本
富文本在开发过程中 主要用于精简代码 主要作用还是很大的 当然主要是样式方面
比如一个label中 包含不同的颜色 不同的字号 不用的背景颜色 等等等等
下面开始简单讲解讲解一下富文本的应用
/*
label 需要改变的Label
string label中包含的全部文字
colorstring 需要改变颜色或状态的文字【必须是包含在 全部文字中】
color 需要改变的颜色
font 需要改变的字号
*/
+(void)NSMutableAttributedStringWithLabel:(UILabel * )label WithAllString:(NSString *)string WithColorString:(NSString *)colorstring WithColor:(UIColor *)color WithFont:(UIFont *)font
{
// 创建对象.
NSMutableAttributedString *mAttStri = [[NSMutableAttributedString alloc] initWithString:string];
//
NSRange range = [string rangeOfString:colorstring];
[mAttStri addAttribute:NSForegroundColorAttributeName value:color range:range];
[mAttStri addAttribute:NSFontAttributeName value:font range:range];
label.attributedText = mAttStri;
}
上面写了一个简单的富文本+方法
其中最主要的语句就是
[mAttStri addAttribute: NSFontAttributeName value:font range:range];
下面我们来看看 都可以更改哪些内容
// Predefined character attributes for text. If the key is not present in the dictionary, it indicates the default value described below.
UIKIT_EXTERN NSAttributedStringKey const NSFontAttributeName NS_AVAILABLE(10_0, 6_0); // UIFont, default Helvetica(Neue) 12
UIKIT_EXTERN NSAttributedStringKey const NSParagraphStyleAttributeName NS_AVAILABLE(10_0, 6_0); // NSParagraphStyle, default defaultParagraphStyle
UIKIT_EXTERN NSAttributedStringKey const NSForegroundColorAttributeName NS_AVAILABLE(10_0, 6_0); // UIColor, default blackColor
UIKIT_EXTERN NSAttributedStringKey const NSBackgroundColorAttributeName NS_AVAILABLE(10_0, 6_0); // UIColor, default nil: no background
UIKIT_EXTERN NSAttributedStringKey const NSLigatureAttributeName NS_AVAILABLE(10_0, 6_0); // NSNumber containing integer, default 1: default ligatures, 0: no ligatures
UIKIT_EXTERN NSAttributedStringKey const NSKernAttributeName NS_AVAILABLE(10_0, 6_0); // NSNumber containing floating point value, in points; amount to modify default kerning. 0 means kerning is disabled.
UIKIT_EXTERN NSAttributedStringKey const NSStrikethroughStyleAttributeName NS_AVAILABLE(10_0, 6_0); // NSNumber containing integer, default 0: no strikethrough
UIKIT_EXTERN NSAttributedStringKey const NSUnderlineStyleAttributeName NS_AVAILABLE(10_0, 6_0); // NSNumber containing integer, default 0: no underline
UIKIT_EXTERN NSAttributedStringKey const NSStrokeColorAttributeName NS_AVAILABLE(10_0, 6_0); // UIColor, default nil: same as foreground color
UIKIT_EXTERN NSAttributedStringKey const NSStrokeWidthAttributeName NS_AVAILABLE(10_0, 6_0); // 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)
UIKIT_EXTERN NSAttributedStringKey const NSShadowAttributeName NS_AVAILABLE(10_0, 6_0); // NSShadow, default nil: no shadow
UIKIT_EXTERN NSAttributedStringKey const NSTextEffectAttributeName NS_AVAILABLE(10_10, 7_0); // NSString, default nil: no text effect
UIKIT_EXTERN NSAttributedStringKey const NSAttachmentAttributeName NS_AVAILABLE(10_0, 7_0); // NSTextAttachment, default nil
UIKIT_EXTERN NSAttributedStringKey const NSLinkAttributeName NS_AVAILABLE(10_0, 7_0); // NSURL (preferred) or NSString
UIKIT_EXTERN NSAttributedStringKey const NSBaselineOffsetAttributeName NS_AVAILABLE(10_0, 7_0); // NSNumber containing floating point value, in points; offset from baseline, default 0
UIKIT_EXTERN NSAttributedStringKey const NSUnderlineColorAttributeName NS_AVAILABLE(10_0, 7_0); // UIColor, default nil: same as foreground color
UIKIT_EXTERN NSAttributedStringKey const NSStrikethroughColorAttributeName NS_AVAILABLE(10_0, 7_0); // UIColor, default nil: same as foreground color
UIKIT_EXTERN NSAttributedStringKey const NSObliquenessAttributeName NS_AVAILABLE(10_0, 7_0); // NSNumber containing floating point value; skew to be applied to glyphs, default 0: no skew
UIKIT_EXTERN NSAttributedStringKey const NSExpansionAttributeName NS_AVAILABLE(10_0, 7_0); // NSNumber containing floating point value; log of expansion factor to be applied to glyphs, default 0: no expansion
UIKIT_EXTERN NSAttributedStringKey const NSWritingDirectionAttributeName NS_AVAILABLE(10_6, 7_0); // 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,
UIKIT_EXTERN NSAttributedStringKey const NSVerticalGlyphFormAttributeName NS_AVAILABLE(10_7, 6_0); // 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.
下面我们开始逐一讲解 可以改变的内容 和 改变内容以后的样式
//改变字体大小
NSFontAttributeName
对应 value:UIFont
默认 Helvetica(Neue) 12
[mAttStri addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Helvetica" size:30] range:range];
//改变段落样式
NSParagraphStyleAttributeName
对应 value:NSParagraphStyle
默认 defaultParagraphStyle
//改变字体颜色
NSForegroundColorAttributeName
对应 value:UIColor
默认 blackColor
[mAttStri addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range];
//改变背景颜色
NSBackgroundColorAttributeName
对应 value:UIColor
默认 nil
[mAttStri addAttribute:NSBackgroundColorAttributeName value:[UIColor blueColor] range:range];
//改变字间距
NSKernAttributeName
对应 value:NSNumber
默认 0
[mAttStri addAttribute:NSKernAttributeName value:@10 range:range];
//删除线
NSStrikethroughStyleAttributeName
对应 value:NSNumber
默认 NSUnderlineStyleNone
NSUnderlineStyleNone 不设置删除线
NSUnderlineStyleSingle 设置删除线为细单实线
NSUnderlineStyleThick 设置删除线为粗单实线
NSUnderlineStyleDouble 设置删除线为细双实线
[mAttStri addAttribute:NSStrikethroughStyleAttributeName value:@1 range:range];
//下划线
NSUnderlineStyleAttributeName
对应 value:NSNumber
默认 NSUnderlineStyleNone
NSUnderlineStyleNone 不设置
NSUnderlineStyleSingle 设置下划线为细单实线
NSUnderlineStyleThick 设置下划线为粗单实线
NSUnderlineStyleDouble 设置下划线为细双实线
[mAttStri addAttribute:NSUnderlineStyleAttributeName value:@1 range:range];
//设置下划线颜色
NSUnderlineColorAttributeName
对应 value:UIColor
默认 nil
[mAttStri addAttribute:NSUnderlineStyleAttributeName value:@1 range:range];
[mAttStri addAttribute:NSUnderlineColorAttributeName value:[UIColor greenColor] range:range];
//设置删除线颜色
NSStrikethroughColorAttributeName
对应 value:UIColor
默认 nil
[mAttStri addAttribute:NSStrikethroughStyleAttributeName value:@1 range:range];
[mAttStri addAttribute:NSStrikethroughColorAttributeName value:[UIColor orangeColor] range:range];
//边缘线颜色 【一般对应边缘线宽度一起使用】
NSStrokeColorAttributeName
对应 value:UIColor
默认 nil
//边缘线宽度
NSStrokeWidthAttributeName
对应 value:NSNumber
默认 0
[mAttStri addAttribute:NSStrokeColorAttributeName value:[UIColor yellowColor] range:range];
[mAttStri addAttribute:NSStrokeWidthAttributeName value:@0.2 range:range];
//label阴影 【单独设置不好使,和这三个任一个都好使,NSVerticalGlyphFormAttributeName,NSObliquenessAttributeName,NSExpansionAttributeName】
NSShadowAttributeName
对应 value:NSShadow
默认 nil
NSShadow * shadow = [[NSShadow alloc]init];
shadow.shadowBlurRadius = 5;//模糊度
shadow.shadowColor = [UIColor orangeColor];//阴影颜色
shadow.shadowOffset = CGSizeMake(1, 3);//偏移量
[mAttStri addAttribute:NSShadowAttributeName value:shadow range:range];
[mAttStri addAttribute:NSVerticalGlyphFormAttributeName value:@0 range:range];
//横竖排版
NSVerticalGlyphFormAttributeName
对应 value:NSNumber
默认 0
0 表示横排文本。1 表示竖排文本。在 iOS 中,总是使用横排文本,0 以外的值都未定义。
//字体倾斜
NSObliquenessAttributeName
对应 value:NSNumber
默认 0
0 不倾斜 1倾斜
//文本扁平化
NSExpansionAttributeName
对应 value:NSNumber
默认 0
0 不扩展 1扩张
//设置文本特殊效果,取值为 NSString 对象,目前只有图版印刷效果可用
NSTextEffectAttributeName
对应 value:NSString
默认 nil
NSTextEffectLetterpressStyle 只有一种
//设置文本附件,取值为NSTextAttachment对象,常用于文字图片混排
NSAttachmentAttributeName
对应 value:NSTextAttachment
默认 nil
//URL链接 很想HTML中的超链接
NSLinkAttributeName
对应 value:URL/string
//设置基线偏移值
NSBaselineOffsetAttributeName
对应 value:NSNumber
默认 0
正值上偏,负值下偏
[mAttStri addAttribute:NSBaselineOffsetAttributeName value:@10 range:range];
好了 基本上 富文本的应用就这些 但是已经很强大了 应该能满足很多要求
因为比较懒 多有没有做配图 改天有时间把配图加上
针对每种状态 定义上适当的配图 并且加上源代码 在做一些详细的说明
OK 结束!
2017-12-15 附上Demo地址 自己写的 有问题可以留言