iOS富文本(一)属性化字符串

来源:互联网 发布:神仙劫翅膀进阶数据 编辑:程序博客网 时间:2024/04/27 17:06

概述

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

设置段落前

设置段落后

1 0