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

来源:互联网 发布:360调价软件 编辑:程序博客网 时间:2024/04/29 05:11

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");        }


0 0
原创粉丝点击