最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2017年10月27日 08:29:18   作者:Mazy_ma  
iOS7以后,因為TextKit的強大,可以用NSAttributedString很方便的實現圖文混排(主要是利用了NSTextAttachment),所以下面這篇文章主要給大家介紹了關于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

總結

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

相關文章

最新評論

丰城市| 宁乡县| 赣州市| 黄冈市| 怀来县| 玉田县| 乌苏市| 久治县| 呈贡县| 延津县| 普陀区| 莱西市| 离岛区| 崇阳县| 五台县| 泸西县| 亚东县| 贵定县| 四平市| 确山县| 兰坪| 阳泉市| 习水县| 上思县| 邹平县| 望奎县| 湘乡市| 眉山市| 治多县| 曲周县| 方山县| 新乡市| 金沙县| 泸西县| 日土县| 滦南县| 吉林省| 弋阳县| 新竹市| 鸡泽县| 新平|