前言:項(xiàng)目中用到檢索表情几晤,網(wǎng)址與號(hào)碼,但是看了TTTAttributeLabel植阴,emojyLabel蟹瘾,奈何都不太滿意,plist格式不太符合掠手,而且這兩個(gè)第三方用到檢索都是系統(tǒng)自帶的檢索憾朴,檢測(cè)網(wǎng)址方面不準(zhǔn)確, 所以就需要自己使用正則進(jìn)行檢索喷鸽。
關(guān)于以上兩個(gè)三方檢索不準(zhǔn)確的可以參考:檢索網(wǎng)址
接下來寫一下實(shí)現(xiàn)的過程伊脓, 沒有高度封裝,僅供參考
關(guān)于網(wǎng)址與號(hào)碼的正則再說明下:
網(wǎng)址:KURlREGULAR @"((http[s]{0,1}|ftp)://[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)|(www.[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)|(((http[s]{0,1}|ftp)://|)((?:(?:25[0-5]|2[0-4]\\d|((1\\d{2})|([1-9]?\\d)))\\.){3}(?:25[0-5]|2[0-4]\\d|((1\\d{2})|([1-9]?\\d))))(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)"
號(hào)碼: KPHONENUMBERREGLAR @"\\d{3}-\\d{8}|\\d{4}-\\d{7}|\\d{11}"
比如我要轉(zhuǎn)的字符串為
@"簡(jiǎn)書:http://jianshu.com哈哈哈[調(diào)皮][流汗][偷笑][再見][可愛][色][害羞][委屈][委屈][抓狂][酷][酷][噓][噓][齜牙][大哭][大哭][大哭][齜牙][噓][噓][調(diào)皮][調(diào)皮]哈哈哈哈[噓][調(diào)皮][調(diào)皮]18637963241他大舅他二舅都是舅魁衙,高桌子地板頭都是木頭"
我需要做的是檢索網(wǎng)址并且替換為和微博一樣的鏈接,號(hào)碼和鏈接有選中狀態(tài)株搔,因?yàn)閁ITextview有檢測(cè)url 的方法可以添加點(diǎn)擊事件剖淀,所以就采用UITextview進(jìn)行封裝。主要采取正則表達(dá)式與RegexKitLite
配合做檢索
1 . 首先建立一個(gè)模型纤房,把文字檢索為富文本纵隔,檢索出 表情,網(wǎng)址以及號(hào)碼炮姨。中間需要使用一個(gè)模型存儲(chǔ)檢索出來的結(jié)果捌刮。對(duì)于特殊符號(hào)的表情建立一個(gè)模型。其實(shí)富文本的都是逐個(gè)檢索舒岸,然后處理绅作,最后拼接為一個(gè)NSMutableAttributedString。
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface ZLStatus : NSObject
//源內(nèi)容
@property (nonatomic, copy) NSString *text;
/** string 信息內(nèi)容 -- 帶有屬性的(特殊文字會(huì)高亮顯示\顯示表情)*/
@property (nonatomic, copy) NSAttributedString *attributedText;
@end
#import "ZLStatus.h"
#import "ZLSpecial.h"
#import "ZLTextPart.h"
#import "RegexKitLite.h"
@implementation ZLStatus
- (void)setText:(NSString *)text
{
_text = [text copy];
// 利用text生成attributedText
self.attributedText = [self attributedTextWithText:text];
}
/**
* 普通文字 --> 屬性文字
*
* @param text 普通文字
*
* @return 屬性文字
*/
- (NSAttributedString *)attributedTextWithText:(NSString *)text
{
NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] init];
// 表情的規(guī)則
NSString *emotionPattern = @"\\[[0-9a-zA-Z\\u4e00-\\u9fa5]+\\]";
// @的規(guī)則
NSString *atPattern = @"@[0-9a-zA-Z\\u4e00-\\u9fa5-_]+";
// #話題#的規(guī)則
NSString *topicPattern = @"#[0-9a-zA-Z\\u4e00-\\u9fa5]+#";
// url鏈接的規(guī)則
NSString *urlPattern = @"((http[s]{0,1}|ftp)://[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)|(www.[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)|(((http[s]{0,1}|ftp)://|)((?:(?:25[0-5]|2[0-4]\\d|((1\\d{2})|([1-9]?\\d)))\\.){3}(?:25[0-5]|2[0-4]\\d|((1\\d{2})|([1-9]?\\d))))(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)";
NSString *phoneNumber =@"\\d{3}-\\d{8}|\\d{3}-\\d{7}|\\d{4}-\\d{8}|\\d{4}-\\d{7}|1+[3578]+\\d{9}|\\d{8}|\\d{7}"
;
NSString *pattern = [NSString stringWithFormat:@"%@|%@|%@|%@|%@", emotionPattern, atPattern, topicPattern, urlPattern,phoneNumber];
// 遍歷所有的特殊字符串
NSMutableArray *parts = [NSMutableArray array];
[text enumerateStringsMatchedByRegex:pattern usingBlock:^(NSInteger captureCount, NSString *const __unsafe_unretained *capturedStrings, const NSRange *capturedRanges, volatile BOOL *const stop) {
if ((*capturedRanges).length == 0) return;
ZLTextPart *part = [[ZLTextPart alloc] init];
part.special = YES;
part.text = *capturedStrings;
part.emotion = [part.text hasPrefix:@"["] && [part.text hasSuffix:@"]"];
part.range = *capturedRanges;
[parts addObject:part];
}];
// 遍歷所有的非特殊字符
[text enumerateStringsSeparatedByRegex:pattern usingBlock:^(NSInteger captureCount, NSString *const __unsafe_unretained *capturedStrings, const NSRange *capturedRanges, volatile BOOL *const stop) {
if ((*capturedRanges).length == 0) return;
ZLTextPart *part = [[ZLTextPart alloc] init];
part.text = *capturedStrings;
part.range = *capturedRanges;
[parts addObject:part];
}];
// 排序
// 系統(tǒng)是按照從小 -> 大的順序排列對(duì)象
[parts sortUsingComparator:^NSComparisonResult(ZLTextPart *part1, ZLTextPart *part2) {
// NSOrderedAscending = -1L, NSOrderedSame, NSOrderedDescending
// 返回NSOrderedSame:兩個(gè)一樣大
// NSOrderedAscending(升序):part2>part1
// NSOrderedDescending(降序):part1>part2
if (part1.range.location > part2.range.location) {
// part1>part2
// part1放后面, part2放前面
return NSOrderedDescending;
}
// part1<part2
// part1放前面, part2放后面
return NSOrderedAscending;
}];
UIFont *font = [UIFont systemFontOfSize:15];
NSMutableArray *specials = [NSMutableArray array];
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"face.plist" ofType:nil];
NSArray *face = [NSArray arrayWithContentsOfFile:filePath];
// 按順序拼接每一段文字
for (ZLTextPart *part in parts) {
// 等會(huì)需要拼接的子串
NSAttributedString *substr = nil;
if (part.isEmotion) { // 表情 表情處理的時(shí)候蛾派,需要根據(jù)你自己的plist進(jìn)行單獨(dú)處理俄认,像一些第三方里自定義的plist个少,格式要是也是很嚴(yán)格的
NSString *str = [text substringWithRange:part.range];
for (int i = 0; i < face.count; i ++) {
if ([face[i][@"face_name"] isEqualToString:str]) {
//face[i][@"png"]就是我們要加載的圖片
//新建文字附件來存放我們的圖片,iOS7才新加的對(duì)象
NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
//給附件添加圖片
textAttachment.image = [UIImage imageNamed:face[i][@"face_image_name"]];
//調(diào)整一下圖片的位置,如果你的圖片偏上或者偏下,調(diào)整一下bounds的y值即可
textAttachment.bounds = CGRectMake(0, -6, 25, 25);
//把附件轉(zhuǎn)換成可變字符串眯杏,用于替換掉源字符串中的表情文字
substr = [NSAttributedString attributedStringWithAttachment:textAttachment];
break;
}
}
} else if (part.special) { // 非表情的特殊文字
NSURL *url =[NSURL URLWithString:part.text];
if (url.scheme) {
substr = [[NSAttributedString alloc] initWithString:part.text attributes:@{
NSForegroundColorAttributeName : [UIColor redColor]
}];
NSString *string =@"網(wǎng)頁(yè)鏈接";
NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
//給附件添加圖片
textAttachment.image = [UIImage imageNamed:@"鏈接"];
//調(diào)整一下圖片的位置,如果你的圖片偏上或者偏下夜焦,調(diào)整一下bounds的y值即可
textAttachment.bounds = CGRectMake(0, -6, 25, 25);
NSMutableAttributedString *tempAttribute = [[NSMutableAttributedString alloc]initWithAttributedString:[NSAttributedString attributedStringWithAttachment:textAttachment]];
NSMutableAttributedString *tempAttribute2 = [[NSMutableAttributedString alloc]initWithAttributedString:[NSAttributedString attributedStringWithAttachment:textAttachment]];
NSAttributedString *text =[[NSAttributedString alloc]initWithString:string attributes:@{NSForegroundColorAttributeName:[UIColor blueColor]}];
[tempAttribute appendAttributedString:text];
substr = [[NSAttributedString alloc]initWithAttributedString:tempAttribute];
// 創(chuàng)建特殊對(duì)象
ZLSpecial *s = [[ZLSpecial alloc] init];
s.text = string;
//需要添加附屬圖片的長(zhǎng)度,負(fù)責(zé)點(diǎn)擊范圍會(huì)變化
NSUInteger loc = attributedText.length+tempAttribute2.length;
NSUInteger len = string.length;
s.range = NSMakeRange(loc, len);
s.urlString = part.text;
[specials addObject:s];
}else{
NSLog(@"%@",part.text);
substr = [[NSAttributedString alloc] initWithString:part.text attributes:@{
NSForegroundColorAttributeName : [UIColor redColor]
}];
// 創(chuàng)建特殊對(duì)象
ZLSpecial *s = [[ZLSpecial alloc] init];
s.text = part.text;
NSUInteger loc = attributedText.length;
NSUInteger len = part.text.length;
s.range = NSMakeRange(loc, len);
s.urlString = part.text;
[specials addObject:s];
}
} else { // 非特殊文字
substr = [[NSAttributedString alloc] initWithString:part.text];
}
[attributedText appendAttributedString:substr];
}
// 一定要設(shè)置字體,保證計(jì)算出來的尺寸是正確的
[attributedText addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, attributedText.length)];
[attributedText addAttribute:@"specials" value:specials range:NSMakeRange(0, 1)];
return attributedText;
}
中間存儲(chǔ)檢索結(jié)果與特殊字符的model:
#import <Foundation/Foundation.h>
@interface ZLTextPart : NSObject
/** 這段文字的內(nèi)容 */
@property (nonatomic, copy) NSString *text;
/** 這段文字的范圍 */
@property (nonatomic, assign) NSRange range;
/** 是否為特殊文字 */
@property (nonatomic, assign, getter = isSpecical) BOOL special;
/** 是否為表情 */
@property (nonatomic, assign, getter = isEmotion) BOOL emotion;
@end
#import <Foundation/Foundation.h>
@interface ZLSpecial : NSObject
/** 這段特殊文字的內(nèi)容 */
@property (nonatomic, copy) NSString *text;
/** 這段特殊文字的范圍 */
@property (nonatomic, assign) NSRange range;
@property(nonatomic,copy)NSString *urlString;
設(shè)置完內(nèi)容后我們需要計(jì)算內(nèi)容高度岂贩,然后復(fù)制給Textview,建立一個(gè)Frame模型茫经。
#import <Foundation/Foundation.h>
#import "ZLStatus.h"
@interface ZLFrame : NSObject
//設(shè)置
/** 限制最大行數(shù) 這一步必須在設(shè)置完contentLabelF之后設(shè)置*/
@property(nonatomic,assign)int maxNumLine;
@property(nonatomic,strong)ZLStatus *status;
@property(nonatomic,assign)CGFloat frameX;
@property(nonatomic,assign)CGFloat frameY;
@property(nonatomic,assign)CGFloat maxWidth;
//取值
/** */
/** 正文 */
@property (nonatomic, assign) CGRect contentLabelF;
@property(nonatomic,assign)CGRect maxNumLabelF;
@end
#import "ZLFrame.h"
@interface ZLFrame()
//檢測(cè)高度的label;
@property(nonatomic,strong)UILabel *templateLabel;
@end
@implementation ZLFrame
-(void)setStatus:(ZLStatus *)status{
_status = status;
CGSize contentSize = [status.attributedText boundingRectWithSize:CGSizeMake(self.maxWidth, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin context:nil].size;
self.contentLabelF = (CGRect){{self.frameX , self.frameY}, contentSize};
}
-(void)setMaxNumLine:(int)maxNumLine{
_maxNumLine = maxNumLine;
self.templateLabel.frame =CGRectMake(self.frameX, self.frameY, self.maxWidth, 0);
self.templateLabel.attributedText = self.status.attributedText;
self.templateLabel.numberOfLines = maxNumLine;
[self.templateLabel sizeToFit];
self.maxNumLabelF = self.templateLabel.frame;
}
-(void)setFrameX:(CGFloat)frameX{
_frameX = frameX;
}
-(void)setFrameY:(CGFloat)frameY{
_frameY = frameY;
}
-(void)setMaxWidth:(CGFloat)maxWidth{
_maxWidth = maxWidth;
}
-(UILabel *)templateLabel{
if (!_templateLabel) {
_templateLabel =[[UILabel alloc]init];
}
return _templateLabel;
}
@end
最后自定義Textview,引入Frame模型
#import <UIKit/UIKit.h>
#import "ZLFrame.h"
@interface ZLStatusTextView : UITextView
/** 所有的特殊字符串(里面存放著HWSpecial) */
@property (nonatomic, strong) NSArray *specials;
@property(nonatomic,strong)ZLFrame *zlFrame;
@property(nonatomic,assign)int maxLine;
@property(nonatomic,assign)BOOL isShowAll;//是否全部顯示
@end
#import "ZLStatusTextView.h"
#import "ZLSpecial.h"
#define ZLStatusTextViewCoverTag 999
@implementation ZLStatusTextView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor clearColor];
self.editable = NO;
self.textContainerInset = UIEdgeInsetsMake(0, -5, 0, -5);
// 禁止?jié)L動(dòng), 讓文字完全顯示出來
self.scrollEnabled = NO;
}
return self;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// 觸摸對(duì)象
UITouch *touch = [touches anyObject];
// 觸摸點(diǎn)
CGPoint point = [touch locationInView:self];
NSArray *specials = [self.attributedText attribute:@"specials" atIndex:0 effectiveRange:NULL];
BOOL contains = NO;
for (ZLSpecial *special in specials) {
self.selectedRange = special.range;
// self.selectedRange --影響--> self.selectedTextRange
// 獲得選中范圍的矩形框
NSArray *rects = [self selectionRectsForRange:self.selectedTextRange];
// 清空選中范圍
self.selectedRange = NSMakeRange(0, 0);
for (UITextSelectionRect *selectionRect in rects) {
CGRect rect = selectionRect.rect;
if (rect.size.width == 0 || rect.size.height == 0) continue;
if (CGRectContainsPoint(rect, point)) { // 點(diǎn)中了某個(gè)特殊字符串
contains = YES;
break;
}
}
if (contains) {
for (UITextSelectionRect *selectionRect in rects) {
CGRect rect = selectionRect.rect;
if (rect.size.width == 0 || rect.size.height == 0) continue;
UIView *cover = [[UIView alloc] init];
cover.backgroundColor = [UIColor greenColor];
cover.frame = rect;
cover.tag = ZLStatusTextViewCoverTag;
cover.layer.cornerRadius = 5;
[self insertSubview:cover atIndex:0];
}
break;
}
}
// 在被觸摸的特殊字符串后面顯示一段高亮的背景
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// 觸摸對(duì)象
UITouch *touch = [touches anyObject];
// 觸摸點(diǎn)
CGPoint point = [touch locationInView:self];
NSArray *specials = [self.attributedText attribute:@"specials" atIndex:0 effectiveRange:NULL];
BOOL contains = NO;
for (ZLSpecial *special in specials) {
self.selectedRange = special.range;
// self.selectedRange --影響--> self.selectedTextRange
// 獲得選中范圍的矩形框
NSArray *rects = [self selectionRectsForRange:self.selectedTextRange];
// 清空選中范圍
self.selectedRange = NSMakeRange(0, 0);
for (UITextSelectionRect *selectionRect in rects) {
CGRect rect = selectionRect.rect;
if (rect.size.width == 0 || rect.size.height == 0) continue;
if (CGRectContainsPoint(rect, point)) { // 點(diǎn)中了某個(gè)特殊字符串
contains = YES;
break;
}
}
if (contains) {
for (UITextSelectionRect *selectionRect in rects) {
CGRect rect = selectionRect.rect;
if (rect.size.width == 0 || rect.size.height == 0) continue;
if (special.urlString) {
NSString *urlStr = special.urlString;
NSURL *url =[NSURL URLWithString:urlStr];
if (url.scheme) {
[[UIApplication sharedApplication]openURL:url];
}else{
NSMutableString *str=[[NSMutableString alloc] initWithFormat:@"tel:%@",special.text];
UIWebView *callWebview = [[UIWebView alloc] init];
[callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
[self addSubview:callWebview];
}
}
}
break;
}
}
[self touchesCancelled:touches withEvent:event];
});
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
// 去掉特殊字符串后面的高亮背景
for (UIView *child in self.subviews) {
if (child.tag == ZLStatusTextViewCoverTag) [child removeFromSuperview];
}
}
-(void)setZlFrame:(ZLFrame *)zlFrame{
_zlFrame = zlFrame;
self.attributedText = zlFrame.status.attributedText;
self.frame = zlFrame.contentLabelF;
}
-(void)setMaxLine:(int)maxLine{
_maxLine = maxLine;
[self.zlFrame setMaxNumLine:maxLine];
self.frame = self.zlFrame.maxNumLabelF;
}
-(void)setIsShowAll:(BOOL)isShowAll{
_isShowAll = isShowAll;
if (isShowAll) {
self.frame = self.zlFrame.contentLabelF;
}else{
[self setMaxLine:3];
}
}
@end
最后在ViewController里引入即可:
#import "ViewController.h"
#import "ZLStatusTextView.h"
#define kTempText @"簡(jiǎn)書:http://jianshu.com哈哈哈[調(diào)皮][流汗][偷笑][再見][可愛][色][害羞][委屈][委屈][抓狂][酷][酷][噓][噓][齜牙][大哭][大哭][大哭][齜牙][噓][噓][調(diào)皮][調(diào)皮]哈哈哈哈[噓][調(diào)皮][調(diào)皮]18637963241他大舅他二舅都是舅萎津,高桌子地板頭都是木頭"
#define KURlREGULAR @"((http[s]{0,1}|ftp)://[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)|(www.[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)|(((http[s]{0,1}|ftp)://|)((?:(?:25[0-5]|2[0-4]\\d|((1\\d{2})|([1-9]?\\d)))\\.){3}(?:25[0-5]|2[0-4]\\d|((1\\d{2})|([1-9]?\\d))))(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)"
#define KPHONENUMBERREGLAR @"\\d{3}-\\d{8}|\\d{3}-\\d{7}|\\d{4}-\\d{8}|\\d{4}-\\d{7}|1+[3578]+\\d{9}|\\d{8}|\\d{7}"
#import "ZLStatus.h"
#import "ZLFrame.h"
#import "LxButton.h"
@interface ViewController ()<UITextViewDelegate>
@property(nonatomic,strong)ZLStatusTextView *textview;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.textview =[[ZLStatusTextView alloc]initWithFrame:CGRectMake(20, 100, 250, 200)];
ZLStatus *status = [[ZLStatus alloc]init];
status.text = kTempText;
ZLFrame *zlFrame =[[ZLFrame alloc]init];
zlFrame.frameX = self.textview.frame.origin.x;
zlFrame.frameY = self.textview.frame.origin.y;
zlFrame.maxWidth = self.textview.frame.size.width;
zlFrame.status = status;
self.textview.zlFrame = zlFrame;
//設(shè)置最大行數(shù)用于展開
self.textview.maxLine = 3;
self.textview.isShowAll = YES;
[self.view addSubview:self.textview];
self.textview.backgroundColor =[UIColor lightGrayColor];
LxButton *button =[LxButton LXButtonWithTitle:@"限制最大行數(shù)" titleFont:[UIFont systemFontOfSize:15] Image:nil backgroundImage:nil backgroundColor:[UIColor brownColor] titleColor:[UIColor blueColor] frame:CGRectMake(20, 40, 150, 40)];
[self.view addSubview:button];
__weak ViewController *weakSelf = self;
[button addClickBlock:^(UIButton *button) {
weakSelf.textview.isShowAll =!weakSelf.textview.isShowAll;
}];
}
@end