美文网首页控件类
ios button和label的文字颜色不同

ios button和label的文字颜色不同

作者: fulen | 来源:发表于2017-05-18 19:19 被阅读17次

推荐使用富文本,比较快,先说button的title文字颜色,如下图所示


Snip20170518_5.png
UIButton *loginRegisteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    loginRegisteBtn.frame = CGRectMake(20, 100, 300, 50);
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"还没有联盟账号?点击注册"];
    [str addAttribute:NSForegroundColorAttributeName value:[UIColor grayColor] range:NSMakeRange(0,6)];
    [str addAttribute:NSForegroundColorAttributeName value:[UIColor yellowColor] range:NSMakeRange(7,3)];
    [loginRegisteBtn setAttributedTitle:str forState:UIControlStateNormal];
    [self.view addSubview:loginRegisteBtn];

label 中文字的颜色

NSMutableAttributedString *companyStr = [[NSMutableAttributedString alloc] initWithString:string];
    NSRange range1 = [[companyStr string]rangeOfString:redString];
    [companyStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range1];
applyLabel.attributedText = companyStr;

相关文章

网友评论

    本文标题:ios button和label的文字颜色不同

    本文链接:https://www.haomeiwen.com/subject/gvxexxtx.html