1

I want to change the color of the text in my button to be blue on load. I also need only the first 9 letters (length of userName) of the buttons text to be blue, not all of the text. How do I do this?

Example:
enter image description here

This above image shows "@LisaLisa is following you". This is the buttons text. How do I make just "@LisaLisa" to be blue, with "is following you" staying black?

Meet Doshi
  • 4,241
  • 10
  • 40
  • 81
Josh O'Connor
  • 4,694
  • 7
  • 54
  • 98
  • 5
    I think this might solve your requirements http://stackoverflow.com/questions/17756067/ios-nsattributedstring-on-uibutton – Piyush Sharma Nov 19 '15 at 20:33

3 Answers3

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];
André Slotta
  • 13,774
  • 2
  • 22
  • 34
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];

This answer worked for me. I wasnt able to test Andre's answers yet.

Borys Verebskyi
  • 4,160
  • 6
  • 28
  • 42
Josh O'Connor
  • 4,694
  • 7
  • 54
  • 98
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];

Following method is used for making label's Dynamic Height & Width. Because we have to set Button's frame only on UserName ("LisaLisa").

//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;
}

Hope, This is what you're looking for. Any concern get back to me.

Meet Doshi
  • 4,241
  • 10
  • 40
  • 81