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

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

  • (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];
    }

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1、设置UILabel行间距 NSMutableAttributedString* attrString = [[...
    十年一品温如言1008阅读 1,694评论 0 3
  • 1、设置UILabel行间距 NSMutableAttributedString* attrString = [[...
    FF_911阅读 1,404评论 0 3
  • 字体属性设置示例:if(color ==nil) {color = [NSColor redColor];}NSF...
    袏扌戒指阅读 8,446评论 0 2
  • Android开发中我们打交道最多的Activity,这里说下对Activity4种启动模式的简单总结 Stand...
    伟豪_5cd8阅读 147评论 0 0
  • “纽约时间比加州时间早三个小时, 但加州时间并没有变慢。 有人22岁就毕业了 但等了五年才找到好的工作! 有人25...
    YU木阅读 229评论 0 0