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

  • 相关阅读:
    java jmap,jstat等工具的使用
    jvm 参数配置
    python NameError: name 'false' is not defined
    aiflow Global variable explicit_defaults_for_timestamp needs to be on (1) for mysql
    TX 笔试题总结
    POJ 3140 Contestants Division
    POJ 1018 Communication System
    POJ 3260 The Fewest Coin
    Leetcode: Median of two sorted Array
    基础知识 (二)
  • 原文地址:https://www.cnblogs.com/vegas/p/5133204.html
Copyright © 2011-2022 走看看