1. 程式人生 > >利用NSAttributedString實現圖文混排

利用NSAttributedString實現圖文混排

nes -s union ref and 代理方法 tag erl efault

UILabel 和 UITextView 都能添加 NSAttributedString 屬性字符串,通過這一點,可以實現帶有屬性的文字和文字內包含圖片的文本內容展示.


效果如下:
技術分享
技術分享

1-初始化可變屬性字符串

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:textString];

2-設置全局字體屬性(設置字體大小為14)

[attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:NSMakeRange(0, textString.length)];

[attributedString addAttribute:NSKernAttributeName value:@1 range:NSMakeRange(0, textString.length)];

上面兩句代碼可以簡寫為一句(為屬性字符串同時添加多個屬性)

[attributedString addAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:14],NSKernAttributeName: @1} range:NSMakeRange(0, textString.length)];

3-修改標題文字屬性

通過字符串獲取範圍

[attributedString addAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:26],NSForegroundColorAttributeName: [UIColor blueColor]} range:[textString rangeOfString:@"360雲盤服務轉型公告"]];

4-獲取一大段文字範圍並修改屬性

通過前後字符串獲取大段字符的範圍

// 此方法可以通過string獲得範圍進行修改

NSRange startRange = [textString localizedStandardRangeOfString:@"我們即將采取以下措施:"];

NSRange endRange = [textString localizedStandardRangeOfString:@"感謝您的一路相伴。"];

[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSUnionRange(startRange, endRange)];

5-為文本添加下劃線

// 設置文本下劃線

NSRange startRange1 = [textString localizedStandardRangeOfString:@"因此,"];

NSRange endRange1 = [textString localizedStandardRangeOfString:@"之後轉型企業雲服務。"];

[attributedString addAttribute:NSUnderlineStyleAttributeName value:@1 range:NSUnionRange(startRange1, endRange1)];

6-為文本內文字添加描邊

// 設置文本的描邊

[attributedString addAttribute:NSStrokeWidthAttributeName value:@2.0 range:[textString rangeOfString:@"存儲傳播非法文件、侵權盜版牟利、傳播淫穢色情信息等違法犯罪行為"]];

[attributedString addAttribute:NSStrokeColorAttributeName value:[UIColor brownColor] range:[textString rangeOfString:@"存儲傳播非法文件、侵權盜版牟利、傳播淫穢色情信息等違法犯罪行為"]];

[attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:17] range:[textString rangeOfString:@"存儲傳播非法文件、侵權盜版牟利、傳播淫穢色情信息等違法犯罪行為"]];

7-為文本添加圖片附件

// 插入圖片附件

NSTextAttachment *imageAtta = [[NSTextAttachment alloc] init];

imageAtta.bounds = CGRectMake(0, 0, 375, 180);

imageAtta.image = [UIImage imageNamed:@"360"];

NSAttributedString *attach = [NSAttributedString attributedStringWithAttachment:imageAtta];

[attributedString insertAttributedString:attach atIndex:0];

8-為文本設置段落屬性

// 段落樣式

NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc]init];

// 行間距

[style setLineSpacing:3];

// 段落間距

[style setParagraphSpacing:6];

// 首行縮進

[style setFirstLineHeadIndent:25];

[attributedString addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(1, textString.length)];

9-添加網址鏈接

// 網址鏈接

NSRange urlRange = [textString rangeOfString:@"yunpan.360.cn"];

[attributedString addAttribute:NSLinkAttributeName value:[NSURL URLWithString:@"http://yunpan.360.cn"] range:NSMakeRange(urlRange.location, 14)];

[attributedString addAttribute:NSBackgroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(urlRange.location, 14)];

10-通過UITextViewDelegate代理方法,監聽URL和附件的點擊

#pragma mark ----------UITextViewDelegate----------

- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange interaction:(UITextItemInteraction)interaction { NSLog(@"%@",URL); return YES; }

- (BOOL)textView:(UITextView *)textView shouldInteractWithTextAttachment:(NSTextAttachment *)textAttachment inRange:(NSRange)characterRange interaction:(UITextItemInteraction)interaction { NSLog(@"%@",textAttachment.image); return YES; }

補充:常用屬性字符串屬性

// 字體 NSFontAttributeName // UIFont, default Helvetica(Neue) 12

// 段落 NSParagraphStyleAttributeName // NSParagraphStyle, default defaultParagraphStyle

// 文字顏色 NSForegroundColorAttributeName // UIColor, default blackColor

// 背景顏色 NSBackgroundColorAttributeName // UIColor, default nil: no background

// 描邊顏色 NSStrokeColorAttributeName // UIColor, default nil: same as foreground color

// 描邊寬度 NSStrokeWidthAttributeName // NSNumber containing floating point value, default 0

// 陰影 NSShadowAttributeName // NSShadow, default nil: no shadow

// 附件 NSAttachmentAttributeName // NSTextAttachment, default nil

// 鏈接URL NSLinkAttributeName // NSURL (preferred) or NSString

// 基線偏移量 NSBaselineOffsetAttributeName // NSNumber containing floating point value,default 0

// 下劃線 NSUnderlineColorAttributeName // UIColor, default nil: same as foreground color

轉自:http://blog.csdn.net/Mazy_ma/article/details/52920596

利用NSAttributedString實現圖文混排