iOS_NSAttributedString 的21种属性详细介绍(图文混排)

iOS_NSAttributedString 的21种属性详细介绍(图文混排)

* API: Character Attributes ,NSAttributedString共有21个属性*

*1.NSFontAttributeName->设置字体属性,默认值:字体:Helvetica(Neue) 字号:12

*2.NSParagraphStyleAttributeName->设置文本段落排版格式,取值为NSParagraphStyle对象(详情见下面的API说明) 

*3.NSForegroundColorAttributeName->设置字体颜色,取值为UIColor对象,默认值为黑色 

*4.NSBackgroundColorAttributeName->设置字体所在区域背景颜色,取值为UIColor对象,默认值为nil, 透明色 

*5.NSLigatureAttributeName->设置连体属性,取值为NSNumber对象(整数),0表示没有连体字符,1表示使用默认的连体字符

*6.NSKernAttributeName->设置字符间距,取值为NSNumber对象(整数),正值间距加宽,负值间距变窄

 *7.NSStrikethroughStyleAttributeName->设置删除线,取值为NSNumber对象(整数) 

*8.NSStrikethroughColorAttributeName->设置删除线颜色,取值为UIColor对象,默认值为黑色

 *9.NSUnderlineStyleAttributeName->设置下划线,取值为NSNumber对象(整数),枚举常量NSUnderlineStyle中的值,与删除线类似 

*10.NSUnderlineColorAttributeName->设置下划线颜色,取值为UIColor对象,默认值为黑色 

*11.NSStrokeWidthAttributeName->设置笔画宽度(粗细),取值为NSNumber对象(整数),负值填充效果,正值中空效果 

*12.NSStrokeColorAttributeName->填充部分颜色,不是字体颜色,取值为UIColor对象 

*13.NSShadowAttributeName->设置阴影属性,取值为NSShadow对象 

*14.NSTextEffectAttributeName->设置文本特殊效果,取值为NSString对象,目前只有图版印刷效果可用 

*15.NSBaselineOffsetAttributeName->设置基线偏移值,取值为NSNumber(float),正值上偏,负值下偏 

*16.NSObliquenessAttributeName->设置字形倾斜度,取值为NSNumber(float),正值右倾,负值左倾 

*17.NSExpansionAttributeName->设置文本横向拉伸属性,取值为NSNumber(float),正值横向拉伸文本,负值横向压缩文本 

*18.NSWritingDirectionAttributeName->设置文字书写方向,从左向右书写或者从右向左书写 

*19.NSVerticalGlyphFormAttributeName->设置文字排版方向,取值为NSNumber对象(整数),0表示横排文本,1表示竖排文本

*20.NSLinkAttributeName->设置链接属性,点击后调用浏览器打开指定URL地址 

*21.NSAttachmentAttributeName->设置文本附件,取值为NSTextAttachment对象,常用于文字图片混排

