标签:

以前看到这种字号和颜色不一样的字符串,想出个讨巧的办法就是“¥150”一个UILable,“元/位”一个UILable。今天翻看以前的工程,command点进UITextField中看到[attributedText]这个关键字,以前都没注意过UITextField还有这个属性,其实UITextView、UILable也有这个属性,iOS6就已经有了,说来惭愧,对此罚站1秒钟。

NSAttributedString叫做富文本,是一种带有属性的字符串,通过它可以轻松的在一个字符串中表现出多种字体、字号、字体大小等各不相同的风格,还可以对段落进行格式化。

通过以下代码即可实现上面图示效果,十分方便,从此再也不用设置两个UILable,并且处心积虑的处理它们的长度了。

 1     UILabel * aLable = [[UILabel alloc] initWithFrame:CGRectMake(100, 500, 200, 40)];
2 aLable.textAlignment = NSTextAlignmentCenter;
3 [self.view addSubview:aLable];
4
5 NSString * aString = @"¥150 元/位";
6
7 //富文本对象
8 NSMutableAttributedString * aAttributedString = [[NSMutableAttributedString alloc] initWithString:aString];
9
10 //富文本样式
11 [aAttributedString addAttribute:NSForegroundColorAttributeName //文字颜色
12 value:[UIColor redColor]
13 range:NSMakeRange(0, 4)];
14
15 [aAttributedString addAttribute:NSFontAttributeName //文字字体
16 value:[UIFont systemFontOfSize:25]
17 range:NSMakeRange(0, 4)];
18
19 aLable.attributedText = aAttributedString;

常用属性:

NSFontAttributeName           文字字体

NSParagraphStyleAttributeName     段落样式(字符串通过“\n”进行分段,此设置必须在lable.numberOfLines = 0时有效,value通过NSMutableParagraphStyle设置,它有以下属性)

 [段落样式-插曲]
1 @property(readwrite) CGFloat lineSpacing;              //行间距
2 @property(readwrite) CGFloat paragraphSpacing;           //段间距
3 @property(readwrite) NSTextAlignment alignment;           //对齐方式
4 @property(readwrite) CGFloat firstLineHeadIndent;          //首行缩紧
5 @property(readwrite) CGFloat headIndent;               //除首行之外其他行缩进
6 @property(readwrite) CGFloat tailIndent;               //每行容纳字符的宽度
7 @property(readwrite) NSLineBreakMode lineBreakMode;        //换行方式
8 @property(readwrite) CGFloat minimumLineHeight;           //最小行高
9 @property(readwrite) CGFloat maximumLineHeight;           //最大行高
10 @property(readwrite) NSWritingDirection baseWritingDirection;  //书写方式(NSWritingDirectionNatural,NSWritingDirectionLeftToRight,NSWritingDirectionRightToLeft)
11 @property(readwrite) CGFloat lineHeightMultiple;
12 @property(readwrite) CGFloat paragraphSpacingBefore;
13 @property(readwrite) float hyphenationFactor;
14 @property(readwrite,copy,NS_NONATOMIC_IOSONLY) NSArray *tabStops NS_AVAILABLE_IOS(7_0);
15 @property(readwrite,NS_NONATOMIC_IOSONLY) CGFloat defaultTabInterval NS_AVAILABLE_IOS(7_0);
 [段落样式demo]
1 UILabel * lable = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, self.view.frame.size.width-100, 200)];
2 lable.backgroundColor = [UIColor lightGrayColor];
3 lable.numberOfLines = 0;
4 [self.view addSubview:lable];
5
6 NSString * string = @"Always believe that something wonderful is about \nto happen!";
7
8 //富文本
9 NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc] initWithString:string];
10
11 //段落样式
12 NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
13
14 #warning lable.numberOfLines必须为0,段落样式才生效
15 //行间距
16 paragraphStyle.lineSpacing = 10.0;
17 //段落间距
18 paragraphStyle.paragraphSpacing = 20.0;
19
20 // paragraphStyle.baseWritingDirection = NSWritingDirectionLeftToRight;
21 // paragraphStyle.firstLineHeadIndent = 10.0;
22 // paragraphStyle.headIndent = 50.0;
23 // paragraphStyle.tailIndent = 200.0;
24
25 [attributedString addAttribute:NSParagraphStyleAttributeName
26 value:paragraphStyle
27 range:NSMakeRange(0, string.length)];
28
29 lable.attributedText = attributedString;


NSForegroundColorAttributeName    文字前景色

NSBackgroundColorAttributeName     文字背景色

NSLigatureAttributeName        连体字(NSNumber  @0:无连体,@1:默认连体,系统字体不包含对连体的支持)

NSUnderlineStyleAttributeName     下划线

NSStrokeColorAttributeName       只有在NSStrokeWidthAttributeName设置了值之后才有效(默认字体颜色和前景色一致,如果设置的颜色和前景色不一致则前景色无效)

NSStrokeWidthAttributeName      设置该属性之后字体变成空心字体,字体边线宽度为value设定的值

