iOS-富文本应用 attributedText

首先 讲一下什么是富文本

富文本在开发过程中 主要用于精简代码 主要作用还是很大的 当然主要是样式方面
比如一个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地址 自己写的 有问题可以留言

Demo下载地址传送门-来吧 尽情的点我

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,524评论 5 460
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,869评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,813评论 0 320
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,210评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,085评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,117评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,533评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,219评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,487评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,582评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,362评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,218评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,589评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,899评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,176评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,503评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,707评论 2 335

推荐阅读更多精彩内容