iOS富文本(一)属性化字符串
2015-10-26 19:39
671 查看
概述
iOS一些复杂的文本布局一般都是由底层的
Core Text来实现的,直到
iOS7苹果发布了
Text Kit框架,
Text Kit能够很简单实现一些复杂的文本样式以及布局,而
Text Kit富文本框架用到的核心数据结构就是属性化字符串
NSAttributeString,本篇文章将介绍
NSAttributeString一些常用属性和使用方法。
字体样式
NSAttributeString有很多属性提供来改变字体的样式,下面代码只是列出了一些常用的属性,如果想更改更多的字体样式请参考苹果官方文档,使用方法大同小异。
代码示例
@interface ViewController () @property (weak,nonatomic) IBOutlet UILabel *stringLabel; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; NSString *string = _stringLabel.text; //初始化属性字符串 NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string]; //字体类型属性 NSDictionary *BoldFontAS = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:17]}; [attributedString addAttributes:BoldFontAS range:[string rangeOfString:@"BoldFont"]]; //字体颜色属性 NSDictionary *RedFondAS = @{NSForegroundColorAttributeName :[UIColor redColor]}; [attributedString addAttributes:RedFondAS range:[string rangeOfString:@"RedFont"]]; //字体背景颜色和字体颜色属性 NSDictionary *BuleBackgroundAS = @{NSBackgroundColorAttributeName:[UIColor blueColor], NSForegroundColorAttributeName:[UIColor whiteColor]}; [attributedString addAttributes:BuleBackgroundAS range:[string rangeOfString:@"BuleBackground"]]; //字体下划线与字体下划线颜色属性 NSDictionary *UnderlineAS = @{NSUnderlineStyleAttributeName:[NSNumber numberWithInteger:NSUnderlineStyleSingle],NSUnderlineColorAttributeName:[UIColor greenColor]}; [attributedString addAttributes:UnderlineAS range:[string rangeOfString:@"Underline"]]; //字体阴影属性 NSShadow *shadow = [[NSShadow alloc] init]; shadow.shadowOffset = CGSizeMake(2, 2); shadow.shadowColor = [UIColor orangeColor]; NSDictionary *ShadowAS = @{NSShadowAttributeName:shadow}; [attributedString addAttributes:ShadowAS range:[string rangeOfString:@"Shadow"]]; //设置Label的字符串属性 _stringLabel.attributedText = attributedString; }
实现效果
段落样式
NSAttributeString的段落样式包括外边距,字体对齐,字体缩进等。在
iOS中段落用
\n用来分隔,如果在一个段落中使用多个段落样式的话,那么只对段落中第一个字符使用的样式有效。在一段文字中如果没有
\n的话那么这一段文字就是一个段落。在显示中常常文字过长系统会自动换行,不管换多少行都只是一个段落。
对整体的文本应用段落样式
@interface ViewController () @property (weak, nonatomic) IBOutlet UILabel *paragraphLabel; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; NSString * paragraphString = @"An NSAttributedString object manages character strings\nand associated sets of attributes (for example, font and kerning)\nthat apply to individual characters or ranges of characters in the string."; NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init]; [paragraph setLineSpacing:8]; //对每一个段落设置行间距 [paragraph setParagraphSpacing:15]; //设置段落之间的间距 [paragraph setFirstLineHeadIndent:20]; //设置每个段落的首行缩进 //初始化段落属性 NSDictionary *attribute = @{NSParagraphStyleAttributeName:paragraph}; NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:paragraphString attributes:attribute]; _paragraphLabel.attributedText = attributeString; }
设置段落前
设置段落后
相关文章推荐
- IOS之同步请求、异步请求、GET请求、POST请求
- iOS:hidesBottomBarWhenPushed的正确用法
- [iOS进阶]Reveal使用技巧
- iOS学习笔记1(结合项目)---oc的@property属性
- #在蓝懿学习iOS的日子#Day5
- iOS 判断字符串中含有某个字符串 rangeOfString
- NetBios漏洞攻防
- iOS Crash获取闪回日志和上传server
- IOS9.0升级后以及xcode7后引起的问题
- nagios监控windows主机
- iOS 2D绘图详解(Quartz 2D)之路径(stroke,fill,clip,subpath,blend)
- iOS开发>学无止境 - 延长启动画面显示时间
- iOS 获取视频的第一帧 CMTimeMakeWithSeconds CMTimeMake
- ios中自定义cell 设置cell的分组结构
- iOS—OC——runtime
- iOS 库
- ios9 不受信任的开发者
- iOS- 详解文本属性Attributes
- ios 得到每周的星期一开始和星期天结束的日期
- iOS开发系列--数据存取