NSBaselineOffsetAttributeName     值为NSNumber类型,表明文字相对于其他文字基准线向上的偏移量

NSUnderlineColorAttributeName      值为UIColor类型,下划线颜色(只有在NSUnderlineStyleAttributeName的value为@1时有效)

NSUnderlineStyleAttributeName      值为NSNumber类型,下划线宽度(默认值为@0:下划线宽度为0——不现实下划线,@1:字符串有下划线)

NSAttributedString的用法的更多相关文章

  1. iOS属性文字NSAttributedString

    它本身是一个Foundation框架的类, 但如果要使用它主要用到了UIKit框架中的NSAttributedString中的一些常量字符串 ----------------------------- ...

  2. NSAttributedString用法

    以前看到这种字号和颜色不一样的字符串,想出个讨巧的办法就是“¥150”一个UILabel,“元/位”一个UILabel.今天翻看以前的工程,command点进UITextField中看到[attrib ...

  3. 富文本常用封装(NSAttributedString浅析)

    最近经常遇到关于富文本的一些需求,特此封装了几个最常用的API分享给大家,但授之以鱼不如授之以渔,接下来会顺便谈谈NSAttributedString,确保你读了本篇文章能够自己封装关于富文本的API ...

  4. NSAttributedString富文本简单介绍和常用方法浅析

    NSAttributedString基本知识点介绍 1.初始化方法 - (instancetype)initWithString:(NSString *)str; - (instancetype)in ...

  5. UI各种小控件的用法

    今天给大家列举出来UI中的一些小控件的用法.方便大的学习与使用 一些方法和属性我们能够查看API文档.不必将每一个控件的功能都记住, 由于在使用的过程中,我们能够查看API文档.方便使用,我们仅仅要记 ...

  6. EditText 基本用法

    title: EditText 基本用法 tags: EditText,编辑框,输入框 --- EditText介绍: EditText 在开发中也是经常用到的控件,也是一个比较必要的组件,可以说它是 ...

  7. jquery插件的用法之cookie 插件

    一.使用cookie 插件 插件官方网站下载地址:http://plugins.jquery.com/cookie/ cookie 插件的用法比较简单,直接粘贴下面代码示例: //生成一个cookie ...

  8. Java中的Socket的用法

                                   Java中的Socket的用法 Java中的Socket分为普通的Socket和NioSocket. 普通Socket的用法 Java中的 ...

  9. [转载]C#中MessageBox.Show用法以及VB.NET中MsgBox用法

    一.C#中MessageBox.Show用法 MessageBox.Show (String) 显示具有指定文本的消息框. 由 .NET Compact Framework 支持. MessageBo ...

随机推荐

  1. 两端对齐(兼容较好,支持IE)

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  2. Visual Studio原生开发的10个调试技巧(二)

    来源:oschina 发布时间:2013-08-10 阅读次数:397 51   我以前关于Visual Studio调试技巧的文章引起了大家很大的兴趣,以至于我决定分享更多调试的知识.以下的列表中你 ...

  3. Qt之QRoundProgressBar(圆形进度条)

    简述 QRoundProgressBar类能够实现一个圆形进度条,继承自QWidget,并且有和QProgressBar类似的API接口. 简述 详细说明 风格 颜色 字体 共有函数 共有槽函数 详细 ...

  4. 关于使用dotnetbar开发winform程序在用户电脑上部署时问题

    1.首先要安装两个软件

  5. SQL Server数据库(SQL Sever语言 事务)

    事务:保障流程的完整执行保证程序某些程序在运行时同时成功同时失败,保证程序的安全性 begin tran --在流程开始的位置加 --此处写SQL语句 if @@error>0 --ERRORS ...

  6. uva 1629

    1629 - Cake slicing Time limit: 3.000 seconds A rectangular cake with a grid of m * n <tex2html_v ...

  7. PHP获取指定时间的上个月

    主要用strtotime()这个函数 php 获得前一个月的月份 date("Y-m-d",strtotime("last month")); php获得给定时 ...

  8. PDF 补丁丁 0.4.1 版:新增嵌入中文字库、替换文档字库的功能

    PDF 补丁丁 0.4.1 版新增了嵌入中文字库.替换文档字库的功能. 嵌入汉字字库 历史上有一批黄底黑字的 PDF 文档.这批文档都具有相同的问题:没有嵌入字库.在一些设备上阅读时显示乱码.复制文本 ...

  9. sed 替换

    sed -i 's/i=0/i=2/g' test2.sh -i 在当前文档替换 g 替换所有文档 sed -i '3s/cccc/ccccc/' a.txt 将第三行的 cccc 替换成 ccccc ...

  10. 如何在 CentOS 中设置 NTP 服务器

    网络时间协议(NTP)用来同步网络上不同主机的系统时间.你管理的所有主机都可以和一个指定的被称为 NTP 服务器的时间服务器同步它们的时间.而另一方面,一个 NTP 服务器会将它的时间和任意公共 NT ...