美文网首页
改变一个label上字体的不同颜色 改变一个button上字体

改变一个label上字体的不同颜色 改变一个button上字体

作者: coderJerry01 | 来源:发表于2016-12-17 16:15 被阅读64次

//改变相应字符串的颜色

  • (void)ChangeTextColorByLabel:(UILabel *)label
    font:(CGFloat)font
    model:(NSString *)text{
    //label 需要操作的Label
    //font 该字符的字号
    NSMutableAttributedString *noteString = [[NSMutableAttributedString alloc] initWithString:label.text];
    NSRange stringRange = NSMakeRange(3, text.length);
    //该字符串的位置
    [noteString addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:font], NSForegroundColorAttributeName:[UIColor blackColor]} range:stringRange];
    [label setAttributedText: noteString];
    }
    //button

  • (void)rcs_changButtonTitleColorAndFontWithText:(NSString *)titleText
    button:(UIButton *)button{

    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@",titleText]];
    [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial-BoldItalicMT" size:18.0] range:NSMakeRange(0,1)];
    [str addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(0,1)];
    //
    [button setAttributedTitle:str forState:UIControlStateNormal];
    }

相关文章