iOS - NSAttributedString字符串属性详解

iOS开发过程中相信大家经常遇到当需要给字体,颜色,下划线等属性的时候参数是一个NSDictionary 字典

但是字典里面到底有哪些键值对了

我们把常用的总结一下


首先我们创建一个最简单的,设置一下字体和大小

我们使用是一个NSString 的方法

- (void)drawInRect:(CGRect)rect withAttributes:(NSDictionary *)attrs

来将一个字符串打印到view上
  1. -(void)drawRect:(CGRect)rect  
  2. {  
  3.     self.backgroundColor=[UIColor whiteColor];  
  4.     NSString *attrString =@"hello word";  
  5.       
  6.     NSDictionary* attrs =@{NSFontAttributeName:[UIFont fontWithName:@"AmericanTypewriter" size:30]  
  7.                            }; //在词典中加入文本的字体 大小  
  8.       
  9.     [attrString drawInRect:CGRectMake(20,120,320,200)withAttributes:attrs];  
  10. }  


置字体颜色


  1. NSDictionary* attrs =@{NSFontAttributeName:[UIFont fontWithName:@"AmericanTypewriter" size:30],//文本的颜色 字体 大小  
  2.                        NSForegroundColorAttributeName:[UIColor redColor]//文字颜色  
  3.                        };  




效果

二, NSParagraphStyleAttributeName

段落格式

  1. -(void)drawRect:(CGRect)rect  
  2. {  
  3.     NSString *attrString =@"hello word";  
  4.       
  5.     NSMutableParagraphStyle *paragraph=[[NSMutableParagraphStyle alloc]init];  
  6.     paragraph.alignment=NSTextAlignmentCenter;//居中  
  7.       
  8.     NSDictionary* attrs =@{NSFontAttributeName:[UIFont fontWithName:@"AmericanTypewriter" size:30],//文本的颜色 字体 大小  
  9.                            NSForegroundColorAttributeName:[UIColor redColor],//文字颜色  
  10.                            NSParagraphStyleAttributeName:paragraph,//段落格式  
  11.                            };  
  12.       
  13.     [attrString drawInRect:CGRectMake(20,120,320,200)withAttributes:attrs];  
  14. }  




效果 :(居中)

三,NSBackgroundColorAttributeName

背景色


  1. NSDictionary* attrs =@{NSFontAttributeName:[UIFont fontWithName:@"AmericanTypewriter" size:30],//文本的颜色 字体 大小  
  2.                           NSForegroundColorAttributeName:[UIColor redColor],//文字颜色  
  3.                           NSParagraphStyleAttributeName:paragraph,//段落格式  
  4.                           NSBackgroundColorAttributeName:[UIColor blueColor],//背景色  
  5.                           };  



效果:




四, NSStrokeColorAttributeName

设置描边颜色,需要和NSStrokeWidthAttributeName 一起使用

  1. NSDictionary* attrs =@{NSFontAttributeName:[UIFont fontWithName:@"AmericanTypewriter" size:30],//文本的颜色 字体 大小  
  2.                        NSForegroundColorAttributeName:[UIColor redColor],//文字颜色  
  3.                        NSParagraphStyleAttributeName:paragraph,//段落格式  
  4.                        //NSBackgroundColorAttributeName:[UIColor blueColor],//背景色  
  5.                        NSStrokeWidthAttributeName:@3//描边宽度  
  6.                        NSStrokeColorAttributeName:[UIColor greenColor],//设置 描边颜色,和NSStrokeWidthAttributeName配合使用,设置了这个NSForegroundColorAttributeName就失效了  
  7.                        };  



效果:

五, NSStrikethroughStyleAttributeName

删除线

  1. NSDictionary* attrs =@{NSFontAttributeName:[UIFont fontWithName:@"AmericanTypewriter" size:30],//文本的颜色 字体 大小  
  2.                        NSForegroundColorAttributeName:[UIColor redColor],//文字颜色  
  3.                        NSParagraphStyleAttributeName:paragraph,//段落格式  
  4.                          NSBackgroundColorAttributeName:[UIColor blueColor],//背景色  
  5.                        NSStrokeWidthAttributeName:@3//描边宽度  
  6.                        NSStrokeColorAttributeName:[UIColor greenColor],//设置 描边颜色,和NSStrokeWidthAttributeName配合使用,设置了这个NSForegroundColorAttributeName就失效了  
  7.                       
  8.                        NSStrikethroughStyleAttributeName:@1,//删除线,数字代表线条宽度  
  9.                        };  



