2015-11-19 32 views
1

我想將我按鈕中文本的顏色更改爲加載時藍色。我也只需要按鈕文本的前9個字母(用戶名長度)爲藍色,而不是全部文本。我該怎麼做呢?更改iOS中按鈕文本的顏色(objective-c)

實施例:
enter image description here

這上面的圖片顯示 「@LisaLisa跟隨你」。這是按鈕文字。我如何才能使「@LisaLisa」變成藍色,「隨着你」變黑?

+5

我認爲這可能會解決您的要求http://stackoverflow.com/questions/17756067/ios-nsattributedstring-on-uibutton –

回答

0
UILabel * myLabel = [[UILabel alloc] init];//used for whole string 
myLabel.numberOfLines = 0; 
NSString * myUserName = @"@LisaLisa";//used for userName 

//add button on UserName 
UIButton * muButton = [[UIButton alloc] init]; 
myLabel.text = [NSString stringWithFormat:@"%@", myUserName]; 
myLabel = [self setDynamicLableFrame:myLabel fontSize:fontSize Width:width]; 
//Here width is your Label's Width. Because we have to make sure that our, our label's width is not greater than our device's width and fontSize is label's fontSize 
muButton.frame = myLabel.frame; 

myLabel.text = [NSString stringWithFormat:@"%@ is following you", myUserName]; 
myLabel = [self setDynamicLableFrame:myLabel fontSize:fontSize Width:width];//used for making dynamic height of label 

//For changing color of UserName 
NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithAttributedString: myLabel.attributedText]; 
[text addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0, myUserName.length)]; 
[myLabel setAttributedText: text]; 

以下方法用於製作標籤的動態高度&寬度。因爲我們必須僅在用戶名(「LisaLisa」)上設置Button的框架。

//for setting the dynamic height of labels 
-(UILabel *)setDynamicLableFrame:(UILabel*)myLabel fontSize:(float)size Width:(float)Width 
{ 
    CGSize possibleSize = [myLabel.text sizeWithFont:[UIFont fontWithName:REGULER_FONT size:size] constrainedToSize:CGSizeMake(300, 9999) lineBreakMode:NSLineBreakByWordWrapping]; 

    CGRect newFrame = myLabel.frame; 
    newFrame.size.height = possibleSize.height; 

    if (possibleSize.width < Width) { 
     newFrame.size.width = possibleSize.width; 
    }else{ 
     newFrame.size.width = Width; 
    } 
    myLabel.frame = newFrame; 
    return myLabel; 
} 

希望,這就是你要找的。任何擔心都會回到我身上。

2
UIButton *someButton = [UIButton buttonWithType:UIButtonTypeSystem]; 

NSString *someUsername = @"@LisaLisa"; 
NSString *buttonText = [NSString stringWithFormat:@"%@ is following you.", someUsername]; 
NSRange rangeToHighlight = [buttonText rangeOfString:someUsername]; 

NSDictionary *defaultAttributes = @{NSForegroundColorAttributeName: [UIColor blackColor]}; 
NSDictionary *highlightedAttributes = @{NSForegroundColorAttributeName: [UIColor blueColor]}; 

NSMutableAttributedString *attributedTitle = [[NSMutableAttributedString alloc] initWithString:buttonText attributes:defaultAttributes]; 
[attributedTitle addAttributes:highlightedAttributes range:rangeToHighlight]; 
[someButton setAttributedTitle:attributedTitle forState:UIControlStateNormal]; 
1
NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy]; 
[style setLineBreakMode:NSLineBreakByWordWrapping]; 

UIColor *color1 = [UIColor redColor]; 
UIColor *color2 = [UIColor blueColor]; 
UIFont *font1 = [UIFont fontWithName:@"HelveticaNeue" size:20.0f]; 
UIFont *font2 = [UIFont fontWithName:@"HelveticaNeue-Light" size:20.0f]; 
NSDictionary *dict1 = @{NSFontAttributeName:font1, 
         NSForegroundColorAttributeName:color1 }; // Added line 
NSDictionary *dict2 = @{NSFontAttributeName:font2, 
         NSForegroundColorAttributeName:color2 }; // Added line 

NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] init]; 
[attString appendAttributedString:[[NSAttributedString alloc] initWithString:senderName attributes:dict1]]; 
[attString appendAttributedString:[[NSAttributedString alloc] initWithString:@"posted to" attributes:dict2]]; 
[cell.profileIDButton setAttributedTitle:attString forState:UIControlStateNormal]; 
[[cell.profileIDButton titleLabel] setNumberOfLines:0]; 
[[cell.profileIDButton titleLabel] setLineBreakMode:NSLineBreakByWordWrapping]; 

這個答案爲我工作。我還沒有能夠測試安德烈的答案。