概述

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. maven 打包jar && lib

    一.springboot 打包成jar 1.pom.xml <build> <!-- jar的名称--> <finalName>shiro</finalNam ...

  2. PyQt5信号-槽机制

    signal -> emit -> slot signal.connect(slot) signal.disconnect(slot) 信号 (singal) 可以连接无数多个槽 (slo ...

  3. C - Game With Sticks

    Problem description After winning gold and silver in IOI 2014, Akshat and Malvika want to have some ...

  4. (转)vue router 如何使用params query传参,以及有什么区别

    写在前面: 传参是前端经常需要用的一个操作,很多场景都会需要用到上个页面的参数,本文将会详细介绍vue router 是如何进行传参的,以及一些小细节问题.有需要的朋友可以做一下参考,喜欢的可以点波赞 ...

  5. 决策实验(2)分水岭&哄骗实验

    转载请注明http://www.cnblogs.com/igoslly/p/6824544.html 史密斯实验II PART I 分水岭实验 两种选项 A 50%没钱,50% 45元 B 获取固定数 ...

  6. 读《我是一只 IT 小小鸟》

    读<我是一只 IT 小小鸟> 作为一个一向看重节操的体面人,即使面临许多 DDL 包括期中考试,在忙乱不堪的时候我也断不断告诫自己,不能迫于课程要求仅为了写出一篇笔记而去读书,以后更是如此 ...

  7. DeepMind用ReinforcementLearning玩游戏

    原文 :  http://dataunion.org/?p=639 1.引言 说到机器学习最酷的分支,非Deep learning和Reinforcement learning莫属(以下分别简称DL和 ...

  8. swift 类型备份

    Swift语法3.03(类型Types) https://www.jianshu.com/p/839f9bc4b9a3 https://developer.apple.com/library/cont ...

  9. concurrently - npm 同时运行前端和后台服务

    项目基于vue(前端)+node(后台),需要启动两个服务 0.文件夹结构及package.json内容: 1.客户端 npm run dev 2.服务器 cd server npm run serv ...

  10. 05 Django与Ajax

      一.Ajax简介 AJAX(Asynchronous Javascript And XML)翻译成中文就是“异步Javascript和XML”.即使用Javascript语言与服务器进行异步交互, ...