iOS - NSAttributedString字符串属性详解
来源:互联网 发布:有利网 知乎 编辑:程序博客网 时间:2024/06/03 22:47
iOS开发过程中相信大家经常遇到当需要给字体,颜色,下划线等属性的时候参数是一个NSDictionary 字典
但是字典里面到底有哪些键值对了
我们把常用的总结一下
首先我们创建一个最简单的,设置一下字体和大小
我们使用是一个NSString 的方法
- (void)drawInRect:(CGRect)rect withAttributes:(NSDictionary *)attrs
来将一个字符串打印到view上
- -(void)drawRect:(CGRect)rect
- {
- self.backgroundColor=[UIColor whiteColor];
- NSString *attrString =@"hello word";
- NSDictionary* attrs =@{NSFontAttributeName:[UIFont fontWithName:@"AmericanTypewriter" size:30]
- }; //在词典中加入文本的字体 大小
- [attrString drawInRect:CGRectMake(20,120,320,200)withAttributes:attrs];
- }
置字体颜色
- NSDictionary* attrs =@{NSFontAttributeName:[UIFont fontWithName:@"AmericanTypewriter" size:30],//文本的颜色 字体 大小
- NSForegroundColorAttributeName:[UIColor redColor]//文字颜色
- };
二,NSParagraphStyleAttributeName效果
三,NSBackgroundColorAttributeName段落格式
- -(void)drawRect:(CGRect)rect
- {
- NSString *attrString =@"hello word";
- NSMutableParagraphStyle *paragraph=[[NSMutableParagraphStyle alloc]init];
- paragraph.alignment=NSTextAlignmentCenter;//居中
- NSDictionary* attrs =@{NSFontAttributeName:[UIFont fontWithName:@"AmericanTypewriter" size:30],//文本的颜色 字体 大小
- NSForegroundColorAttributeName:[UIColor redColor],//文字颜色
- NSParagraphStyleAttributeName:paragraph,//段落格式
- };
- [attrString drawInRect:CGRectMake(20,120,320,200)withAttributes:attrs];
- }
效果 :(居中)
四,NSStrokeColorAttributeName背景色
- NSDictionary* attrs =@{NSFontAttributeName:[UIFont fontWithName:@"AmericanTypewriter" size:30],//文本的颜色 字体 大小
- NSForegroundColorAttributeName:[UIColor redColor],//文字颜色
- NSParagraphStyleAttributeName:paragraph,//段落格式
- NSBackgroundColorAttributeName:[UIColor blueColor],//背景色
- };
效果:
五,NSStrikethroughStyleAttributeName设置描边颜色,需要和NSStrokeWidthAttributeName 一起使用
- NSDictionary* attrs =@{NSFontAttributeName:[UIFont fontWithName:@"AmericanTypewriter" size:30],//文本的颜色 字体 大小
- NSForegroundColorAttributeName:[UIColor redColor],//文字颜色
- NSParagraphStyleAttributeName:paragraph,//段落格式
- //NSBackgroundColorAttributeName:[UIColor blueColor],//背景色
- NSStrokeWidthAttributeName:@3, //描边宽度
- NSStrokeColorAttributeName:[UIColor greenColor],//设置 描边颜色,和NSStrokeWidthAttributeName配合使用,设置了这个NSForegroundColorAttributeName就失效了
- };
效果:
六,NSUnderlineStyleAttributeName删除线
- NSDictionary* attrs =@{NSFontAttributeName:[UIFont fontWithName:@"AmericanTypewriter" size:30],//文本的颜色 字体 大小
- NSForegroundColorAttributeName:[UIColor redColor],//文字颜色
- NSParagraphStyleAttributeName:paragraph,//段落格式
- NSBackgroundColorAttributeName:[UIColor blueColor],//背景色
- NSStrokeWidthAttributeName:@3, //描边宽度
- NSStrokeColorAttributeName:[UIColor greenColor],//设置 描边颜色,和NSStrokeWidthAttributeName配合使用,设置了这个NSForegroundColorAttributeName就失效了
- NSStrikethroughStyleAttributeName:@1,//删除线,数字代表线条宽度
- };
效果:
七,NSShadowAttributeName下划线
- NSDictionary* attrs =@{NSFontAttributeName:[UIFont fontWithName:@"AmericanTypewriter" size:30],//文本的颜色 字体 大小
- NSForegroundColorAttributeName:[UIColor redColor],//文字颜色
- NSParagraphStyleAttributeName:paragraph,//段落格式
- // NSBackgroundColorAttributeName:[UIColor blueColor],//背景色
- NSStrokeWidthAttributeName:@3, //描边宽度
- NSStrokeColorAttributeName:[UIColor greenColor],//设置 描边颜色,和NSStrokeWidthAttributeName配合使用,设置了这个NSForegroundColorAttributeName就失效了
- // NSStrikethroughStyleAttributeName:@1,//删除线,数字代表线条宽度
- NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle),//下划线,值为一个枚举类型,大家可以分别试试
- };
效果:
八,NSObliquenessAttributeName设置阴影,他的对象是一个NSShadow的对象
- NSDictionary* attrs =@{NSFontAttributeName:[UIFont fontWithName:@"AmericanTypewriter" size:30],//文本的颜色 字体 大小
- NSForegroundColorAttributeName:[UIColor redColor],//文字颜色
- NSParagraphStyleAttributeName:paragraph,//段落格式
- // NSBackgroundColorAttributeName:[UIColor blueColor],//背景色
- NSStrokeWidthAttributeName:@3, //描边宽度
- NSStrokeColorAttributeName:[UIColor greenColor],//设置 描边颜色,和NSStrokeWidthAttributeName配合使用,设置了这个NSForegroundColorAttributeName就失效了
- // NSStrikethroughStyleAttributeName:@1,//删除线,数字代表线条宽度
- NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle),//下划线,值为一个枚举类型,大家可以分别试试
- NSShadowAttributeName:shadow,//设置阴影,复制为一个NSShadow 的对象
- };
NSShadow
- NSShadow *shadow=[[NSShadow alloc]init];
- shadow.shadowBlurRadius=5;//阴影的模糊程度
- shadow.shadowColor=[UIColor blueColor];//阴影颜色
- shadow.shadowOffset=CGSizeMake(6, 6);//阴影相对原来的偏移
效果:
倾斜
- NSDictionary* attrs =@{NSFontAttributeName:[UIFont fontWithName:@"AmericanTypewriter" size:30],//文本的颜色 字体 大小
- NSForegroundColorAttributeName:[UIColor redColor],//文字颜色
- NSParagraphStyleAttributeName:paragraph,//段落格式
- // NSBackgroundColorAttributeName:[UIColor blueColor],//背景色
- NSStrokeWidthAttributeName:@3, //描边宽度
- NSStrokeColorAttributeName:[UIColor greenColor],//设置 描边颜色,和NSStrokeWidthAttributeName配合使用,设置了这个NSForegroundColorAttributeName就失效了
- // NSStrikethroughStyleAttributeName:@1,//删除线,数字代表线条宽度
- NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle),//下划线,值为一个枚举类型,大家可以分别试试
- NSShadowAttributeName:shadow,//设置阴影,复制为一个NSShadow 的对象
- NSObliquenessAttributeName:@1//倾斜程度
- };
效果:
0 0
- iOS - NSAttributedString字符串属性详解
- iOS-属性字符串NSAttributedString
- NSAttributedString 详解(属性字符串)
- iOS NSAttributedString(属性字符串)
- NSAttributedString字符串属性
- iOS-NSAttributedString属性关键字
- NSAttributedString属性详解
- iOS NSAttributedString所有文本属性详解(多图)
- iOS NSAttributedString所有文本属性详解(多图)
- ios NSAttributedString 详解
- iOS NSAttributedString 详解
- 属性字符串NSAttributedString简易教程
- Objective-C属性字符串NSAttributedString
- iOS 字符属性NSAttributedString描述
- iOS 字符属性NSAttributedString描述
- iOS 字符属性NSAttributedString描述
- iOS 字符属性NSAttributedString描述
- iOS 字符属性NSAttributedString描述
- RocketMQ是什么(二)
- 前话
- C++中L和_T()之区别www.cnblogs.com/lidabo/archive/2013/03/17/2964961.html
- LeetCode OJ-27-Remove Element
- 使用NPOI创建当天Excel文件代码
- iOS - NSAttributedString字符串属性详解
- Android_android studio导入os库
- ACCPJAVAS1测试第三题
- JAVA 微信小程序开发资源总结(JavaScript)
- mysql 远程数据库备份到本地 + mysql忘记密码
- 浅谈mvc
- 新的开始
- ReentrantReadWriteLock
- 12cR1出现的 Adaptive Plans新特性的个人理解