iOS 文字屬性字典

來源:互聯網
上載者:User

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//傾斜程度                           };


 


效果:

 

 

這些常用的我們做了整理,還有一些沒做整理。大家有興趣可以研究,歡迎加群和大家討論。

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.