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
- iOS富文本(一)属性化字符串
- 富文本(属性字符串)
- ios富文本属性
- iOS 富文本属性
- 属性字符串(富文本)的使用
- 链式编程,iOS简单封装属性字符串,实现富文本.
- iOS 富文本属性解释
- 富文本带属性的字符串NSMutableAttributedString
- iOS开发富文本NSAttributedString属性
- iOS开发富文本NSAttributedString属性
- iOS笔记 -- 富文本处理属性
- iOS富文本字符串AttributedString详解
- iOS富文本字符串AttributedString详解
- 富文本属性
- 富文本属性2
- Android富文本字符串
- Android富文本字符串
- iOS开发-- 开发细节(富文本)
- android部分目录分析
- oracle课堂收录-函数举例
- Hihocoder #1095 : HIHO Drinking Game (微软笔试题)
- ZwCreateFile打开文件时出现SHARE_VIOLATION的问题的一种情况
- Android JNI实现计算器demo
- iOS富文本(一)属性化字符串
- iOS target
- 音频编码之opus(一)
- java 快速排序
- UNIX Tutorial for Beginners
- 杨氏矩阵及其变形
- asp.net 中把txt文件导入 vs2010的sql2008数据库的一张表中【已测试,可以实现】
- NavigationItem的Title不居中,BackButton修改它得title
- 打字母游戏3