字符串中设置不同内容的字体和颜色

只针对一处的字符串内容设置字体大小和颜色,可以用UIlabel或UITextView进行设置:


 self.m_editTransSumLabel.attributedText = @"您此笔交易转让价格由1000元修改为500元";

 NSString*tempStr =self.m_editTransSumLabel.text;

  NSMutableAttributedString*strAtt = [[NSMutableAttributedString alloc] initWithString:tempStr];

  [strAtt  addAttribute:NSForegroundColorAttributeName value:WJRGBAColor(245, 81, 81, 1) range:NSMakeRange(0, [strAtt length])];

NSRangemarkRange1 = [tempStr rangeOfString:@"1000元"];

 [strAtt  addAttribute:NSForegroundColorAttributeName value:WJRGBAColor(245, 81, 81, 1) range:markRange1];

  [strAtt  addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"PingFangSC-Semibold" size:12] range:markRange1];

 self.m_editTransSumLabel.attributedText= strAtt;


若是针对多处的字符串内容设置字体大小和颜色,此时用UIlabel无效,可以用UITextView进行设置:

 self.m_editTransSumLabel.attributedText = @"您此笔交易转让价格由1000元修改为500元";

 NSString*tempStr =self.m_editTransSumLabel.text;

  NSMutableAttributedString*strAtt = [[NSMutableAttributedString alloc] initWithString:tempStr];

  [strAttaddAttribute:NSForegroundColorAttributeName value:WJRGBAColor(245, 81, 81, 1) range:NSMakeRange(0, [strAtt length])];

NSRangemarkRange1 = [tempStr rangeOfString:@"1000元"];

 [strAttaddAttribute:NSForegroundColorAttributeName value:WJRGBAColor(245, 81, 81, 1) range:markRange1];

  [strAttaddAttribute:NSFontAttributeName value:[UIFont fontWithName:@"PingFangSC-Semibold" size:12] range:markRange1];

 NSRangemarkRange2 = [tempStr rangeOfString:@"500元"];

   [strAttaddAttribute:NSForegroundColorAttributeName value:WJRGBAColor(245, 81, 81, 1) range:markRange2];

   [strAttaddAttribute:NSFontAttributeName value:[UIFont fontWithName:@"PingFangSC-Semibold" size:12] range:markRange2];

 self.m_editTransSumLabel.attributedText= strAtt;

你可能感兴趣的:(字符串中设置不同内容的字体和颜色)