日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

iOS NSTextAttachment - 图文混排

發布時間:2023/12/13 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 iOS NSTextAttachment - 图文混排 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

蘋果在iOS7中推出了一個新的類NSTextAttachment,它是做圖文混排的利器,本文就是用這個類,只用50行代碼實現文字與表情混排,當然也可以實現段落中的圖文混排。?
首先說一下文字和表情的混排:?
先來做點兒準備工作,搞一個存放表情信息的plist文件?

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Image" ofType:@"plist"];NSArray *face = [NSArray arrayWithContentsOfFile:filePath];NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:text];NSString *regex_emoji =@"\\[[a-zA-Z0-9\\/\\u4e00-\\u9fa5]+\\]";//匹配表情NSError *error =nil;NSRegularExpression *re = [NSRegularExpression regularExpressionWithPattern:regex_emoji options:NSRegularExpressionCaseInsensitive error:&error];if(!re) {NSLog(@"%@", [error localizedDescription]);return attributeString;}NSArray *resultArray = [re matchesInString:text options:0 range:NSMakeRange(0, text.length)];NSMutableArray *imageArray = [NSMutableArray arrayWithCapacity:resultArray.count];//根據匹配范圍來用圖片進行相應的替換for(NSTextCheckingResult *match in resultArray) {//獲取數組元素中得到rangeNSRange range = [match range];//獲取原字符串中對應的值NSString *subStr = [text substringWithRange:range];for(int i =0; i < face.count; i ++) {if([face[i][@"cht"] isEqualToString:subStr]) {//face[i][@"png"]就是我們要加載的圖片//新建文字附件來存放我們的圖片,iOS7才新加的對象NSTextAttachment *textAttachment = [[NSTextAttachment alloc]init];//給附件添加圖片textAttachment.image= [UIImage imageNamed:face[i][@"png"]];//調整一下圖片的位置,如果你的圖片偏上或者偏下,調整一下bounds的y值即可textAttachment.bounds=CGRectMake(0, -8, textAttachment.image.size.width, textAttachment.image.size.height);//把附件轉換成可變字符串,用于替換掉源字符串中的表情文字NSAttributedString *imageStr = [NSAttributedString attributedStringWithAttachment:textAttachment];//把圖片和圖片對應的位置存入字典中NSMutableDictionary *imageDic = [NSMutableDictionary dictionaryWithCapacity:2];[imageDic setObject:imageStr forKey:@"image"];[imageDic setObject:[NSValue valueWithRange:range]forKey:@"range"];//把字典存入數組中[imageArray addObject:imageDic];}}}//4、從后往前替換,否則會引起位置問題for(int i = (int)imageArray.count-1; i >=0; i--) {NSRange range;[imageArray[i][@"range"] getValue:&range];//進行替換[attributeString replaceCharactersInRange:range withAttributedString:imageArray[i][@"image"]];}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86

以上代碼段是寫了一個工具類以實現圖文混排,也可以為NSString寫一個分類來實現。下面說一下用法:

NSString *content =@"文字加上表情[得意][][呲牙]";NSMutableAttributedString *attrStr = [Utility emotionStrWithString:content];_contentLabel.attributedText= attrStr;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

有了上面的方法,要實現圖片和文字的排布就更容易了,只需要將某些圖片給它設置一個固定的字符對應即可。?
具體方法如下:?
NSString *praiseStr =@”路人甲、路人乙”;

NSString *praiseInfo = [NSString stringWithFormat:@”<點贊> %@”,praiseStr];

NSDictionary *attributesForAll =@{NSFontAttributeName:[UIFont systemFontOfSize:14.0],NSForegroundColorAttributeName:[UIColorgrayColor]};

NSMutableAttributedString*attrStr = [Utility exchangeString:@”<點贊>”withText:praiseInfoimageName:@”dynamic_love_blue”];

有什么不足或者bug歡迎及時溝通!

總結

以上是生活随笔為你收集整理的iOS NSTextAttachment - 图文混排的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。