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),//底線,值為一個枚舉類型,大家可以分別試試 };
效果:
七,NSShadowAttributeName
設定陰影,他的對象是一個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);//陰影相對原來的位移
效果:
八,NSObliquenessAttributeName
傾斜
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//傾斜程度 };
效果:
這些常用的我們做了整理,還有一些沒做整理。大家有興趣可以研究,歡迎加群和大家討論。