iOS 文字属性字典
分类:iOS
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
段落格式
-(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];}
效果 :(居中)
三,NSBackgroundColorAttributeName
背景色
NSDictionary* attrs =@{NSFontAttributeName:[UIFont fontWithName:@"AmericanTypewriter" size:30],//文本的颜色 字体 大小NSForegroundColorAttributeName:[UIColor redColor],//文字颜色NSParagraphStyleAttributeName:paragraph,//段落格式NSBackgroundColorAttributeName:[UIColor blueColor],//背景色};
效果:
四,NSStrokeColorAttributeName
设置描边颜色,需要和NSStrokeWidthAttributeName一起使用
NSDictionary* attrs =@{NSFontAttributeName:[UIFont fontWithName:@"AmericanTypewriter" size:30],//文本的颜色 字体 大小NSForegroundColorAttributeName:[UIColor redColor],//文字颜色NSParagraphStyleAttributeName:paragraph,//段落格式//NSBackgroundColorAttributeName:[UIColor blueColor],//背景色NSStrokeWidthAttributeName:@3, //描边宽度NSStrokeColorAttributeName:[UIColor greenColor],//设置 描边颜色,和NSStrokeWidthAttributeName配合使用,设置了这个NSForegroundColorAttributeName就失效了};
效果:
五,NSStrikethroughStyleAttributeName
删除线
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
下划线
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),//下划线,值为一个枚举类型,大家可以分别试试};
效果:
海阔凭鱼跃,天高任鸟飞。我要加油,冲向我的理想。