您的位置:首页 > 移动开发 > IOS开发

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;
}


设置段落前



设置段落后

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: