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 ...
随机推荐
- 2017-3-11 leetcode 217 219 228
ji那天好像是周六.....吃完饭意识到貌似今天要有比赛(有题解当然要做啦),跑回寝室发现周日才开始233333 =========================================== ...
- angular4 select 绑定(ngModel)对象
欢迎加入前端交流群交流知识&&获取视频资料:749539640 <h1>My Application</h1> <select [(ngModel)]=& ...
- Tomcat安全设置与优化详解(非原创)
一.Tomcat简介二.Tomcat安全设置三.Tomcat优化四.参考文章 一.Tomcat简介 Tomcat 是 Apache软件基金会下的一个免费.开源的WEB应用服务器,它可以运行在 Li ...
- 前端分页功能实现(PC)
<!DOCTYPE html><html> <head> <meta charset="utf-8"> <title>加 ...
- BZOJ 4742 DP
思路: Claris大大说了 排序以后 这个可以看成是括号序列 f[i][j][k]表示到了i j个左括号 k个右括号 (f[i][j][k]+=f[i-1][j][k])%=p; if(node[i ...
- TensorFlow-LSTM序列预测
问题情境:已知某一天内到目前为止股票各个时刻的价格,预测接下来短时间内的价格变化. import tushare as ts import time from collections import n ...
- 了解jQuery的$符号
$是什么? 可以使用typeof关键字来观察$的本质. console.log(type of $); //输出结果为function 因此可以得出结论,$其实就是一个函数.$(); 只是根据所给参数 ...
- DBGridEh checkbox的一个问题
function TCustomDBGridEh.CheckBeginRowMoving(MouseX, MouseY: Integer; CheckInOnly: Boolean): Boolean ...
- 快速登录MySQL数据库
[root@tdh51 docker]# mysql -h localhost -u transwarp -p$(cat /etc/transwarp-manager/master/db.proper ...
- JAVA代理方式使用示例总结
JAVA代理方式使用示例总结 一. 代理方式概括 Java的代理方式主要包含了静态代理,动态代理两种方式,其中,动态代理根据实现的方式不同,又可以划分为jdk动态代理和cglib动态代理. 二. ...