- (void)creatTitleLabel {self.titleLabel = [[UILabelalloc] initWithFrame:CGRectMake(20,50,320,400)];self.titleLabel.numberOfLines =0;self.titleLabel.layer.borderColor = [UIColorgrayColor].CGColor;self.titleLabel.layer.borderWidth =0.5;self.titleLabel.textAlignment =NSTextAlignmentLeft;    [self.view addSubview:self.titleLabel];NSString*string =@"An NSAttributedString object manages character strings and associated sets of attributes (for example, font and kerning) that apply to individual characters or ranges of characters in the string. An association of characters and their attributes is called an attributed string. ";/* 这句话就是对这个类的一个最简明扼要的概括。NSAttributedString管理一个字符串,以及与该字符串中的单个字符或某些范围的字符串相关的属性。它有一个子类NSMutableAttributedString

    * 具体实现时,NSAttributedString维护了一个NSString,用来保存最原始的字符串,另有一个NSDictionary用来保存各个子串/字符的属性。

    */

titleLabel.png

#pragma mark - NSMutableAttributedString 创建/* 三种初始化方法,NSMutableAttributedString没有初始化方法,使用父类初始化方法, 使用initWithString:, initWithString:attributes:, 或者 initWithAttributedString: */NSAttributedString*attStri = [[NSAttributedStringalloc] initWithString:string attributes:@{NSFontAttributeName:[UIFontsystemFontOfSize:30]}];NSMutableAttributedString*mAttStri = [[NSMutableAttributedStringalloc] initWithString:string];#pragma mark ** 1. NSFontAttributeName 设置字体属性/* 字体大小 及 字体类型 */NSRangefont_range = [string rangeOfString:@"An"];    [mAttStri addAttribute:NSFontAttributeNamevalue:[UIFontsystemFontOfSize:30] range:font_range];    [mAttStri addAttribute:NSFontAttributeNamevalue:[UIFontfontWithName:@"Courier-BoldOblique"size:17.0] range:NSMakeRange(10,10)];

字体大小 及 字体类型 .png

#pragma mark ** 2. NSParagraphStyleAttributeName 设置文本段落排版格式NSMutableParagraphStyle*style = [[NSMutableParagraphStylealloc] init];    style.firstLineHeadIndent =20;    style.lineSpacing =10;        [mAttStri addAttribute:NSParagraphStyleAttributeNamevalue:style range:NSMakeRange(0, mAttStri.length /2)];

设置文本段落排版格式.png

#pragma mark ** 3. NSForegroundColorAttributeName 设置字体颜色/* 值为UIColor,字体颜色,默认为黑色. */[mAttStri addAttribute:NSForegroundColorAttributeNamevalue:[UIColorredColor] range:NSMakeRange(0, mAttStri.length)];

设置字体颜色.png

#pragma mark ** 4. NSBackgroundColorAttributeName 设置字体所在区域背景颜色/* 值为UIColor,字体背景色,默认透明. */[mAttStri addAttribute:NSBackgroundColorAttributeNamevalue:[UIColorgrayColor] range:NSMakeRange(0,20)];

设置字体所在区域背景颜色.png

#pragma mark ** 5. NSLigatureAttributeName 设置连体属性/* 取值为NSNumber 对象(整数). 0 表示没有连体字符, 1 表示使用默认的连体字符. 一般中文用不到,在英文中可能出现相邻字母连笔的情况 */[mAttStri addAttribute:NSLigatureAttributeNamevalue:@0range:NSMakeRange(0, mAttStri.length)];

#pragma mark ** 6. NSKernAttributeName 设置字符间距/* 值为浮点数NSNumber,字距属性,默认值为0。*/[mAttStri addAttribute:NSKernAttributeNamevalue:@3range:NSMakeRange(0, mAttStri.length)];

设置字符间距.png

#pragma mark ** 7. NSStrikethroughStyleAttributeName 设置删除线/* 值为整型NSNumber,可取值为(取值大小为删除线的宽度)

        enum {


        NSUnderlineStyleNone = 0×00,


        NSUnderlineStyleSingle = 0×01,


        }; 设置删除线。

    */[mAttStri addAttribute:NSStrikethroughStyleAttributeNamevalue:@3range:NSMakeRange(3,7)];

设置删除线.png

#pragma mark ** 8. NSStrikethroughColorAttributeName 设置删除线颜色/* 这个属性的值是一个UIColor对象. */[mAttStri addAttribute:NSStrikethroughColorAttributeNamevalue:[UIColorblueColor] range:NSMakeRange(3,3)];

设置删除线颜色.png

#pragma mark ** 9. NSUnderlineStyleAttributeName 设置下划线/* 取值为 NSNumber 对象(整数),枚举常量 NSUnderlineStyle中的值,与删除线类似 */[mAttStri addAttribute:NSUnderlineStyleAttributeNamevalue:@2range:NSMakeRange(6,5)];

设置下划线.png

#pragma mark ** 10. NSUnderlineColorAttributeName 设置下划线颜色/* 这个属性的值是一个UIColor对象.默认值为nil. */[mAttStri addAttribute:NSUnderlineColorAttributeNamevalue:[UIColorblackColor] range:NSMakeRange(6,5)];

设置下划线颜色.png

#pragma mark ** 11. NSStrokeWidthAttributeName 设置笔画宽度(粗细)/* 值为浮点数NSNumber。设置笔画的粗细。负值填充效果,正值中空效果. */[mAttStri addAttribute:NSStrokeWidthAttributeNamevalue:@10range:NSMakeRange(50,30)];

设置笔画宽度(粗细).png

#pragma mark ** 12. NSStrokeColorAttributeName 填充部分颜色,/* 不是字体颜色,取值为 UIColor 对象 默认值为nil,设置的属性同ForegroundColor。*/[mAttStri addAttribute:NSStrokeColorAttributeNamevalue:[UIColororangeColor] range:NSMakeRange(50,20)];

填充部分颜色.png

#pragma mark ** 13. NSShadowAttributeName 设置阴影属性/* 值为NSShadow,设置笔画的阴影,默认值为nil。*/NSShadow*shadow = [[NSShadowalloc]init];    shadow.shadowOffset =CGSizeMake(10,10);    shadow.shadowColor = [UIColorgreenColor];    [mAttStri addAttribute:NSShadowAttributeNamevalue:shadow range:NSMakeRange(20,10)];

设置阴影属性.png

#pragma mark ** 14. NSTextEffectAttributeName 设置文本特殊效果/* 这个属性的值是一个NSString对象。使用此属性指定的文字效果,如NSTextEffectLetterpressStyle。此属性的默认值为nil,表示没有文本效应。*/[mAttStri addAttribute:NSTextEffectAttributeNamevalue:NSTextEffectLetterpressStylerange:NSMakeRange(80,10)];

设置文本特殊效果.png

#pragma mark ** 15. NSBaselineOffsetAttributeName 设置基线偏移值/* 此属性的值是包含一个浮点值的NSNumber对象,表示的字符从基线偏移的NSNumber对象,默认值是0。正值上偏,负值下偏 */[mAttStri addAttribute:NSBaselineOffsetAttributeNamevalue:@5range:NSMakeRange(112,10)];

设置基线偏移值.png

#pragma mark ** 16. NSObliquenessAttributeName 设置字形倾斜度取值为 NSNumber (float),正值右倾,负值左倾/* 此属性的值是包含一个浮点值的NSNumber对象。默认值为0,表示没有倾斜, 正值右倾,负值左倾。 */[mAttStri addAttribute:NSObliquenessAttributeNamevalue:@0.8range:NSMakeRange(135,15)];

设置字形倾斜度取值.png

#pragmamark ** 17. NSExpansionAttributeName 设置文本横向拉伸属性/* 取值为 NSNumber(float), 正值横向拉伸文本, 负值横向压缩文本 */NSRange range =  [stringrangeOfString:@"An association of"];    [mAttStri addAttribute:NSExpansionAttributeName value:@1.0range:range];

设置文本横向拉伸属性png

#pragma mark ** 18. NSWritingDirectionAttributeName 设置文字书写方向/**      * 取值为包含NSNumber对象的数组. 从左向右书写或者从右向左书写.    *    * The values of the NSNumber objects should be0,1,2,or3,forLRE, RLE, LRO,orRLO respectively,andcombinations of NSWritingDirectionLeftToRightandNSWritingDirectionRightToLeftwithNSTextWritingDirectionEmbeddingorNSTextWritingDirectionOverride,asshowninValues of NSWritingDirectionAttributeNameandequivalent markup.          */    NSRange rang2 = [string rangeOfString:@"characters and their"];    [mAttStri addAttribute:NSWritingDirectionAttributeName value:@[@3] range:rang2];

设置文字书写方向.png

#pragma mark ** 19. NSVerticalGlyphFormAttributeName 设置文字排版方向/**

    * 值为整型NSNumber,0为水平排版的字,1为垂直排版的字。注意,在iOS中, 总是以横向排版

    *

    * In iOS, horizontal text is always used and specifying a different value is undefined.

    */[mAttStri addAttribute:NSVerticalGlyphFormAttributeNamevalue:@1range:NSMakeRange(1,10)];

设置文字排版方向.png

#pragma mark ** 20. NSLinkAttributeName 设置链接属性/**

    * 此属性的值是NSURL对象(首选)或一个NSString对象。此属性的默认值为nil,表示没有链接。

    * UILabel无法使用该属性, 可以使用UITextView 控件.

    */UITextView*textView = [[UITextViewalloc] initWithFrame:CGRectMake(20,450,320,60)];    [self.view addSubview:textView];    textView.backgroundColor  = [UIColorlightGrayColor];NSString*strLink =@"百度链接";NSAttributedString*attStr  = [[NSAttributedStringalloc] initWithString:strLink attributes:@{NSLinkAttributeName: [NSURLURLWithString:@"http://www.baidu.com"]}];        textView.editable =NO;/* 签订协议, 指定代理人之后. 但点击链接时, 会回调协议方法 (- textView:shouldInteractWithURL:inRange:) */textView.delegate =self;        textView.attributedText = attStr;

设置链接属性.png

#pragma mark ** 21. NSAttachmentAttributeName 设置文本附件/* 这个属性的值是一个NSTextAttachment对象。此属性的默认值为nil,表示无附件。*//**

    * 关于NSTextAttachment类的简单说明

    *

    * NSTextAttachment 类有一个指定的初始化方法(- initWithData:ofType:), 需要指定附件文档的数据和附件文件的类型. 如果附件文档数据指定nil, 那么系统将会默认指定为image对象作为值. 因此, 也可以通过这个特性实现图文混排.

    * 下面就以附件为image对象来说明NSAttachmentAttributeName的使用.

    *

    */UILabel*label = [[UILabelalloc] initWithFrame:CGRectMake(20,550,320,60)];    label.backgroundColor = [UIColoryellowColor];    [self.view addSubview:label];/* 下面实现在百度两个汉字之间插入一个照片 */NSString*stiAtt =@"百度";NSTextAttachment*attach = [[NSTextAttachmentalloc] initWithData:nilofType:nil];    attach.bounds =CGRectMake(0,0,50,50);    attach.image = [UIImageimageNamed:@"baidu.jpg"];NSAttributedString*strAtt = [NSAttributedStringattributedStringWithAttachment:attach];NSMutableAttributedString*strMatt = [[NSMutableAttributedStringalloc] initWithString:stiAtt];        [strMatt insertAttributedString:strAtt atIndex:1];        label.attributedText = strMatt;self.titleLabel.attributedText = mAttStri;    [self.titleLabel sizeToFit];    }#pragma mark - textView delegate - (BOOL)textView:(UITextView*)textView shouldInteractWithURL:(NSURL*)URL inRange:(NSRange)characterRange {NSLog(@"%s", __func__);NSLog(@"url: %@", URL);returnYES;}

设置文本附件.png

小礼物走一走,来简书关注我

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

推荐阅读更多精彩内容