您的位置:首页 > 移动开发 > IOS开发

iOS在label中显示不同颜色的字

2017-03-29 17:53 417 查看
UILabel *Label = [[UILabel alloc] initWithFrame:CGRectMake(20, 300, 300, 30)];

    

    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"set
background color with button"];

    [string addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0,2)];

    [string addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(6,12)];

    [string addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(19,6)];

    [string addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial-BoldItalicMT" size:17.0] range:NSMakeRange(0, 5)];

    [string addAttribute:NSFontAttributeName value:[UIFontfontWithName:@"HelveticaNeue-Bold" size:17.0] range:NSMakeRange(6, 12)];

    [string addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Courier-BoldOblique" size:17.0] range:NSMakeRange(19, 6)];

    Label.attributedText = string;

    [self.view addSubview:Label];

   效果图 :  set background
color with button

原文地址:http://blog.csdn.net/sevenquan/article/details/50435373
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  label