iOS利用NSAttributedString實現圖文混排效果示例
前言
NSAttributedString 可以非常方便的實現文字排版和圖文混排功能,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:@"之后轉型企業(yè)云服務。"]; [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.#"]; [attributedString addAttribute:NSLinkAttributeName value:[NSURL URLWithString:@"http://yunpan.#"] range:NSMakeRange(urlRange.location, 14)]; [attributedString addAttribute:NSBackgroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(urlRange.location, 14)];
10-通過UITextViewDelegate代理方法,監(jiān)聽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
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關文章
詳解ios中自定義cell,自定義UITableViewCell
本篇文章主要介紹了ios中自定義cell,自定義UITableViewCell,非常具有實用價值,需要的朋友可以參考下。2016-12-12iOS應用開發(fā)中AFNetworking庫的常用HTTP操作方法小結
AFNetworking庫是Objective-C語言寫成的用于處理HTTP的第三方庫,在GitHub上開源并且一直在被更新和維護,下面就一起來看一下iOS應用開發(fā)中AFNetworking庫的常用HTTP操作方法小結2016-05-05iOS中使用UISearchBar控件限制輸入字數的實現方法
這篇文章主要介紹了iOS中使用UISearchBar控件限制輸入字數的實現方法的相關資料,需要的朋友可以參考下2016-08-08