zoukankan      html  css  js  c++  java
  • iOS

    • 在UILabel中的一段文字上添加不同颜色,不同下划线

    注意: 如果同时给一段文字加上颜色与下划线是没有下划线效果的,目前不知为什么. 例如:"hello" 给它同时加上下划线与改变字体颜色则只能改变颜色.

    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"Using NSAttributed String"];
        [str addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0,5)];
        [str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(6,12)];
        [str addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(19,6)];
                
        [str  addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial-BoldItalicMT"size:30.0] range:NSMakeRange(0, 5)];
                
        [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Bold"size:30.0] range:NSMakeRange(6, 12)];
                
        [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Courier-BoldOblique"size:30.0] range:NSMakeRange(19, 6)];
        //加下划线
        [str addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(0, 3)];
        
        UILabel * lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, 320, 50)];
        lbl.userInteractionEnabled = YES;
        [self.view addSubview:lbl];
        lbl.attributedText = str;
        
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickStr)];
        [lbl addGestureRecognizer:tap];
    }
    
    
    
    • 效果图:

    UIButton下划线设置: http://www.cnblogs.com/adampei-bobo/p/6116469.html

  • 相关阅读:
    登入界面的创建
    什么是IO流 以及文件输入输出
    java 的面向对象
    Mac 终端命令大全
    jQuery 的属性
    商城管理系统
    Java IO学习第二天部分详解
    Java IO学习第一天部分详解
    用JAVA描述一个车与修车厂两个事物
    JAVA基础(数组)数组排序和查找数组中是否还有某一个数
  • 原文地址:https://www.cnblogs.com/adampei-bobo/p/6500782.html
Copyright © 2011-2022 走看看