NSAttributedString用法
以前看到这种字号和颜色不一样的字符串,想出个讨巧的办法就是“¥150”一个UILabel,“元/位”一个UILabel。今天翻看以前的工程,command点进UITextField中看到[attributedText]这个关键字,以前都没注意过UITextField还有这个属性,其实UITextView、UILable也有这个属性,iOS6就已经有了,说来惭愧,对此罚站1秒钟。
NSAttributedString叫做富文本,是一种带有属性的字符串,通过它可以轻松的在一个字符串中表现出多种字体、字号、字体大小等各不相同的风格,还可以对段落进行格式化。
通过以下代码即可实现上面图示效果,十分方便,从此再也不用设置两个UILable,并且处心积虑的处理它们的长度了。
UILabel * aLable = [[UILabel alloc] initWithFrame:CGRectMake(, , , )];
aLable.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:aLable]; NSString * aString = @"¥150 元/位"; //富文本对象
NSMutableAttributedString * aAttributedString = [[NSMutableAttributedString alloc] initWithString:aString]; //富文本样式
[aAttributedString addAttribute:NSForegroundColorAttributeName //文字颜色
value:[UIColor redColor]
range:NSMakeRange(, )]; [aAttributedString addAttribute:NSFontAttributeName //文字字体
value:[UIFont systemFontOfSize:]
range:NSMakeRange(, )]; aLable.attributedText = aAttributedString;
常用属性:
NSFontAttributeName 文字字体
NSParagraphStyleAttributeName 段落样式(字符串通过“\n”进行分段,此设置必须在lable.numberOfLines = 0时有效,value通过NSMutableParagraphStyle设置,它有以下属性)
[段落样式-插曲]
1 @property(readwrite) CGFloat lineSpacing; //行间距
@property(readwrite) CGFloat paragraphSpacing; //段间距
@property(readwrite) NSTextAlignment alignment; //对齐方式
@property(readwrite) CGFloat firstLineHeadIndent; //首行缩进
@property(readwrite) CGFloat headIndent; //除首行之外其他行缩进
@property(readwrite) CGFloat tailIndent; //每行容纳字符的宽度
@property(readwrite) NSLineBreakMode lineBreakMode; //换行方式
@property(readwrite) CGFloat minimumLineHeight; //最小行高
@property(readwrite) CGFloat maximumLineHeight; //最大行高
@property(readwrite) NSWritingDirection baseWritingDirection; //书写方式(NSWritingDirectionNatural,NSWritingDirectionLeftToRight,NSWritingDirectionRightToLeft)
@property(readwrite) CGFloat lineHeightMultiple;
@property(readwrite) CGFloat paragraphSpacingBefore;
@property(readwrite) float hyphenationFactor;
@property(readwrite,copy,NS_NONATOMIC_IOSONLY) NSArray *tabStops NS_AVAILABLE_IOS(7_0);
@property(readwrite,NS_NONATOMIC_IOSONLY) CGFloat defaultTabInterval NS_AVAILABLE_IOS(7_0);
[段落样式demo]
1 UILabel * lable = [[UILabel alloc] initWithFrame:CGRectMake(, , self.view.frame.size.width-, )];
lable.backgroundColor = [UIColor lightGrayColor];
lable.numberOfLines = ;
[self.view addSubview:lable]; NSString * string = @"Always believe that something wonderful is about \nto happen!"; //富文本
NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc] initWithString:string]; //段落样式
NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init]; #warning lable.numberOfLines必须为0,段落样式才生效
//行间距
paragraphStyle.lineSpacing = 10.0;
//段落间距
paragraphStyle.paragraphSpacing = 20.0; // paragraphStyle.baseWritingDirection = NSWritingDirectionLeftToRight;
// paragraphStyle.firstLineHeadIndent = 10.0;
// paragraphStyle.headIndent = 50.0;
// paragraphStyle.tailIndent = 200.0; [attributedString addAttribute:NSParagraphStyleAttributeName
value:paragraphStyle
range:NSMakeRange(, string.length)]; 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:字符串有下划线)
属性挺多的,有其他需要的话command点进去看一下就ok,如果对他们的功能不了解三根指头点一下关键词,或者按住option点一下看看官方文档的Description相信就会有所了解了,其他的暂时就不介绍了。
如有问题,欢迎指正,小弟在此拜谢。
这里有篇讲解富文本的文章:http://www.2cto.com/kf/201409/334308.html
本文参考源码:https://github.com/NSSONGMENG/Practice/tree/master/LessonAttributedString
饿死了,吃饭去嘞~
最新文章
- WM_MOUSELEAVE和WM_MOUSEHOVER使用
- iOS开发——多线程篇——NSThread
- Arduino101学习笔记(二)—— 一些注意的语法点
- Linux下bash: scp: command not found问题 或者装ssh包时报错 Requires: libedit.so.0()(64bit)
- delegate事件绑定
- 轮式移动机器人QBot的使用
- KM算法详解+模板
- PHP、Java、C#实现URI参数签名算法,确保应用与REST服务器之间的安全通信,防止Secret Key盗用、数据篡改等恶意攻击行为
- Opencv——灰度直方图
- Java导入证书失败Keystore was tampered with, or password was incorrect
- DBNull.value
- 【Java提高】---枚举的应用
- Linux学习之CentOS(十八)-----恢复Ext3下被删除的文件与 使用grep恢复被删文件内容(转)
- 最简单的基于DirectShow的示例:视频播放器自定义版
- Python-Django-Ajax进阶2
- Find out where to contain the smartforms
- linux sar 命令详解(历史资源查看,如内存、CUP等等)
- PostgreSQL数组使用
- Docker Kubernetes 容器更新与回滚
- java 导包
热门文章
- gmail
- Ext.form.FormPanel定义的参数说明
- TCP三次握手和连接关闭过程详解
- 关于 Unity UGUI 中修改 Mask 组件下 Image 等子节点组件的材质无效的问题
- 控制反转(IoC)与依赖注入(DI)
- php 获取某个月的周次信息
- .Net训练营优惠有条件 做到立减800元大钞
- XMind十大最有用的功能
- 修正android cocos2dx项目当点击属性时提示错误的问题
- eclipse 配置Maven问题解决办法:新建maven工程时报错:Could not resolve archetype org.apache.maven.archetypes .