简体   繁体   English

更改iOS中按钮文本的颜色(objective-c)

[英]Changing the color of a buttons text in iOS (objective-c)

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. 我还需要按钮文本的前9个字母(userName的长度)为蓝色,而不是所有文本。 How do I do this? 我该怎么做呢?

Example: 例:
在此输入图像描述

This above image shows "@LisaLisa is following you". 上图显示“@LisaLisa跟着你”。 This is the buttons text. 这是按钮文字。 How do I make just "@LisaLisa" to be blue, with "is following you" staying black? 我如何让“@LisaLisa”成为蓝色,“跟着你”保持黑色?

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];
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. 我还没能测试安德烈的答案。

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"). 因为我们只需要在UserName(“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;
}

Hope, This is what you're looking for. 希望,这就是你要找的东西。 Any concern get back to me. 任何关注都会回复给我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM