iOS知识点iOS 开发 首页投稿(暂停使用,暂停投稿)

UILabel样式自定义(同一个Label展示不同颜色,字体)

2016-05-11  本文已影响4605人  余荣景
- (void)smallRMBByLabel:(UILabel *)label font:(int)font
{
  //label  需要操作的Label
  //font   该字符的字号
  NSMutableAttributedString *noteString = [[NSMutableAttributedString alloc] initWithString:label.text];
  NSRange stringRange = NSMakeRange(0, 1); //该字符串的位置
  [noteString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:font] range:stringRange];
  [label setAttributedText: noteString];
}
部分字符颜色不一

不啰嗦,直接上干货

  NSMutableAttributedString *noteStr = [[NSMutableAttributedString alloc] initWithString:@"照片中信息真实有效且清晰可见,包括手持证件人的五官、身份证上的所有信息(请看三遍再上传图片噢)"];
  NSRange redRange = NSMakeRange([[noteStr string] rangeOfString:@"(请看三遍再上传图片噢)"].location, [[noteStr string] rangeOfString:@"(请看三遍再上传图片噢)"].length);
  //需要设置的位置
  [noteStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:redRange]; 
  //设置颜色
  [label setAttributedText:noteStr];

搞定,收工。

上一篇 下一篇

猜你喜欢

热点阅读