Label显示不同颜色、字体的文字

NSString *string = @"床前明月光,疑是地上霜";
NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithString:string];
NSRange range1 = [[attrStr string]rangeOfString:@"光"];
NSRange range2 = [[attrStr string]rangeOfString:@"霜"];
[attrStr addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:range1];
[attrStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range2];
label.attributedText = attrStr;

AttributedString属性

  • NSFontAttributeName 设置字体
    获取系统的字体:[UIFont familyNames]
NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithString:@"床前明月光,疑是地上霜"];
[attrStr addAttribute: NSFontAttributeName value: [UIFont fontWithName: @"Papyrus" size: 15] range: NSMakeRange(0, attrStr.length)];
  • NSForegroundColorAttributeName 设置字体颜色
NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithString:@"床前明月光,疑是地上霜"];
[attrStr addAttribute: NSForegroundColorAttributeName value: [UIColor orangeColor] range: NSMakeRange(0, attrStr.length)];
  • NSBackgroundColorAttributeName 设置字体所在区域背景颜色
NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithString:@"床前明月光,疑是地上霜"];
[attrStr addAttribute: NSBackgroundColorAttributeName value: [UIColor redColor] range: NSMakeRange(0, attrStr.length)];
  • NSKernAttributeName 设定字符间距
NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithString:@"床前明月光,疑是地上霜"];
[attrStr addAttribute: NSKernAttributeName value: @(2) range: NSMakeRange(0, attrStr.length)];
  • NSStrikethroughStyleAttributeName 设置删除线
NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithString:@"床前明月光,疑是地上霜"];
[attrStr addAttribute: NSStrikethroughStyleAttributeName value: @(1) range: NSMakeRange(0, attrStr.length)];
  • NSUnderlineStyleAttributeName 设置下划线
    NSUnderlineStyle

  • NSStrokeColorAttributeName 设置填充颜色

NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithString:@"我是NSStrokeColorAttributeName 配合NSStrokeWidthAttributeName使用"];
[attrStr addAttribute: NSStrokeColorAttributeName value: [UIColor greenColor] range: NSMakeRange(0, attrStr.length)];
[attrStr addAttribute: NSStrokeWidthAttributeName value: @(2) range: NSMakeRange(0, attrStr.length)];
  • NSStrokeWidthAttributeName 设置笔画宽度
    负值填充效果,正值中空效果。与NSStrokeColorAttributeName配合使用
NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithString:@"床前明月光,疑是地上霜"];
[attrStr addAttribute: NSStrokeWidthAttributeName value: @(-2) range: NSMakeRange(0, attrStr.length)];
[attrStr addAttribute: NSStrokeColorAttributeName value: [UIColor blueColor] range: NSMakeRange(0, attrStr.length)];
  • NSShadowAttributeName 阴影
NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithString:@"我是NSShadowAttributeName 竖直方向偏移 15"];
NSShadow * shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor grayColor];
shadow.shadowOffset = CGSizeMake(0, 15);
[attrStr addAttribute: NSShadowAttributeName value:shadow range: NSMakeRange(0, attrStr.length)];
  • NSAttachmentAttributeName 设置文本附件,取值为NSTextAttachment对象,常用于文字图片混排
NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithString:@"我是NSAttachmentAttributeName "];
NSTextAttachment * textAtt = [[NSTextAttachment alloc] init];
textAtt.image = [UIImage imageNamed:@"tag_d"];
textAtt.bounds = CGRectMake(0, 0, 44, 44);
NSAttributedString *attrStr2 = [NSAttributedString attributedStringWithAttachment: textAtt];
[attrStr insertAttributedString: attrStr2 atIndex: 6];


- NSLinkAttributeName 设置链接属性

UITextView *textView = [[UITextView alloc] init];
textView.scrollEnabled = NO;
textView.editable = NO;
textView.frame =cell.bounds;
textView.textContainer.lineFragmentPadding = 0;
textView.textContainerInset = UIEdgeInsetsMake(0, 0, 0, 0);
textView.delegate = self;
attrStr = [[NSMutableAttributedString alloc] initWithString:@"百度"];
[attrStr addAttribute: NSLinkAttributeName value:[NSURL URLWithString: @"http://www.baidu.com"] range: NSMakeRange(0, attrStr.length)];
textView.attributedText = attrStr;
  • NSBaselineOffsetAttributeName 设置基线偏移值,取值为 NSNumber (float),正值上偏,负值下偏
NSAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:@"我是NSBaselineOffsetAttributeName 偏移5"];
[attrStr addAttribute: NSBaselineOffsetAttributeName value: @(5) range: NSMakeRange(0, attrStr.length)];
  • NSUnderlineColorAttributeName 设置下划线颜色
NSAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:@"我是NSUnderlineColorAttributeName "];
[attrStr addAttribute: NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range: NSMakeRange(0, attrStr.length)];
[attrStr addAttribute: NSUnderlineColorAttributeName value:[UIColor yellowColor] range: NSMakeRange(0, attrStr.length)];
  • NSStrikethroughColorAttributeName 设置删除线颜色
NSAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:@"我是NSStrikethroughColorAttributeName "];
[attrStr addAttribute: NSStrikethroughStyleAttributeName value:@(1) range: NSMakeRange(0, attrStr.length)];
[attrStr addAttribute: NSStrikethroughColorAttributeName value:[UIColor brownColor] range: NSMakeRange(0, attrStr.length)];
  • NSObliquenessAttributeName 设置字形倾斜度 正值右倾,负值左倾
NSAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:@"我是NSStrikethroughColorAttributeName 右倾斜"];
[attrStr addAttribute: NSObliquenessAttributeName value:@(1) range: NSMakeRange(0, attrStr.length)];
  • NSExpansionAttributeName设置文本横向拉伸属性,取值为 NSNumber (float),正值横向拉伸文本,负值横向压缩
NSAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:@"我是NSExpansionAttributeName 横向压"];
[attrStr addAttribute: NSFontAttributeName value: [UIFont fontWithName: @"Papyrus" size: 12] range: NSMakeRange(0, attrStr.length)];
[attrStr addAttribute: NSExpansionAttributeName value:@(1) range: NSMakeRange(0, attrStr.length)];
  • NSWritingDirectionAttributeName 设置文字书写方向,取值为以下组合
@[@(NSWritingDirectionLeftToRight|NSWritingDirectionEmbedding)];  
@[@(NSWritingDirectionRightToLeft|NSWritingDirectionEmbedding)];   
@[@(NSWritingDirectionLeftToRight|NSWritingDirectionOverride)];
@[@(NSWritingDirectionRightToLeft|NSWritingDirectionOverride)];
NSAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:@"我是NSWritingDirectionAttributeName 方式NSWritingDirectionLeftToRight&Embedding"];
[attrStr addAttribute: NSWritingDirectionAttributeName value:@[@(NSWritingDirectionLeftToRight | NSWritingDirectionEmbedding)] range: NSMakeRange(0, attrStr.length)];
  • NSDocumentTypeDocumentAttribute 直接进行 html 的展示
NSString * htmlString = @"<html><body> Some html string \n <font size=\"13\" color=\"red\">This is some text!This is some text!This is some text!This is some text!This is some text!font> body>html>";
attrStr = [[NSMutableAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];

Label显示不同颜色、字体的文字_第1张图片

你可能感兴趣的:(iOS)