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

  • 相关阅读:
    .net MVC 图片水印,半透明
    提取数据库字段里面的值,并改变+图片懒加载,jquery延迟加载
    sqlalchemy + alembic数据迁移
    fastfdfs搭配nginx
    ubuntu安装fastdfds
    django自定义实现登录验证-更新版
    tornado异步
    django发送邮件的坑
    python3.6 ubuntu部署nginx、 uwsgi、 django
    ubuntu安装python3.6
  • 原文地址:https://www.cnblogs.com/vegas/p/5133204.html
Copyright © 2011-2022 走看看