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

首頁 > 系統(tǒng) > iOS > 正文

iOS利用NSAttributedString實現(xiàn)圖文混排效果示例

2019-10-21 18:43:59
字體:
供稿:網(wǎng)友

前言

NSAttributedString 可以非常方便的實現(xiàn)文字排版和圖文混排功能,UILabel 和 UITextView 都能添加 NSAttributedString 屬性字符串,通過這一點,可以實現(xiàn)帶有屬性的文字和文字內(nèi)包含圖片的文本內(nèi)容展示。話不多說了,下面來一起看看詳細的介紹吧。

效果如下:

ios圖文混排,ios富文本圖文混排,ios開發(fā)圖文混排,NSAttributedString

ios圖文混排,ios富文本圖文混排,ios開發(fā)圖文混排,NSAttributedString

示例代碼如下:

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

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

2-設(shè)置全局字體屬性(設(shè)置字體大小為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云盤服務(wù)轉(zhuǎn)型公告"]];

4-獲取一大段文字范圍并修改屬性

通過前后字符串獲取大段字符的范圍

 // 此方法可以通過string獲得范圍進行修改 NSRange startRange = [textString localizedStandardRangeOfString:@"我們即將采取以下措施:"]; NSRange endRange = [textString localizedStandardRangeOfString:@"感謝您的一路相伴。"]; [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSUnionRange(startRange, endRange)];

5-為文本添加下劃線

 // 設(shè)置文本下劃線 NSRange startRange1 = [textString localizedStandardRangeOfString:@"因此,"]; NSRange endRange1 = [textString localizedStandardRangeOfString:@"之后轉(zhuǎn)型企業(yè)云服務(wù)。"]; [attributedString addAttribute:NSUnderlineStyleAttributeName value:@1 range:NSUnionRange(startRange1, endRange1)];

6-為文本內(nèi)文字添加描邊

 // 設(shè)置文本的描邊 [attributedString addAttribute:NSStrokeWidthAttributeName value:@2.0 range:[textString rangeOfString:@"存儲傳播非法文件、侵權(quán)盜版牟利、傳播淫穢色情信息等違法犯罪行為"]]; [attributedString addAttribute:NSStrokeColorAttributeName value:[UIColor brownColor] range:[textString rangeOfString:@"存儲傳播非法文件、侵權(quán)盜版牟利、傳播淫穢色情信息等違法犯罪行為"]]; [attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:17] range:[textString rangeOfString:@"存儲傳播非法文件、侵權(quán)盜版牟利、傳播淫穢色情信息等違法犯罪行為"]];

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-為文本設(shè)置段落屬性

 // 段落樣式 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-添加網(wǎng)址鏈接

 // 網(wǎng)址鏈接 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

總結(jié)

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


注:相關(guān)教程知識閱讀請移步到IOS開發(fā)頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 杭锦后旗| 包头市| 金乡县| 鹤庆县| 安阳市| 耿马| 高青县| 武夷山市| 循化| 文山县| 凌源市| 海安县| 武义县| 甘泉县| 渭南市| 揭阳市| 绥宁县| 玉环县| 淳化县| 江孜县| 孟津县| 铁力市| 博乐市| 潮安县| 太保市| 芦山县| 黔西| 芦溪县| 项城市| 龙海市| 灵石县| 绥宁县| 乌兰浩特市| 平泉县| 松桃| 万荣县| 滨海县| 资兴市| 嘉黎县| 普洱| 翁源县|