使用UITextView實現圖文混排
使用UITextView實現超鏈接,與自定義表情皮配,還能加載自定義大小的本地圖片與網絡圖片。
用的是系統UITextView控件的linkTextAttributes和attributedText屬性實現了此功能,相關API須在>=7.0的系統上運行。由于實現了在文本里加載網絡圖片,所以導入了SDWebImage第三方文件,若不需要可以刪除LXTextView.h文件里的相關代碼(#import "UIImageView+WebCache.h"以及- (void)downloadImageWithUrl:(NSString *)urlString withIndex:(NSInteger)index)。
效果圖如下:
screen.png
注意:
- 效果圖上匹配了一些錯誤的網址,那沒關系,正則表達式不夠嚴謹,這也可以滿足一般需求了。
- 鏈接需要深按才有效,系統的原因。
使用方法非常簡單,集成代碼如下:
LXTextView *textView = [[LXTextView alloc] initWithFrame:CGRectMake(10, 10, kScreenWidth - 20, kScreenHeight - 20)];
textView.delegate = self;
textView.editable = NO; // 非編輯狀態下才可以點擊Url]
textView.fontFloat = 18.;
textView.textString = @"Thiswww.hiphotos.baidu.comis ancel超鏈接ofa bri_StringWith Attachment:textAttachment]indfdxcsehttps://www.baidu.comrtAttribu tedStriUse [傲慢]this method to release shared invali dateexwwwwssdvdv.hiphotos.baidu.dsccomamptimers, and [嚇]isa覆蓋核 http://c.hip[大哭]hotos.baidu.com設施nexstore enough application state information[微笑] to re https://c.hiphotos.baidu.cnstore your";
[self.view addSubview:textView];
核心代碼如下:
- (void)setTextString:(NSString *)string
{
NSDictionary *attributeDict =
[NSDictionary dictionaryWithObjectsAndKeys:
[UIFont systemFontOfSize:self.fontFloat], NSFontAttributeName,
[UIColor blackColor], NSForegroundColorAttributeName,nil];
attributedString = [[NSMutableAttributedString alloc] initWithString:string attributes:attributeDict];
//設置行距與換行模式
NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = 5.0;
paragraphStyle.lineBreakMode = NSLineBreakByCharWrapping;
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, string.length)];
NSArray *emojiArr = [LXHelpClass validateEmojiOfText:attributedString.string];
for (int i = 0; i < emojiArr.count; i++) {
NSInteger index = [helpClass.keyArr indexOfObject:emojiArr[i]];
NSString *imgName = [helpClass.valueArr objectAtIndex:index];
NSRange range = [attributedString.string rangeOfString:emojiArr[i] options:NSCaseInsensitiveSearch range:NSMakeRange(0, attributedString.string.length)];
//正則表達式匹配的表情
LXTextAttachment *textAttachmentEmoji = [[LXTextAttachment alloc] initWithData:nil ofType:nil];
textAttachmentEmoji.imgSize = CGSizeMake(30 ,30);
textAttachmentEmoji.image = [UIImage imageNamed:imgName] ;
NSAttributedString *textAttachmentString = [NSAttributedString attributedStringWithAttachment:textAttachmentEmoji];
[attributedString replaceCharactersInRange:range withAttributedString:textAttachmentString];
}
//自定義添加的本地圖片
LXTextAttachment *textAttachmentImg = [[LXTextAttachment alloc] initWithData:nil ofType:nil];
textAttachmentImg.imgSize = CGSizeMake(270, 160);
textAttachmentImg.image = [UIImage imageNamed:@"bird"];
NSAttributedString *textAttachmentString2 = [NSAttributedString attributedStringWithAttachment:textAttachmentImg];
[attributedString insertAttributedString:textAttachmentString2 atIndex:56];
NSArray *linkArr = [LXHelpClass validateLinkOfText:attributedString.string];
for (int i = 0; i < linkArr.count; i++) {
NSRange range = NSRangeFromString(linkArr[i]);
//正則表達式匹配的鏈接
NSString *linkStr = [attributedString.string substringWithRange:range];
[attributedString addAttribute:NSLinkAttributeName value:linkStr range:range];
}
//自定義的鏈接
[attributedString addAttribute:NSLinkAttributeName value:@"http://baidu.com" range:[[attributedString string] rangeOfString:@"超鏈接"]];
NSDictionary *linkAttributes =
@{NSForegroundColorAttributeName:[UIColor greenColor], NSUnderlineColorAttributeName:[UIColor lightGrayColor], NSUnderlineStyleAttributeName:@(NSUnderlinePatternSolid)};
self.linkTextAttributes = linkAttributes;
self.attributedText = attributedString;
}
本文由用戶 wc553311 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!