iOS富文本(一)属性化字符串
概述
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富文本(一)属性化字符串的更多相关文章
- iOS富文本组件的实现—DTCoreText源码解析 数据篇
本文转载 http://blog.cnbang.net/tech/2630/ DTCoreText是个开源的iOS富文本组件,它可以解析HTML与CSS最终用CoreText绘制出来,通常用于在一些需 ...
- iOS富文本(二)初识Text Kit
概述 Text Kit 是建立在Core Text上的文本布局系统,虽然没有Core Text那么强大的文本处理功能,但是对于大多数常见的文本布局用Text Kit能够很简单的实现,而不是用Core ...
- iOS 富文本属性
// NSFontAttributeName 设置字体属性,默认值:字体:Helvetica(Neue) 字号:12 // NSForegroundColorAttributeNam 设置字体颜色,取 ...
- iOS - 富文本
iOS--NSAttributedString超全属性详解及应用(富文本.图文混排) ios项目中经常需要显示一些带有特殊样式的文本,比如说带有下划线.删除线.斜体.空心字体.背景色.阴影以及图文 ...
- iOS富文本
背景:前些天突然想做一个笔记本功能,一开始,觉得挺简单的呀,一个UITextView,网络缓存也不干了,直接本地NSUserDefault存储,然后完事了,美工,弄几张好看的图片,加几个动画,也就这样 ...
- iOS - 富文本AttributedString
最近项目中用到了图文混排,所以就研究了一下iOS中的富文本,打算把研究的结果分享一下,也是对自己学习的一个总结. 在iOS中或者Mac OS X中怎样才能将一个字符串绘制到屏幕上呢? ...
- iOS富文本-NSAttributedString简单封装
直接调用系统的写起来比较麻烦,封装一下 因为要简单所以就写类方法 WJAttributeStyle 基类 ) { ; i < styles.count; i ++) { ...
- OS开发小记:iOS富文本框架DTCoreText在UITableView上的使用
要在页面中显示自己的布局,比如文字的字体和颜色.图文并排的样式,我们要用iOS SDK的原生UI在app本地搭建,如果一个页面需要在服务器端获取数据的话,我们也要在本地搭建好固定的布局,解析服务器传回 ...
- iOS-文本段落样式NSMutableParagraphStyle与NSParagraphStyle的使用和一些富文本处理属性
开发过程中,经常会遇到动态计算行高的问题, - (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)op ...
随机推荐
- poj1179 区间dp(记忆化搜索写法)有巨坑!
http://poj.org/problem?id=1179 Description Polygon is a game for one player that starts on a polygon ...
- Jetty:部署到Jetty
Web应用的框架 标准Jetty公布版本号能部署标准servlet Spec Web应用和Jetty内部ContextHandler部署描写叙述符,或者两者的一个混合. Web应用是可部署的动态(se ...
- Android与JS互相调用以及注意
近期项目中常常使用Html5而Android与JS调用常常会用到,这里记录一下,測试系统5.0以上. 这里先贴一下源代码 Activity: package jwzhangjie.com.webvie ...
- bzoj4240: 有趣的家庭菜园(树状数组+贪心思想)
4240: 有趣的家庭菜园 题目:传送门 题解: 好题!%%% 一开始不知道在想什么鬼,感觉满足二分性?感觉可以维护一个先单调增再单调减的序列? 然后开始一顿瞎搞...一WA 看一波路牌...树状数组 ...
- ASP.NET Razor - C# Variables
http://www.w3schools.com/aspnet/razor_cs_variables.asp Variables are named entities used to store da ...
- ES 断路器——本质上保护OOM提前抛出异常而已
监控fielddata使用了多少内存以及是否有数据被驱逐是非常重要的.大量的数据被驱逐会导致严重的资源问题以及不好的性能. Fielddata使用可以通过下面的方式来监控: 对于单个索引使用 {ref ...
- sublime界面主题
一直以来都是使用的SUBLIME,真的很强大. 最近刚转到linux来学习C,把它重新配置了一遍,默认的字体颜色的搭配已经很不错了.不过界面的样子还是不太习惯.重新安装了下soda这个主题包,惭愧!即 ...
- div向右偏移设置 css让div靠右移一定距离
转自:https://www.thinkcss.com/shili/1372.shtml div对象盒子向右偏移设置,使用css让div靠右一定距离-div向右移教程实例篇 div向右偏移一定距离,可 ...
- 【转】In ASP.NET using jQuery Uploadify upload attachment
Upload Uploadify is a JQuery plug-in, achieve the effect is very good, with progress display. Upload ...
- 软件需求规范说明 (Software Requirements Specification, 简称SRS)
GB/T 9385-2008 笔记 为了形成确定和完备的规格说明, 我们需要明确 软件的顾客希望得到什么; 软件的供方理解用户想要什么; 4.2 SRS的基本性质 SRS是对在具体环境中执行确定功能的 ...