(zhuan)富文本 Attributes 下划线、删除线等
每次需要特殊設置文本的attribute屬性時,都要查很多資料,其中有些常用的key值有時不會那么準確的記住,所以花點時間整理了一下;
使用下面這首古詩作為例子吧:
朝辭白帝彩云間,
千里江陵一日還。
兩岸猿聲啼不盡,
輕舟已過萬重山。
直接顯示這首古詩的時候,結果是這樣的:
NSString *string = @"朝辭白帝彩云間,\n千里江陵一日還。\n兩岸猿聲啼不盡,\n輕舟已過萬重山"; self.label.text = string;直接顯示
下面,我們就一點點的添加文本的屬性:
首先,創建一個NSMutableAttributedString:
這里的NSRange在下面的設置屬性的時候要用到,就是你需要特殊添加屬性的字符串,當然,這個字符串需要包含在整個字符串string里的;當然,如果你需要為整個字符串添加屬性,就不需要這個屬性了,可以在創建添加屬性:
- (instancetype)initWithString:(NSString *)str attributes:(nullable NSDictionary<NSString *, id> *)attires;1. NSKernAttributeName(字間距)
調整字間距:
[mString addAttribute:NSKernAttributeName value:@10 range:range];這里的value值是NSNumber類型的
設置字間距
2. NSFontAttributeName(字體)
設置字體:
[mString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:22] range:range];這里的value值是UIFont對象
設置字體
3. NSForegroundColorAttributeName(文本顏色)
設置文本顏色:
[mString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range];value:UIColor對象
設置文本顏色
4. NSParagraphStyleAttributeName(段落樣式)
設置段落樣式:
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc]init];style.alignment = NSTextAlignmentCenter;style.lineSpacing = 20;[mString addAttribute:NSParagraphStyleAttributeName value:style range:range];value: NSMutableParagraphStyle對象
設置段落樣式
5. NSBackgroundColorAttributeName(文本背景色)
設置文本背景色:
[mString addAttribute:NSBackgroundColorAttributeName value:[UIColor greenColor] range:range];value:UIColor對象
設置背景色
6. NSStrokeColorAttributeName(描邊顏色)
設置文本描邊顏色,需要和NSStrokeWidthAttributeName設置描邊寬度,這樣就能使文字空心.
NSStrokeWidthAttributeName這個屬性所對應的值是一個 NSNumber 對象(小數)。該值改變描邊寬度(相對于字體size 的百分比)。默認為 0,即不改變。正數只改變描邊寬度。負數同時改變文字的描邊和填充寬度。例如,對于常見的空心字,這個值通常為3.0。
同時設置了空心的兩個屬性,并且NSStrokeWidthAttributeName屬性設置為整數,文字前景色(NSForegroundColorAttributeName)就無效果了
設置空心文本
如果數值設置為負數,就沒有空心字的效果:
[mString addAttribute:NSStrokeColorAttributeName value:[UIColor greenColor] range:range];[mString addAttribute:NSStrokeWidthAttributeName value:@(-3) range:range];設置文本描邊
7. NSStrikethroughStyleAttributeName(刪除線)
添加刪除線
[mString addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlineStyleSingle) range:range];value:線的類型NSUnderlineStyle
typedef NS_ENUM(NSInteger, NSUnderlineStyle) {NSUnderlineStyleNone = 0x00,NSUnderlineStyleSingle = 0x01,NSUnderlineStyleThick NS_ENUM_AVAILABLE(10_0, 7_0) = 0x02,NSUnderlineStyleDouble NS_ENUM_AVAILABLE(10_0, 7_0) = 0x09,NSUnderlinePatternSolid NS_ENUM_AVAILABLE(10_0, 7_0) = 0x0000,NSUnderlinePatternDot NS_ENUM_AVAILABLE(10_0, 7_0) = 0x0100,NSUnderlinePatternDash NS_ENUM_AVAILABLE(10_0, 7_0) = 0x0200,NSUnderlinePatternDashDot NS_ENUM_AVAILABLE(10_0, 7_0) = 0x0300,NSUnderlinePatternDashDotDot NS_ENUM_AVAILABLE(10_0, 7_0) = 0x0400,NSUnderlineByWord NS_ENUM_AVAILABLE(10_0, 7_0) = 0x8000 } NS_ENUM_AVAILABLE(10_0, 6_0);添加刪除線
8. NSUnderlineStyleAttributeName(下劃線)
添加下劃線:
[mString addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:range];value:線的類型NSUnderlineStyle
typedef NS_ENUM(NSInteger, NSUnderlineStyle) {NSUnderlineStyleNone = 0x00,NSUnderlineStyleSingle = 0x01,NSUnderlineStyleThick NS_ENUM_AVAILABLE(10_0, 7_0) = 0x02,NSUnderlineStyleDouble NS_ENUM_AVAILABLE(10_0, 7_0) = 0x09,NSUnderlinePatternSolid NS_ENUM_AVAILABLE(10_0, 7_0) = 0x0000,NSUnderlinePatternDot NS_ENUM_AVAILABLE(10_0, 7_0) = 0x0100,NSUnderlinePatternDash NS_ENUM_AVAILABLE(10_0, 7_0) = 0x0200,NSUnderlinePatternDashDot NS_ENUM_AVAILABLE(10_0, 7_0) = 0x0300,NSUnderlinePatternDashDotDot NS_ENUM_AVAILABLE(10_0, 7_0) = 0x0400,NSUnderlineByWord NS_ENUM_AVAILABLE(10_0, 7_0) = 0x8000 } NS_ENUM_AVAILABLE(10_0, 6_0);添加下劃線
9. NSShadowAttributeName(陰影)
設置陰影
NSShadow *shadow = [[NSShadow alloc]init];shadow.shadowBlurRadius = 5;//設置模糊度shadow.shadowColor = [UIColor blueColor];//設置陰影顏色shadow.shadowOffset = CGSizeMake(1, 3);//設置陰影的偏移量[mString addAttribute:NSShadowAttributeName value:shadow range:range];一般與:
NSVerticalGlyphFormAttributeName,NSObliquenessAttributeName,
NSExpansionAttributeName結合使用
10. NSVerticalGlyphFormAttributeName(排版方式)
文本排版方式
該屬性所對應的值是一個 NSNumber 對象(整數)。
0 表示橫排文本。
1 表示豎排文本。
在 iOS 中,總是使用橫排文本,0 以外的值都未定義,即其他值無效果。
NSShadow *shadow = [[NSShadow alloc]init];shadow.shadowBlurRadius = 5;//設置模糊度shadow.shadowColor = [UIColor blueColor];//設置陰影顏色shadow.shadowOffset = CGSizeMake(1, 3);//設置陰影的偏移量[mString addAttribute:NSShadowAttributeName value:shadow range:range];[mString addAttribute:NSVerticalGlyphFormAttributeName value:@(0) range:range];添加陰影和排版
11. NSObliquenessAttributeName (斜體)
設置文本斜體
NSShadow *shadow = [[NSShadow alloc]init];shadow.shadowBlurRadius = 5;//設置模糊度shadow.shadowColor = [UIColor blueColor];//設置陰影顏色shadow.shadowOffset = CGSizeMake(1, 3);//設置陰影的偏移量[mString addAttribute:NSShadowAttributeName value:shadow range:range];[mString addAttribute:NSObliquenessAttributeName value:@(1) range:range];添加斜體
12. NSExpansionAttributeName(扁平化)
設置文本扁平化
[mString addAttribute:NSExpansionAttributeName value:@(1) range:range];添加扁平化
總結:
以上便是常用的文本設置,以備使用時查找;
如有不正確的地方,還請指正;
下面,就開始為你的文本添加豐富的attributes吧!!!
作者:流火緋瞳
鏈接:http://www.jianshu.com/p/618a147449d7
來源:簡書
著作權歸作者所有。商業轉載請聯系作者獲得授權,非商業轉載請注明出處。
總結
以上是生活随笔為你收集整理的(zhuan)富文本 Attributes 下划线、删除线等的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Linux】制作U-Boot烧写镜像到
- 下一篇: Intel 9代CPU自砍鸡肋:全新酷睿