zoukankan      html  css  js  c++  java
  • 设置一段文字显示不同的颜色及大小等属性

       实现效果:

    UIButton *valueBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        valueBtn.frame=CGRectMake(100, 100, 200, 40);
        NSMutableAttributedString *str=[[NSMutableAttributedString alloc]initWithString:@"昨日产值: 5L鲜奶"];//有空格
        NSRange range=NSMakeRange(0, [str length]);

    //设置前半部分字体颜色
        [str addAttribute:NSForegroundColorAttributeName value:[UIColor  lightGrayColor] range:NSMakeRange(0, 5)];

    //设置前半部分字体大小
        [str addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14.0f] range:NSMakeRange(0, 5)];

    //设置后半部分字体颜色
        [str addAttribute:NSForegroundColorAttributeName value:[UIColor  colorWithRed:244/255.0f green:93/255.0f blue:97/255.0f alpha:1.0] range:NSMakeRange(5, 5)];

    //设置后半字体大小
        [str addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:18.0f] range:NSMakeRange(5, 5)];

    //添加下划线
        [str addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:range];
        [valueBtn setAttributedTitle:str forState:UIControlStateNormal];
        [self.view addSubview:valueBtn];

    #warning :1.关键字 NSMutableAttributedString

          2.注意 NSMakeRange(a,b)中 ,参数1:是起始点位置下标  参数2:长度

          3.前面属性带AttributeName

  • 相关阅读:
    《你不知道的javascript》读书笔记2
    你不知道的console调试
    《你不知道的javascript》读书笔记1
    使用js做LeetCode
    用装饰器来进行登录验证
    python 解压序列
    pycharm 的live_template的使用
    faker 库的使用
    Python常用内置类和常用内置函数汇总
    迭代器 ,生成器(生成器函数/生成器表达式)
  • 原文地址:https://www.cnblogs.com/vegas/p/5133204.html
Copyright © 2011-2022 走看看