概述

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富文本(一)属性化字符串的更多相关文章

  1. iOS富文本组件的实现—DTCoreText源码解析 数据篇

    本文转载 http://blog.cnbang.net/tech/2630/ DTCoreText是个开源的iOS富文本组件,它可以解析HTML与CSS最终用CoreText绘制出来,通常用于在一些需 ...

  2. iOS富文本(二)初识Text Kit

    概述 Text Kit 是建立在Core Text上的文本布局系统,虽然没有Core Text那么强大的文本处理功能,但是对于大多数常见的文本布局用Text Kit能够很简单的实现,而不是用Core ...

  3. iOS 富文本属性

    // NSFontAttributeName 设置字体属性,默认值:字体:Helvetica(Neue) 字号:12 // NSForegroundColorAttributeNam 设置字体颜色,取 ...

  4. iOS - 富文本

    iOS--NSAttributedString超全属性详解及应用(富文本.图文混排)   ios项目中经常需要显示一些带有特殊样式的文本,比如说带有下划线.删除线.斜体.空心字体.背景色.阴影以及图文 ...

  5. iOS富文本

    背景:前些天突然想做一个笔记本功能,一开始,觉得挺简单的呀,一个UITextView,网络缓存也不干了,直接本地NSUserDefault存储,然后完事了,美工,弄几张好看的图片,加几个动画,也就这样 ...

  6. iOS - 富文本AttributedString

    最近项目中用到了图文混排,所以就研究了一下iOS中的富文本,打算把研究的结果分享一下,也是对自己学习的一个总结. 在iOS中或者Mac OS X中怎样才能将一个字符串绘制到屏幕上呢?         ...

  7. iOS富文本-NSAttributedString简单封装

    直接调用系统的写起来比较麻烦,封装一下 因为要简单所以就写类方法 WJAttributeStyle 基类 ) {         ; i < styles.count; i ++) {      ...

  8. OS开发小记:iOS富文本框架DTCoreText在UITableView上的使用

    要在页面中显示自己的布局,比如文字的字体和颜色.图文并排的样式,我们要用iOS SDK的原生UI在app本地搭建,如果一个页面需要在服务器端获取数据的话,我们也要在本地搭建好固定的布局,解析服务器传回 ...

  9. iOS-文本段落样式NSMutableParagraphStyle与NSParagraphStyle的使用和一些富文本处理属性

    开发过程中,经常会遇到动态计算行高的问题, - (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)op ...

随机推荐

  1. Linux下叹号!的用法

    在定义包含!的字符串变量时,出现了以下提示错误. 注意:在centos系统下错误,在Fedora系统可以正常运行. 修正方法有以下几种: (1)将整个字符串用单引号引起来. (2)在!后添加空格. ( ...

  2. WINDOWS XP 系统显示乱码的解决方法(修改注册表,使用正常字体)

            一位同事的计算机进入WINDOWS XP系统后,电脑里的所有汉字全部显示乱码,很多办公文档无法打开而影响工作.因为第一次遇到这种问题,当然首先是百度解决了,搜索了相关的信息后找到了答案 ...

  3. luogu3865 【模板】 ST表

    题目大意:给出一段序列,每次查询一段区间,求区间最大值. ST表:设原序列为A,定义F[i][k]为A[i][2k-1]的最大值.有递归式:F[i][k]=max(F[i][k-1], F[i+2k- ...

  4. [Linux]history 显示命令执行的时间

    显示历史命令之行时间 这里的环境是centos5.8 vim  ~/.bashrc 或者 ~/.bash_profile 增加 export HISTTIMEFORMAT="%F %T  & ...

  5. poj1700--贪心--Crossing River

    Crossing River Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12260   Accepted: 4641 D ...

  6. 杂项-DB:时序数据库

    ylbtech-杂项-DB:时序数据库 1.返回顶部 1. 时序数据库全称为时间序列数据库.时间序列数据库主要用于指处理带时间标签(按照时间的顺序变化,即时间序列化)的数据,带时间标签的数据也称为时间 ...

  7. HBase学习----windows10下使用eclipse搭建HBase的开发环境

    以下是我搭建HBase开发环境的一些心得(windows10) 0.安装JDK和eclipse和一个可用的HBase. 这步是最基础的,在此就不赘述了 1.创建一个java项目: 基础问题,不赘述. ...

  8. caffe 参数介绍 solver.prototxt

    转载自 http://blog.csdn.net/cyh_24/article/details/51537709 solver.prototxt net: "models/bvlc_alex ...

  9. C - Ilya and Sticks(贪心)

    Problem description In the evening, after the contest Ilya was bored, and he really felt like maximi ...

  10. CentOS6.5下nginx-1.8.1.tar.gz的单节点搭建(图文详解)

    不多说,直接上干货! [hadoop@djt002 local]$ su root Password: [root@djt002 local]# ll total drwxr-xr-x. root r ...