iOS 文字屬性,ios文字屬性
- (void)drawInRect:(CGRect)rect withAttributes:(NSDictionary *)attrs
-(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//傾斜程度 };