效果:


六, NSUnderlineStyleAttributeName

下划线

  1.  NSDictionary* attrs =@{NSFontAttributeName:[UIFont fontWithName:@"AmericanTypewriter" size:30],//文本的颜色 字体 大小  
  2.                            NSForegroundColorAttributeName:[UIColor redColor],//文字颜色  
  3.                            NSParagraphStyleAttributeName:paragraph,//段落格式  
  4. //                           NSBackgroundColorAttributeName:[UIColor blueColor],//背景色  
  5.                            NSStrokeWidthAttributeName:@3//描边宽度  
  6.                            NSStrokeColorAttributeName:[UIColor greenColor],//设置 描边颜色,和NSStrokeWidthAttributeName配合使用,设置了这个NSForegroundColorAttributeName就失效了  
  7.                           
  8. //                           NSStrikethroughStyleAttributeName:@1,//删除线,数字代表线条宽度  
  9.                            NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle),//下划线,值为一个枚举类型,大家可以分别试试  
  10.                            };  


效果:

七, NSShadowAttributeName

设置阴影,他的对象是一个NSShadow的对象


  1.     NSDictionary* attrs =@{NSFontAttributeName:[UIFont fontWithName:@"AmericanTypewriter" size:30],//文本的颜色 字体 大小  
  2.                            NSForegroundColorAttributeName:[UIColor redColor],//文字颜色  
  3.                            NSParagraphStyleAttributeName:paragraph,//段落格式  
  4. //                           NSBackgroundColorAttributeName:[UIColor blueColor],//背景色  
  5.                            NSStrokeWidthAttributeName:@3//描边宽度  
  6.                            NSStrokeColorAttributeName:[UIColor greenColor],//设置 描边颜色,和NSStrokeWidthAttributeName配合使用,设置了这个NSForegroundColorAttributeName就失效了  
  7.                           
  8. //                           NSStrikethroughStyleAttributeName:@1,//删除线,数字代表线条宽度  
  9.                            NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle),//下划线,值为一个枚举类型,大家可以分别试试  
  10.                            NSShadowAttributeName:shadow,//设置阴影,复制为一个NSShadow 的对象  
  11.                              
  12.                            };  


NSShadow
  1. NSShadow *shadow=[[NSShadow alloc]init];  
  2. shadow.shadowBlurRadius=5;//阴影的模糊程度  
  3. shadow.shadowColor=[UIColor blueColor];//阴影颜色  
  4. shadow.shadowOffset=CGSizeMake(66);//阴影相对原来的偏移  




效果:


八, NSObliquenessAttributeName

倾斜

  1.     NSDictionary* attrs =@{NSFontAttributeName:[UIFont fontWithName:@"AmericanTypewriter" size:30],//文本的颜色 字体 大小  
  2.                            NSForegroundColorAttributeName:[UIColor redColor],//文字颜色  
  3.                            NSParagraphStyleAttributeName:paragraph,//段落格式  
  4. //                           NSBackgroundColorAttributeName:[UIColor blueColor],//背景色  
  5.                            NSStrokeWidthAttributeName:@3//描边宽度  
  6.                            NSStrokeColorAttributeName:[UIColor greenColor],//设置 描边颜色,和NSStrokeWidthAttributeName配合使用,设置了这个NSForegroundColorAttributeName就失效了  
  7.                           
  8. //                           NSStrikethroughStyleAttributeName:@1,//删除线,数字代表线条宽度  
  9.                            NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle),//下划线,值为一个枚举类型,大家可以分别试试  
  10.                            NSShadowAttributeName:shadow,//设置阴影,复制为一个NSShadow 的对象  
  11.                            NSObliquenessAttributeName:@1//倾斜程度  
  12.                            };  



效果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值