国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

iOS利用NSAttributedString實現圖文混排效果示例

2020-07-26 02:38:38
字體:
來源:轉載
供稿:網友

前言

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.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代理方法,監(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

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對武林網的支持。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 金阳县| 汉中市| 高雄市| 塔河县| 新民市| 漳浦县| 蓝山县| 内江市| 乐至县| 体育| 象山县| 合作市| 综艺| 洛南县| 卓尼县| 厦门市| 页游| 老河口市| 龙南县| 浏阳市| 巨鹿县| 茌平县| 镇江市| 英吉沙县| 固原市| 鄂尔多斯市| 晋宁县| 清水河县| 峡江县| 始兴县| 沂南县| 任丘市| 都安| 巫溪县| 白山市| 大足县| 旅游| 道孚县| 乌鲁木齐县| 上林县| 名山县|