// 建立可變屬性化字元串
const CGFloat fontSize = 20.0;
NSMutableAttributedString *attrString =
[[NSMutableAttributedString alloc] initWithString:string];
NSInteger length = [string length];
// 設定基本字型
UIFont *baseFont = [UIFont systemFontOfSize:fontSize];
[attrString addAttribute:NSFontAttributeName value:baseFont
range:NSMakeRange(0, length)];
UIColor *color = [UIColor redColor];
[attrString addAttribute:NSForegroundColorAttributeName
value:color
range:[string rangeOfString:@"surprise"]];
//設定行之間的margin
NSMutableParagraphStyle * pStyle = [[NSMutableParagraphStyle alloc]init];
pStyle.minimumLineHeight = 30;
pStyle.maximumLineHeight = 40;
[attrString addAttribute:NSParagraphStyleAttributeName value:pStyle range:NSMakeRange(0, length)];
// 設定下劃線-NSUnderlineStyleSingle -NSUnderlinePatternSolid 下劃線
[attrString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:NSUnderlineStyleDouble] range:NSMakeRange(10, 30)];
//NSUnderlineColorAttributeName -下劃線的顔色
[attrString addAttribute:NSUnderlineColorAttributeName value:[UIColor blueColor] range:NSMakeRange(10, 20)];
//NSStrokeColorAttributeName
[attrString addAttribute:NSStrokeColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 4)];
//NSStrokeWidthAttributeName
[attrString addAttribute:NSStrokeWidthAttributeName value:[NSNumber numberWithInt:2] range:NSMakeRange(0, 10)];
[attrString addAttribute:NSAttachmentAttributeName value:[[NSTextAttachment alloc] init] range:NSMakeRange(0, 10)];
//點選文字 打開連結
[attrString addAttribute:NSLinkAttributeName value:[NSURL URLWithString:@"http://www.baidu.com"] range:NSMakeRange(0, 4)];
//文字的傾斜度
[attrString addAttribute:NSObliquenessAttributeName value:[NSNumber numberWithFloat:0.5] range:NSMakeRange(length -5, 5)];
self.textview.editable = false;//讓UITextView 不可編輯,隻可檢視。
self.textview.attributedText = attrString;//[dict objectForKey:@"storage"];
self.textview.scrollsToTop = YES;