原文链接:http://www.jianshu.com/p/09f54730feaa

先看看所有的Key

NSFontAttributeName; //字体,value是UIFont对象
NSParagraphStyleAttributeName;//绘图的风格(居中,换行模式,间距等诸多风格),value是NSParagraphStyle对象
NSForegroundColorAttributeName;// 文字颜色,value是UIFont对象
NSBackgroundColorAttributeName;// 背景色,value是UIFont
NSLigatureAttributeName; // 字符连体,value是NSNumber
NSKernAttributeName; // 字符间隔
NSStrikethroughStyleAttributeName;//删除线,value是NSNumber
NSUnderlineStyleAttributeName;//下划线,value是NSNumber
NSStrokeColorAttributeName; //描绘边颜色,value是UIColor
NSStrokeWidthAttributeName; //描边宽度,value是NSNumber
NSShadowAttributeName; //阴影,value是NSShadow对象
NSTextEffectAttributeName; //文字效果,value是NSString
NSAttachmentAttributeName;//附属,value是NSTextAttachment 对象
NSLinkAttributeName;//链接,value是NSURL or NSString
NSBaselineOffsetAttributeName;//基础偏移量,value是NSNumber对象
NSUnderlineColorAttributeName;//下划线颜色,value是UIColor对象
NSStrikethroughColorAttributeName;//删除线颜色,value是UIColor
NSObliquenessAttributeName; //字体倾斜
NSExpansionAttributeName; //字体扁平化
NSVerticalGlyphFormAttributeName;//垂直或者水平,value是 NSNumber,0表示水平,1垂直

NSAttributedString 可以非常方便的实现文字排版和图文混排功能. 共有21种效果(API), 本文将较详细的介绍21种属性的使用

核心API: NSAttributedString. NSMutableAttributedString.

  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用来保存各个子串/字符的属性。
*/
#pragma mark - NSMutableAttributedString 创建
/* 三种初始化方法,NSMutableAttributedString没有初始化方法,使用父类初始化方法, 使用initWithString:, initWithString:attributes:, 或者 initWithAttributedString: */
NSAttributedString *attStr = [[NSAttributedString alloc] initWithString:string attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:30]}]; NSMutableAttributedString *mAttStr = [[NSMutableAttributedString alloc] initWithString:string];

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

