給UIButton增加下劃線,改變某個(gè)范圍文字的顏色

通過NSMutableAttributedString修改某個(gè)范圍內(nèi)的屬性
在iOS開發(fā)中,常常會(huì)有一段文字顯示不同的顏色和字體,或者給某幾個(gè)文字加刪除線或下劃線的需求。
自己在網(wǎng)上找了一下,看到部分大神使用NSMutableAttributedString來修改,我也就去了解了一下,下面是部分屬性(網(wǎng)上找到的)
1.實(shí)例化方法和使用方法

實(shí)例化方法:

//使用字符串初始化
- (id)initWithString:(NSString *)str;

例:

NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:@"還沒有賬號(hào),去注冊(cè)"];
- (id)initWithString:(NSString *)str attributes:(NSDictionary *)attrs;

字典中存放一些屬性名和屬性值,如:

NSDictionary *attributeDict = [NSDictionary dictionaryWithObjectsAndKeys:
                                    [UIFont systemFontOfSize:15.0],NSFontAttributeName,
                                    [UIColor redColor],NSForegroundColorAttributeName,
                                    NSUnderlineStyleAttributeName,NSUnderlineStyleSingle,nil];
NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:@"還沒有賬號(hào),去注冊(cè)" attributes:attributeDict];
- (id)initWithAttributedString:(NSAttributedString *)attester;
//使用NSAttributedString初始化,跟NSMutableString,NSString類似

使用方法:

//為某一范圍內(nèi)文字設(shè)置多個(gè)屬性
- (void)setAttributes:(NSDictionary *)attrs range:(NSRange)range;

//為某一范圍內(nèi)文字添加某個(gè)屬性
- (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)range;

//為某一范圍內(nèi)文字添加多個(gè)屬性
- (void)addAttributes:(NSDictionary *)attrs range:(NSRange)range;

//移除某范圍內(nèi)的某個(gè)屬性
- (void)removeAttribute:(NSString *)name range:(NSRange)range;

2.常見的屬性及說明

NSFontAttributeName             字體
NSParagraphStyleAttributeName       段落格式    
NSForegroundColorAttributeName      字體顏色
NSBackgroundColorAttributeName      背景顏色
NSStrikethroughStyleAttributeName   刪除線格式
NSUnderlineStyleAttributeName       下劃線格式
NSStrokeColorAttributeName          刪除線顏色
NSStrokeWidthAttributeName      刪除線寬度
NSShadowAttributeName           陰影

更多方法和屬性說明詳見蘋果官方說明文檔:
蘋果官方文檔

3.實(shí)例

   /**
     *  通過 NSMutableAttributedString 實(shí)現(xiàn)富文本
     */
    NSString * oneStr = @"還沒有賬號(hào),去注冊(cè)";
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@",oneStr]];
    //修改某個(gè)范圍內(nèi)的字體大小
    [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial-BoldItalicMT" size:16.0] range:NSMakeRange(7,2)];
    //修改某個(gè)范圍內(nèi)字的顏色
    [str addAttribute:NSForegroundColorAttributeName value:GLColor(62, 190, 219, 1)  range:NSMakeRange(7,2)];
    //在某個(gè)范圍內(nèi)增加下劃線
    [str addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(0, [str length])];

 UIButton * registerUsers = [UIButton buttonWithType:UIButtonTypeCustom];
    registerUsers.frame = CGRectMake(self.view.centerX - 75, SCREENHEIGHT - 64 -50 - 20, 150, 50);
    registerUsers.titleLabel.textAlignment = NSTextAlignmentCenter;
    registerUsers.titleLabel.font = [UIFont systemFontOfSize:12];
    [registerUsers setAttributedTitle:str forState:UIControlStateNormal];
    [registerUsers addTarget:self action:@selector(setregisterUsers:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:registerUsers];

運(yùn)行效果:


實(shí)例運(yùn)行效果.png

這只是簡(jiǎn)單的紀(jì)錄一下,以后會(huì)繼續(xù)填充(對(duì)了,暫時(shí)發(fā)現(xiàn)可以用于 UITextField 和 UILabel 上)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容