//原始数据字符串 NSString *string = @"我是好人我是好人我是个好人额鹅鹅鹅鹅鹅鹅饿哇哇哇哇哇哇哇"; // 创建可变属性化字符串 NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:string]; //改变字符串当中从第18位置向后的10位数的字体 UIFont *smallFont = [UIFont systemFontOfSize:30]; [attrString addAttribute:NSFontAttributeName value:smallFont range:NSMakeRange(5, 10)]; //改变字符串当中第一个“1”的颜色 UIColor *rcolor = [UIColor redColor]; [attrString addAttribute:NSForegroundColorAttributeName value:rcolor range:[string rangeOfString:@"是"]]; UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 44, 320, 200)]; label.numberOfLines = 0; [self.view addSubview:label]; label.attributedText = attrString;
时间: 2025-01-31 06:46:19