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

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:[UIFont fontWithName:@"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];

扫描二维码关注公众号,回复: 1678292 查看本文章

 

 

   效果图 :   set background color with button

猜你喜欢

转载自www.cnblogs.com/Free-Thinker/p/9208923.html