/* 设置字体大小及字体类型 */
NSRange font_range = [string rangeOfString:@"An"];
[mAttStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20] range:font_range];
[mAttStr addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Courier-BoldOblique" size:17.0] range:NSMakeRange(10, 10)];

2.NSParagraphStyleAttributeName ->设置文本段落排版格式,取值为 NSParagraphStyle 对象(详情见如下代码)

  • NSParagraphStyle与NSMutableParagraphStyle包括以下属性

    • alignment ->对齐方式
    • firstLineHeadIndent ->首行缩进
    • headIndent ->缩进
    • tailIndent ->尾部缩进
    • lineBreakMode ->断行方式
    • maximumLineHeight ->最大行高
    • minimumLineHeight ->最低行高
    • lineSpacing ->行距
    • paragraphSpacing ->段距
    • paragraphSpacingBefore ->段首空间
    • baseWritingDirection ->句子方向
    • lineHeightMultiple ->可变行高,乘因数。
    • hyphenationFactor ->连字符属性
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
style.firstLineHeadIndent = 10;
style.lineSpacing = 10; [mAttStr addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(10, 10)];

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

[mAttStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(10, 10)];

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

[mAttStr addAttribute:NSBackgroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(10, 10)];

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

[mAttStr addAttribute:NSLigatureAttributeName value:@1 range:NSMakeRange(10, 10)];

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

[mAttStr addAttribute:NSKernAttributeName value:@2 range:NSMakeRange(10, 10)];

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

/* 值为整型NSNumber,可取值为 NSUnderlineStyle
enum {
NSUnderlineStyleNone = 0x00,
NSUnderlineStyleSingle = 0x01,
NSUnderlineStyleThick NS_ENUM_AVAILABLE(10_0, 7_0) = 0x02,
NSUnderlineStyleDouble NS_ENUM_AVAILABLE(10_0, 7_0) = 0x09, NSUnderlinePatternSolid NS_ENUM_AVAILABLE(10_0, 7_0) = 0x0000,
NSUnderlinePatternDot NS_ENUM_AVAILABLE(10_0, 7_0) = 0x0100,
NSUnderlinePatternDash NS_ENUM_AVAILABLE(10_0, 7_0) = 0x0200,
NSUnderlinePatternDashDot NS_ENUM_AVAILABLE(10_0, 7_0) = 0x0300,
NSUnderlinePatternDashDotDot NS_ENUM_AVAILABLE(10_0, 7_0) = 0x0400, NSUnderlineByWord NS_ENUM_AVAILABLE(10_0, 7_0) = 0x8000
}; 设置删除线。
*/
[mAttStr addAttribute:NSStrikethroughStyleAttributeName value:@3 range:NSMakeRange(10, 10)];

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

[mAttStr addAttribute:NSStrikethroughColorAttributeName value:[UIColor blueColor] range:NSMakeRange(10, 10)];

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

[mAttStr addAttribute:NSUnderlineStyleAttributeName value:@3 range:NSMakeRange(10, 10)];

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

[mAttStr addAttribute:NSUnderlineColorAttributeName value:[UIColor blackColor] range:NSMakeRange(10, 10)];

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

[mAttStr addAttribute:NSStrokeWidthAttributeName value:@10 range:NSMakeRange(10, 10)];

12.NSStrokeColorAttributeName ->填充部分颜色,不是字体颜色,取值为 UIColor 对象 默认值为nil,设置的属性同ForegroundColor

[mAttStr addAttribute:NSStrokeColorAttributeName value:[UIColor radColor] range:NSMakeRange(10, 10)];

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

NSShadow *shadow = [[NSShadow alloc]init];
shadow.shadowOffset = CGSizeMake(10, 10);
shadow.shadowColor = [UIColor redColor];
[mAttStr addAttribute:NSShadowAttributeName value:shadow range:NSMakeRange(10, 10)];

14.NSTextEffectAttributeName ->设置文本特殊效果,取值为 NSString 对象,目前只有图版印刷效果可用, 使用此属性指定的文字效果,如NSTextEffectLetterpressStyle。此属性的默认值为nil,表示没有文本效应

[mAttStr addAttribute:NSTextEffectAttributeName value:NSTextEffectLetterpressStyle range:NSMakeRange(10, 10)];

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

[mAttStr addAttribute:NSBaselineOffsetAttributeName value:@1 range:NSMakeRange(10, 10)];

16.NSObliquenessAttributeName ->设置字形倾斜度,取值为 NSNumber (float),正值右倾,负值左倾, 默认值是0(表示没有倾斜)

[mAttStr addAttribute:NSObliquenessAttributeName value:@0.5 range:NSMakeRange(10, 10)];

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

[mAttStr addAttribute:NSExpansionAttributeName value:@1.0 range:NSMakeRange(10, 10)];

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

//取值为包含NSNumber对象的数组. 从左向右书写或者从右向左书写.
// 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 NSTextWritingDirection values. LRE:
NSWritingDirectionLeftToRight|NSWritingDirectionEmbedding, RLE:
NSWritingDirectionRightToLeft|NSWritingDirectionEmbedding, LRO:
NSWritingDirectionLeftToRight|NSWritingDirectionOverride, RLO:
NSWritingDirectionRightToLeft|NSWritingDirectionOverride, [mAttStr addAttribute:NSWritingDirectionAttributeName value:@[@2] range:NSMakeRange(10, 10)];

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

[mAttStr addAttribute:NSVerticalGlyphFormAttributeName value:@0 range:NSMakeRange(10, 10)];

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

/**
* 此属性的值是NSURL对象(首选)或一个NSString对象。此属性的默认值为nil,表示没有链接。
* UILabel无法使用该属性, 可以使用UITextView 控件.
*/
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(50, 50, 50, 50)];
[self.view addSubview:textView];
textView.backgroundColor = [UIColor redColor]; NSString *strLink = @"百度链接";
NSAttributedString *attStrUrl = [[NSAttributedString alloc] initWithString:strLink attributes:@{NSLinkAttributeName: [NSURL URLWithString:@"http://www.baidu.com"]}]; textView.editable = NO; /* 签订协议, 指定代理人之后. 但点击链接时, 会回调协议方法 (- textView:shouldInteractWithURL:inRange:) */
textView.delegate = self; textView.attributedText = attStr;
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange {

    NSLog(@"%s", __FUNCTION__);
NSLog(@"url: %@", URL);
return YES;
}

注意:

@interface ViewController () <UITextViewDelegate>

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

/* 这个属性的值是一个NSTextAttachment对象。此属性的默认值为nil,表示无附件。*/

    /**
* 关于NSTextAttachment类的简单说明
*
* NSTextAttachment 类有一个指定的初始化方法(- initWithData:ofType:), 需要指定附件文档的数据和附件文件的类型. 如果附件文档数据指定nil, 那么系统将会默认指定为image对象作为值. 因此, 也可以通过这个特性实现图文混排.
* 下面就以附件为image对象来说明NSAttachmentAttributeName的使用.
*
*/ UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 50, 50, 50)];
label.backgroundColor = [UIColor redColor];
[self.view addSubview:label]; /* 下面实现在百度两个汉字之间插入一个照片 */
NSString *stiAtt = @"百度"; NSTextAttachment *attach = [[NSTextAttachment alloc] initWithData:nil ofType:nil];
attach.bounds = CGRectMake(0, 0, 50, 50);
attach.image = [UIImage imageNamed:@"taobao.jpg"]; NSAttributedString *strAtt = [NSAttributedString attributedStringWithAttachment:attach]; NSMutableAttributedString *strMatt = [[NSMutableAttributedString alloc] initWithString:stiAtt]; [strMatt insertAttributedString:strAtt atIndex:1]; label.attributedText = strMatt; self.titleLabel.attributedText = mAttStr;
[self.titleLabel sizeToFit];

