原文:http://blog.csdn.net/lwjok2007/article/details/47184911
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,
-
- NSStrokeWidthAttributeName:@3,
- NSStrokeColorAttributeName:[UIColor greenColor],
- };
效果:
五,
NSStrikethroughStyleAttributeName
删除线
- NSDictionary* attrs =@{NSFontAttributeName:[UIFont fontWithName:@"AmericanTypewriter" size:30],
- NSForegroundColorAttributeName:[UIColor redColor],
- NSParagraphStyleAttributeName:paragraph,
- NSBackgroundColorAttributeName:[UIColor blueColor],
- NSStrokeWidthAttributeName:@3,
- NSStrokeColorAttributeName:[UIColor greenColor],
-
- NSStrikethroughStyleAttributeName:@1,
- };
效果:
六,
NSUnderlineStyleAttributeName
下划线
- NSDictionary* attrs =@{NSFontAttributeName:[UIFont fontWithName:@"AmericanTypewriter" size:30],
- NSForegroundColorAttributeName:[UIColor redColor],
- NSParagraphStyleAttributeName:paragraph,
-
- NSStrokeWidthAttributeName:@3,
- NSStrokeColorAttributeName:[UIColor greenColor],
-
-
- NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle),
- };
效果:
七,
NSShadowAttributeName
设置阴影,他的对象是一个NSShadow的对象
- NSDictionary* attrs =@{NSFontAttributeName:[UIFont fontWithName:@"AmericanTypewriter" size:30],
- NSForegroundColorAttributeName:[UIColor redColor],
- NSParagraphStyleAttributeName:paragraph,
-
- NSStrokeWidthAttributeName:@3,
- NSStrokeColorAttributeName:[UIColor greenColor],
-
-
- NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle),
- NSShadowAttributeName:shadow,
-
- };
NSShadow
- NSShadow *shadow=[[NSShadow alloc]init];
- shadow.shadowBlurRadius=5;
- shadow.shadowColor=[UIColor blueColor];
- shadow.shadowOffset=CGSizeMake(6, 6);
效果:
八,
NSObliquenessAttributeName
倾斜
- NSDictionary* attrs =@{NSFontAttributeName:[UIFont fontWithName:@"AmericanTypewriter" size:30],
- NSForegroundColorAttributeName:[UIColor redColor],
- NSParagraphStyleAttributeName:paragraph,
-
- NSStrokeWidthAttributeName:@3,
- NSStrokeColorAttributeName:[UIColor greenColor],
-
-
- NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle),
- NSShadowAttributeName:shadow,
- NSObliquenessAttributeName:@1
- };
效果: