1. 程式人生 > >使用NSMutableAttributedString 實現富文字(不同顏色字型、下劃線等)

使用NSMutableAttributedString 實現富文字(不同顏色字型、下劃線等)

開發中,常常會有一段文字顯示不同的顏色和字型,或者給某幾個文字加刪除線或下劃線的需求。NSMuttableAttstring(帶屬性的字串),上面的一些需求都可以很簡便的實現。
1.例項化方法有兩種:
使用字串來初始化
①:- (id)initWithString:(NSString *)str;
例如:
NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:@"滿1000減60的優惠券一張"];

②初始化的同時,將其屬性也進行改變
- (id)initWithString:(NSString *)str attributes:(NSDictionary *)attrs;

例子:

NSDictionary *attributeDict = [NSDictionary dictionaryWithObjectsAndKeys:
                                    [UIFont systemFontOfSize:15.0],NSFontAttributeName,
                                    [UIColor redColor],NSForegroundColorAttributeName,nil];
NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:@"滿1000減60的優惠券一張"
attributes:attributeDict];

使用方法①進行初始化時,初始化完後要再利用相關的方法進行屬性的設定,常使用的方法如下:

為某一範圍內文字設定多個屬性
- (void)setAttributes:(NSDictionary *)attrs range:(NSRange)range;
為某一範圍內文字新增某個屬性
- (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)range;
為某一範圍內文字新增多個屬性
- (void)addAttributes:(NSDictionary *)attrs range:(NSRange
)
range; 移除某範圍內的某個屬性 - (void)removeAttribute:(NSString *)name range:(NSRange)range;

上面的方法中(NSString *)name 處,需要新增的是要設定的文字的屬性,常見的屬性有:

NSFontAttributeName //字型

NSParagraphStyleAttributeName //段落格式

NSForegroundColorAttributeName //字型顏色

NSBackgroundColorAttributeName //背景顏色 

NSLigatureAttributeName 連體屬性,取值為NSNumber 物件(整數),0 表示沒有連體字元,1 表示使用預設的連體字元

NSStrikethroughStyleAttributeName//刪除線格式 取值為 NSNumber 物件(整數),列舉常量 NSUnderlineStyle中的值
(// NSUnderlineStyleNone 不設定刪除線
// NSUnderlineStyleSingle 設定刪除線為細單實線
// NSUnderlineStyleThick 設定刪除線為粗單實線
// NSUnderlineStyleDouble 設定刪除線為細雙實線)

NSUnderlineStyleAttributeName  //下劃線  

NSStrokeColorAttributeName//刪除線顏色       

NSStrokeWidthAttributeName//刪除線寬度  取值為 NSNumber 物件(整數),負值填充效果,正值中空效果

NSShadowAttributeName //設定陰影屬性,取值為NSShadow 物件

NSTextEffectAttributeName //設定文字特殊效果,取值為 NSString 物件,(圖版印刷效果)

NSBaselineOffsetAttributeName //設定基線偏移值,取值為 NSNumber (float),正值上偏,負值下偏

NSObliquenessAttributeName //字形傾斜度,取值為 NSNumber (float),正值右傾,負值左傾

NSExpansionAttributeName //文字橫向拉伸屬性,取值為 NSNumber (float),正值橫向拉伸文字,負值橫向壓縮文字

NSWritingDirectionAttributeName //文字書寫方向

NSVerticalGlyphFormAttributeName //文字排版方向,取值為 NSNumber 物件(整數),0 表示橫排文字,1 表示豎排文字(目前iOS總是橫排文字iOS7)

NSLinkAttributeName //設定連結屬性,點選後呼叫瀏覽器開啟指定URL地址  取值為NSURL,如: [NSURL URLWithString:@"http://www.baidu.com"]

NSAttachmentAttributeName //文字附件,取值為NSTextAttachment物件,常用於文字圖片混排

而方法中(NSDictionary *)attrs處需要新增是由上面的屬性組成的字典。
使用例項:
1.用初始化方法①進行設定
利用Label的attributedText屬性來設定:程式碼如下:

 UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(0, 100, 320, 30)];  
    [self.view addSubview:lable];

    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"滿減 滿1000減60的優惠券一張"];
    [str addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0,2)];

    [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Bold" size:30.0] range:NSMakeRange(3, 6)];
    lable.attributedText = str;

效果如圖:
這裡寫圖片描述
方法②進行初始化:

 UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(0, 100, 320, 30)];
    [self.view addSubview:lable];
NSDictionary *attributeDict = [NSDictionary dictionaryWithObjectsAndKeys:
                                   [UIFont systemFontOfSize:15.0],NSFontAttributeName,
                                   [UIColor redColor],NSForegroundColorAttributeName,nil];
    NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:@"滿減 滿1000減60的優惠券一張" attributes:attributeDict];
    lable.attributedText = AttributedStr;

效果如圖:
這裡寫圖片描述

下面列舉幾種例子:
帶有刪除線的


    UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(0, 100, 320, 30)];

    [self.view addSubview:lable];

    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"滿減 滿1000減60的優惠券一張"];

//NSStrikethroughStyleAttributeName:為文字新增刪除線。必須設定NSNumber物件為Value
    [str addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInt:NSUnderlineStyleSingle] range:NSMakeRange(0,2)];

    //NSFontAttributeName 字型
    [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Bold" size:30.0] range:NSMakeRange(3, 6)];
    lable.attributedText = str;

效果圖:
這裡寫圖片描述