在此要感谢我的启蒙老师, 原哥, 是他带我走上了iOS编程这条"不归"之路!

NSAttributedString 的21种属性 详解的更多相关文章

  1. tomcat 三种部署方式以及server.xml文件的几个属性详解

    一.直接将web项目文件件拷贝到webapps目录中 这是最常用的方式,Tomcat的Webapps目录是Tomcat默认的应用目录,当服务器启动时,会加载所有这个目录下的应用.如果你想要修改这个默认 ...

  2. Android 三种动画详解

    [工匠若水 http://blog.csdn.net/yanbober 转载请注明出处.点我开始Android技术交流] 1 背景 不能只分析源码呀,分析的同时也要整理归纳基础知识,刚好有人微博私信让 ...

  3. Linux文件权限与属性详解 之 一般权限

    目录 一般属性 1. iNode: 3152621 2. 文件类型 3.文件访问权限 4. 链接数目: 5. 文件所有者 6. 文件所属组 7. 文件大小 8. 修改时间 9. 文件名称 Linux文 ...

  4. Linux文件权限与属性详解 之 su & sudo

    Linux文件权限与属性详解 之 一般权限 Linux文件权限与属性详解 之 ACL Linux文件权限与属性详解 之 SUID.SGID & SBIT Linux文件权限与属性详解 之 ch ...

  5. [ 转载 ] Java开发中的23种设计模式详解(转)

    Java开发中的23种设计模式详解(转)   设计模式(Design Patterns) ——可复用面向对象软件的基础 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类 ...

  6. Android开发–Intent-filter属性详解

    Android开发–Intent-filter属性详解 2011年05月09日 ⁄ Andriod ⁄ 暂无评论 ⁄ 被围观 1,396 views+ 如果一个 Intent 请求在一片数据上执行一个 ...

  7. Android零基础入门第80节:Intent 属性详解(下)

    上一期学习了Intent的前三个属性,本期接着学习其余四个属性,以及Android系统常用内置组件的启动. 四.Data和Type属性 Data属性通常用于向Action属性提供操作的数据.Data属 ...

  8. OutputCache属性详解(三)— VaryByHeader,VaryByCustom

    目录 OutputCache概念学习 OutputCache属性详解(一) OutputCache属性详解(二) OutputCache属性详解(三) OutputCache属性详解(四)— SqlD ...

  9. WPF依赖属性详解

    WPF依赖属性详解 WPF 依赖属性 英文译为 Dependency Properties,是WPF引入的一种新类型的属性,在WPF中有着极为广泛的应用,在WPF中对于WPF Dependency P ...

随机推荐

  1. Dubbo协议与连接控制

    协议参考手册 (+) (#) 推荐使用Dubbo协议 性能测试报告各协议的性能情况,请参见:性能测试报告 (+) dubbo:// (+) (#) Dubbo缺省协议采用单一长连接和NIO异步通讯,适 ...

  2. 【转】Spring事务超时时间可能存在的错误认识

    1.先看代码 1.1.spring-config.xml <bean id="dataSource" class="org.springframework.jdbc ...

  3. Openjudge-计算概论(A)-短信计费

    描述: 用手机发短信,一条短信资费为0.1元,但限定一条短信的内容在70个字以内(包括70个字).如果你一次所发送的短信超过了70个字,则会按照每70个字一条短信的限制把它分割成多条短信发送.假设已经 ...

  4. UIButton的属性设置

    1.背景颜色 btn.backgroundColor = [UIColor redColor]; 2.给按钮添加文字并添加显示状态  [btn setTitle@"播放" forS ...

  5. ural 1091. Tmutarakan Exams(容斥原理)

    1091. Tmutarakan Exams Time limit: 1.0 secondMemory limit: 64 MB University of New Tmutarakan trains ...

  6. 1077. [NOIP2010冲刺六] 数列游戏

    [题目描述] 小M很喜欢找点游戏自娱自乐.有一天,她在纸上写了一串数字:1,1,2,5,4.接着她擦掉了一个1,结果发现剩下1,2,4都在自己所在的位置上,即1在第1位,2在第2位,4在第4位.她希望 ...

  7. JQ和其他框架一起使用方法

    时下,越来越多的javascripe框架不断崛起,同时开源网站系统也之间增多.网站建设过程中当使用一些开源的网站程序时,免不了会在javascript上产生冲突.也许网站的开发者习惯使用jQuery, ...

  8. html 图片上传预览

    Html5 upload img 2012年12月27日 20:36 <!DOCTYPE HTML> <html> <head> <meta http-equ ...

  9. SQL STUFF函数 拼接字符串

    今日看到一篇文章,是关于和并列的,也研究了下,还是不错的 要这种效果. create table tb(idint, value varchar(10)) insert into tbvalues(1 ...

  10. # Linux Whois3获取 运营商信息

    Linux Whois3获取 运营商信息 APNIC是管理亚太地区IP地址分配的机构,它有着丰富准确的IP地址分配库,同时这些信息也是对外公开的,并提供了一个查询工具,下面就让我们看看如何在Linux ...