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;
NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithString:@"床前明月光,疑是地上霜"];
[attrStr addAttribute: NSFontAttributeName value: [UIFont fontWithName: @"Papyrus" size: 15] range: NSMakeRange(0, attrStr.length)];
NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithString:@"床前明月光,疑是地上霜"];
[attrStr addAttribute: NSForegroundColorAttributeName value: [UIColor orangeColor] range: NSMakeRange(0, attrStr.length)];
NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithString:@"床前明月光,疑是地上霜"];
[attrStr addAttribute: NSBackgroundColorAttributeName value: [UIColor redColor] range: NSMakeRange(0, attrStr.length)];
NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithString:@"床前明月光,疑是地上霜"];
[attrStr addAttribute: NSKernAttributeName value: @(2) range: NSMakeRange(0, attrStr.length)];
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)];
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)];
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)];
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];
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;
NSAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:@"我是NSBaselineOffsetAttributeName 偏移5"];
[attrStr addAttribute: NSBaselineOffsetAttributeName value: @(5) range: NSMakeRange(0, attrStr.length)];
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)];
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)];
NSAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:@"我是NSStrikethroughColorAttributeName 右倾斜"];
[attrStr addAttribute: NSObliquenessAttributeName value:@(1) range: NSMakeRange(0, attrStr.length)];
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)];
@[@(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)];
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];