iOS一个UILabel 显示两种字体和颜色

UILabel infoLabel3;
以空格为截断点。查找空格的位置,然后建立 NSRang。当然也可以自己指定范围。

 
  

        NSRange range;
        range = [infoLabel3.text rangeOfString:@" "];
        if (range.location != NSNotFound) {
            NSLog(@"found at location = %lu, length = %lu",(unsigned long)range.location,(unsigned long)range.length);
            
            NSInteger startIndex = range.location ;
            NSInteger endIndex = infoLabel3.text.length -1;
            range = NSMakeRange(startIndex, endIndex);
            UIFont *labelFont = [UIFont fontWithName:@"HelveticaNeue-Bold" size:15];
            NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"Using NSAttributed String"];
            [str addAttribute:NSForegroundColorAttributeName value:[UIColor lightGrayColor] range:range];
            [str addAttribute:NSFontAttributeName value:labelFont range: range];
            infoLabel3.attributedText = str;
        }else{
            NSLog(@"Not Found");
        }


你可能感兴趣的:(IOS,ios,uilabel,字体)