iOS 计算富文本,检索网址,号码,表情,并且计算高度,设置最大行数(聊天表情计算)

  1.   

前言:项目中用到检索表情,网址与号码,但是看了TTTAttributeLabel,emojyLabel,奈何都不太满意,plist格式不太符合,而且这两个第三方用到检索都是系统自带的检索,检测网址方面不准确, 所以就需要自己使用正则进行检索。

关于以上两个三方检索不准确的可以参考:检索网址

接下来写一下实现的过程, 没有高度封装,仅供参考

关于网址与号码的正则再说明下:

网址: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\.\-~!@#$%^&*+?:_/=<>]*)?)"

号码: KPHONENUMBERREGLAR @"\d{3}-\d{8}|\d{4}-\d{7}|\d{11}"

比如我要转的字符串为

@"简书:http://jianshu.com哈哈哈[调皮][流汗][偷笑][再见][可爱][色][害羞][委屈][委屈][抓狂][酷][酷][嘘][嘘][龇牙][大哭][大哭][大哭][龇牙][嘘][嘘][调皮][调皮]哈哈哈哈[嘘][调皮][调皮]18637963241他大舅他二舅都是舅,高桌子地板头都是木头"

我需要做的是检索网址并且替换为和微博一样的链接,号码和链接有选中状态,因为UITextview有检测url 的方法可以添加点击事件,所以就采用UITextview进行封装。主要采取正则表达式与RegexKitLite配合做检索

1 . 首先建立一个模型,把文字检索为富文本,检索出 表情,网址以及号码。中间需要使用一个模型存储检索出来的结果。对于特殊符号的表情建立一个模型。其实富文本的都是逐个检索,然后处理,最后拼接为一个NSMutableAttributedString。

  
  
[objc] view plain copy
  1. #import <Foundation/Foundation.h>  
  2. #import <UIKit/UIKit.h>  
  3. @interface ZLStatus : NSObject  
  4. //源内容  
  5. @property (nonatomiccopyNSString *text;  
  6.   
  7. /**    string    信息内容 -- 带有属性的(特殊文字会高亮显示\显示表情)*/  
  8. @property (nonatomiccopyNSAttributedString *attributedText;  
  9.   
  10. @end  
  11. #import "ZLStatus.h"  
  12. #import "ZLSpecial.h"  
  13. #import "ZLTextPart.h"  
  14. #import "RegexKitLite.h"  
  15. @implementation ZLStatus  
  16. - (void)setText:(NSString *)text  
  17. {  
  18.     _text = [text copy];  
  19.   
  20.     // 利用text生成attributedText  
  21.     self.attributedText = [self attributedTextWithText:text];  
  22. }  
  23. /** 
  24.  *  普通文字 --> 属性文字 
  25.  * 
  26.  *  @param text 普通文字 
  27.  * 
  28.  *  @return 属性文字 
  29.  */  
  30. - (NSAttributedString *)attributedTextWithText:(NSString *)text  
  31. {  
  32.     NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] init];  
  33.   
  34.     // 表情的规则  
  35.     NSString *emotionPattern = @"\\[[0-9a-zA-Z\\u4e00-\\u9fa5]+\\]";  
  36.     // @的规则  
  37.     NSString *atPattern = @"@[0-9a-zA-Z\\u4e00-\\u9fa5-_]+";  
  38.     // #话题#的规则  
  39.     NSString *topicPattern = @"#[0-9a-zA-Z\\u4e00-\\u9fa5]+#";  
  40.     // url链接的规则  
  41.     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\\.\\-~!@#$%^&*+?:_/=<>]*)?)";  
  42.     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}"  
  43.     ;  
  44.     NSString *pattern = [NSString stringWithFormat:@"%@|%@|%@|%@|%@", emotionPattern, atPattern, topicPattern, urlPattern,phoneNumber];  
  45.   
  46.     // 遍历所有的特殊字符串  
  47.     NSMutableArray *parts = [NSMutableArray array];  
  48.     [text enumerateStringsMatchedByRegex:pattern usingBlock:^(NSInteger captureCount, NSString *const __unsafe_unretained *capturedStrings, const NSRange *capturedRanges, volatile BOOLBOOL *const stop) {  
  49.         if ((*capturedRanges).length == 0return;  
  50.   
  51.         ZLTextPart *part = [[ZLTextPart alloc] init];  
  52.         part.special = YES;  
  53.         part.text = *capturedStrings;  
  54.         part.emotion = [part.text hasPrefix:@"["] && [part.text hasSuffix:@"]"];  
  55.         part.range = *capturedRanges;  
  56.         [parts addObject:part];  
  57.     }];  
  58.     // 遍历所有的非特殊字符  
  59.     [text enumerateStringsSeparatedByRegex:pattern usingBlock:^(NSInteger captureCount, NSString *const __unsafe_unretained *capturedStrings, const NSRange *capturedRanges, volatile BOOLBOOL *const stop) {  
  60.         if ((*capturedRanges).length == 0return;  
  61.   
  62.         ZLTextPart *part = [[ZLTextPart alloc] init];  
  63.         part.text = *capturedStrings;  
  64.         part.range = *capturedRanges;  
  65.         [parts addObject:part];  
  66.     }];  
  67.   
  68.     // 排序  
  69.     // 系统是按照从小 -> 大的顺序排列对象  
  70.     [parts sortUsingComparator:^NSComparisonResult(ZLTextPart *part1ZLTextPart *part2) {  
  71.         // NSOrderedAscending = -1L, NSOrderedSame, NSOrderedDescending  
  72.         // 返回NSOrderedSame:两个一样大  
  73.         // NSOrderedAscending(升序):part2>part1  
  74.         // NSOrderedDescending(降序):part1>part2  
  75.         if (part1.range.location > part2.range.location) {  
  76.             // part1>part2  
  77.             // part1放后面, part2放前面  
  78.             return NSOrderedDescending;  
  79.         }  
  80.         // part1<part2  
  81.         // part1放前面, part2放后面  
  82.         return NSOrderedAscending;  
  83.     }];  
  84.   
  85.     UIFont *font = [UIFont systemFontOfSize:15];  
  86.     NSMutableArray *specials = [NSMutableArray array];  
  87.     NSString *filePath = [[NSBundle mainBundle] pathForResource:@"face.plist" ofType:nil];  
  88.     NSArray  *face = [NSArray arrayWithContentsOfFile:filePath];  
  89.     // 按顺序拼接每一段文字  
  90.     for (ZLTextPart *part in parts) {  
  91.         // 等会需要拼接的子串  
  92.         NSAttributedString *substr = nil;  
  93.         if (part.isEmotion) { // 表情  表情处理的时候,需要根据你自己的plist进行单独处理,像一些第三方里自定义的plist,格式要是也是很严格的  
  94.             NSString *str = [text substringWithRange:part.range];  
  95.             for (int i = 0; i < face.count; i ++) {  
  96.                 if ([face[i][@"face_name"] isEqualToString:str]) {  
  97.                     //face[i][@"png"]就是我们要加载的图片  
  98.                     //新建文字附件来存放我们的图片,iOS7才新加的对象  
  99.                     NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];  
  100.                     //给附件添加图片  
  101.                     textAttachment.image = [UIImage imageNamed:face[i][@"face_image_name"]];  
  102.                     //调整一下图片的位置,如果你的图片偏上或者偏下,调整一下bounds的y值即可  
  103.                     textAttachment.bounds = CGRectMake(0, -62525);  
  104.                     //把附件转换成可变字符串,用于替换掉源字符串中的表情文字  
  105.                    substr = [NSAttributedString attributedStringWithAttachment:textAttachment];  
  106.   
  107.                     break;  
  108.                 }  
  109.             }  
  110.   
  111.         } else if (part.special) { // 非表情的特殊文字  
  112.             NSURL *url =[NSURL URLWithString:part.text];  
  113.             if (url.scheme) {  
  114.                 substr = [[NSAttributedString alloc] initWithString:part.text attributes:@{  
  115.                                                                                            NSForegroundColorAttributeName : [UIColor redColor]  
  116.                                                                                            }];  
  117.   
  118.                NSString *string =@"网页链接";  
  119.                             NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];  
  120.                                 //给附件添加图片  
  121.                                 textAttachment.image = [UIImage imageNamed:@"链接"];  
  122.                                 //调整一下图片的位置,如果你的图片偏上或者偏下,调整一下bounds的y值即可  
  123.                                 textAttachment.bounds = CGRectMake(0, -62525);  
  124.                 NSMutableAttributedString *tempAttribute = [[NSMutableAttributedString alloc]initWithAttributedString:[NSAttributedString attributedStringWithAttachment:textAttachment]];  
  125.                  NSMutableAttributedString *tempAttribute2 = [[NSMutableAttributedString alloc]initWithAttributedString:[NSAttributedString attributedStringWithAttachment:textAttachment]];  
  126.                 NSAttributedString *text =[[NSAttributedString alloc]initWithString:string attributes:@{NSForegroundColorAttributeName:[UIColor blueColor]}];  
  127.                 [tempAttribute appendAttributedString:text];  
  128.                 substr = [[NSAttributedString alloc]initWithAttributedString:tempAttribute];  
  129.                 // 创建特殊对象  
  130.   
  131.                 ZLSpecial *s = [[ZLSpecial alloc] init];  
  132.                 s.text = string;  
  133.               //需要添加附属图片的长度,负责点击范围会变化  
  134.                 NSUInteger loc = attributedText.length+tempAttribute2.length;  
  135.                 NSUInteger len = string.length;  
  136.                 s.range = NSMakeRange(loc, len);  
  137.                 s.urlString = part.text;  
  138.                 [specials addObject:s];  
  139.             }else{  
  140.   
  141.                 NSLog(@"%@",part.text);  
  142.                 substr = [[NSAttributedString alloc] initWithString:part.text attributes:@{  
  143.                                                                                            NSForegroundColorAttributeName : [UIColor redColor]  
  144.                                                                                            }];  
  145.                 // 创建特殊对象  
  146.                 ZLSpecial *s = [[ZLSpecial alloc] init];  
  147.                 s.text = part.text;  
  148.                 NSUInteger loc = attributedText.length;  
  149.                 NSUInteger len = part.text.length;  
  150.                 s.range = NSMakeRange(loc, len);  
  151.                 s.urlString = part.text;  
  152.                 [specials addObject:s];  
  153.             }  
  154.   
  155.   
  156.   
  157.         } else { // 非特殊文字  
  158.             substr = [[NSAttributedString alloc] initWithString:part.text];  
  159.         }  
  160.         [attributedText appendAttributedString:substr];  
  161.     }  
  162.   
  163.     // 一定要设置字体,保证计算出来的尺寸是正确的  
  164.     [attributedText addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, attributedText.length)];  
  165.     [attributedText addAttribute:@"specials" value:specials range:NSMakeRange(01)];  
  166.   
  167.     return attributedText;  
  168. }  

中间存储检索结果与特殊字符的model:
  1. #import <Foundation/Foundation.h>  
  2.   
  3. @interface ZLTextPart : NSObject  
  4. /** 这段文字的内容 */  
  5. @property (nonatomiccopyNSString *text;  
  6. /** 这段文字的范围 */  
  7. @property (nonatomic, assign) NSRange range;  
  8. /** 是否为特殊文字 */  
  9. @property (nonatomic, assign, getter = isSpecical) BOOL special;  
  10. /** 是否为表情 */  
  11. @property (nonatomic, assign, getter = isEmotion) BOOL emotion;  
  12.   
  13.   
  14. @end  
  15. #import <Foundation/Foundation.h>  
  16.   
  17. @interface ZLSpecial : NSObject  
  18. /** 这段特殊文字的内容 */  
  19. @property (nonatomiccopyNSString *text;  
  20. /** 这段特殊文字的范围 */  
  21. @property (nonatomic, assign) NSRange range;  
  22. @property(nonatomic,copy)NSString *urlString;  
  1. 设置完内容后我们需要计算内容高度,然后复制给Textview,建立一个Frame模型。  
  2. <pre name="code" class="objc">#import <Foundation/Foundation.h>  
  3. #import "ZLStatus.h"  
  4. @interface ZLFrame : NSObject  
  5.   
  6. //设置  
  7.   
  8. /** 限制最大行数  这一步必须在设置完contentLabelF之后设置*/  
  9. @property(nonatomic,assign)int  maxNumLine;  
  10. @property(nonatomic,strong)ZLStatus *status;  
  11. @property(nonatomic,assign)CGFloat frameX;  
  12. @property(nonatomic,assign)CGFloat frameY;  
  13. @property(nonatomic,assign)CGFloat maxWidth;  
  14. //取值  
  15. /**   */  
  16. /** 正文 */  
  17. @property (nonatomic, assign) CGRect contentLabelF;  
  18. @property(nonatomic,assign)CGRect   maxNumLabelF;  
  19.   
  20. @end  
  21. #import "ZLFrame.h"  
  22. @interface ZLFrame()  
  23. //检测高度的label;  
  24. @property(nonatomic,strong)UILabel *templateLabel;  
  25. @end  
  26. @implementation ZLFrame  
  27. -(void)setStatus:(ZLStatus *)status{  
  28.     _status = status;  
  29.     CGSize contentSize = [status.attributedText boundingRectWithSize:CGSizeMake(self.maxWidth, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin context:nil].size;  
  30.     self.contentLabelF = (CGRect){{self.frameX , self.frameY}, contentSize};  
  31.   
  32. }  
  33. -(void)setMaxNumLine:(int)maxNumLine{  
  34.     _maxNumLine = maxNumLine;  
  35.   
  36.     self.templateLabel.frame =CGRectMake(self.frameXself.frameYself.maxWidth0);  
  37.     self.templateLabel.attributedText = self.status.attributedText;  
  38.     self.templateLabel.numberOfLines = maxNumLine;  
  39.     [self.templateLabel sizeToFit];  
  40.     self.maxNumLabelF = self.templateLabel.frame;  
  41.   
  42. }  
  43. -(void)setFrameX:(CGFloat)frameX{  
  44.     _frameX = frameX;  
  45. }  
  46. -(void)setFrameY:(CGFloat)frameY{  
  47.     _frameY = frameY;  
  48. }  
  49. -(void)setMaxWidth:(CGFloat)maxWidth{  
  50.     _maxWidth = maxWidth;  
  51. }  
  52.   
  53. -(UILabel *)templateLabel{  
  54.     if (!_templateLabel) {  
  55.         _templateLabel =[[UILabel alloc]init];  
  56.   
  57.     }  
  58.     return _templateLabel;  
  59. }  
  60. @end</pre><br>  
  61. <br>  
  62. <pre></pre>  
  63. <br>  
  64. <br>  
最后自定义Textview,引入Frame模型
  1. #import <UIKit/UIKit.h>  
  2. #import "ZLFrame.h"  
  3. @interface ZLStatusTextView : UITextView  
  4. /** 所有的特殊字符串(里面存放着HWSpecial) */  
  5. @property (nonatomicstrongNSArray *specials;  
  6. @property(nonatomic,strong)ZLFrame *zlFrame;  
  7. @property(nonatomic,assign)int maxLine;  
  8. @property(nonatomic,assign)BOOL isShowAll;//是否全部显示  
  9. @end  
  10. #import "ZLStatusTextView.h"  
  11. #import "ZLSpecial.h"  
  12. #define ZLStatusTextViewCoverTag 999  
  13. @implementation ZLStatusTextView  
  14. - (id)initWithFrame:(CGRect)frame  
  15. {  
  16.     self = [super initWithFrame:frame];  
  17.     if (self) {  
  18.         self.backgroundColor = [UIColor clearColor];  
  19.         self.editable = NO;  
  20.         self.textContainerInset = UIEdgeInsetsMake(0, -50, -5);  
  21.         // 禁止滚动, 让文字完全显示出来  
  22.         self.scrollEnabled = NO;  
  23.     }  
  24.     return self;  
  25. }  
  26. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event  
  27. {  
  28.     // 触摸对象  
  29.     UITouch *touch = [touches anyObject];  
  30.   
  31.     // 触摸点  
  32.     CGPoint point = [touch locationInView:self];  
  33.   
  34.     NSArray *specials = [self.attributedText attribute:@"specials" atIndex:0 effectiveRange:NULL];  
  35.     BOOL contains = NO;  
  36.   
  37.     for (ZLSpecial *special in specials) {  
  38.         self.selectedRange = special.range;  
  39.         // self.selectedRange --影响--> self.selectedTextRange  
  40.         // 获得选中范围的矩形框  
  41.         NSArray *rects = [self selectionRectsForRange:self.selectedTextRange];  
  42.         // 清空选中范围  
  43.         self.selectedRange = NSMakeRange(00);  
  44.   
  45.         for (UITextSelectionRect *selectionRect in rects) {  
  46.             CGRect rect = selectionRect.rect;  
  47.             if (rect.size.width == 0 || rect.size.height == 0continue;  
  48.   
  49.             if (CGRectContainsPoint(rect, point)) { // 点中了某个特殊字符串  
  50.                 contains = YES;  
  51.                 break;  
  52.             }  
  53.         }  
  54.   
  55.         if (contains) {  
  56.             for (UITextSelectionRect *selectionRect in rects) {  
  57.                 CGRect rect = selectionRect.rect;  
  58.                 if (rect.size.width == 0 || rect.size.height == 0continue;  
  59.   
  60.                 UIView *cover = [[UIView alloc] init];  
  61.                 cover.backgroundColor = [UIColor greenColor];  
  62.                 cover.frame = rect;  
  63.                 cover.tag = ZLStatusTextViewCoverTag;  
  64.                 cover.layer.cornerRadius = 5;  
  65.                 [self insertSubview:cover atIndex:0];  
  66.             }  
  67.   
  68.             break;  
  69.         }  
  70.     }  
  71.   
  72.     // 在被触摸的特殊字符串后面显示一段高亮的背景  
  73. }  
  74. - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event  
  75. {  
  76.     dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{  
  77.         // 触摸对象  
  78.         UITouch *touch = [touches anyObject];  
  79.   
  80.         // 触摸点  
  81.         CGPoint point = [touch locationInView:self];  
  82.   
  83.         NSArray *specials = [self.attributedText attribute:@"specials" atIndex:0 effectiveRange:NULL];  
  84.         BOOL contains = NO;  
  85.   
  86.         for (ZLSpecial *special in specials) {  
  87.             self.selectedRange = special.range;  
  88.             // self.selectedRange --影响--> self.selectedTextRange  
  89.             // 获得选中范围的矩形框  
  90.             NSArray *rects = [self selectionRectsForRange:self.selectedTextRange];  
  91.             // 清空选中范围  
  92.             self.selectedRange = NSMakeRange(00);  
  93.   
  94.             for (UITextSelectionRect *selectionRect in rects) {  
  95.                 CGRect rect = selectionRect.rect;  
  96.                 if (rect.size.width == 0 || rect.size.height == 0continue;  
  97.   
  98.                 if (CGRectContainsPoint(rect, point)) { // 点中了某个特殊字符串  
  99.                     contains = YES;  
  100.                     break;  
  101.                 }  
  102.             }  
  103.   
  104.             if (contains) {  
  105.                 for (UITextSelectionRect *selectionRect in rects) {  
  106.                     CGRect rect = selectionRect.rect;  
  107.                     if (rect.size.width == 0 || rect.size.height == 0continue;  
  108.   
  109.                     if (special.urlString) {  
  110.                         NSString *urlStr = special.urlString;  
  111.                         NSURL *url =[NSURL URLWithString:urlStr];  
  112.                         if (url.scheme) {  
  113.                             [[UIApplication sharedApplication]openURL:url];  
  114.                         }else{  
  115.                             NSMutableString *str=[[NSMutableString alloc] initWithFormat:@"tel:%@",special.text];  
  116.                             UIWebView *callWebview = [[UIWebView alloc] init];  
  117.                             [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];  
  118.                             [self addSubview:callWebview];  
  119.                     }  
  120.                     }  
  121.                 }  
  122.   
  123.                 break;  
  124.             }  
  125.         }  
  126.   
  127.         [self touchesCancelled:touches withEvent:event];  
  128.     });  
  129. }  
  130.   
  131. - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event  
  132. {  
  133.     // 去掉特殊字符串后面的高亮背景  
  134.     for (UIView *child in self.subviews) {  
  135.         if (child.tag == ZLStatusTextViewCoverTag) [child removeFromSuperview];  
  136.     }  
  137. }  
  138. -(void)setZlFrame:(ZLFrame *)zlFrame{  
  139.     _zlFrame = zlFrame;  
  140.     self.attributedText = zlFrame.status.attributedText;  
  141.     self.frame = zlFrame.contentLabelF;  
  142.   
  143.   
  144. }  
  145. -(void)setMaxLine:(int)maxLine{  
  146.     _maxLine = maxLine;  
  147.     [self.zlFrame setMaxNumLine:maxLine];  
  148.     self.frame = self.zlFrame.maxNumLabelF;  
  149. }  
  150. -(void)setIsShowAll:(BOOL)isShowAll{  
  151.     _isShowAll = isShowAll;  
  152.     if (isShowAll) {  
  153.         self.frame = self.zlFrame.contentLabelF;  
  154.   
  155.     }else{  
  156.         [self setMaxLine:3];  
  157.     }  
  158. }  
  159. @end  

最后在ViewController里引入即可:

   
   
[objc] view plain copy
  1. #import "ViewController.h"  
  2. #import "ZLStatusTextView.h"  
  3. #define kTempText  @"简书:http://jianshu.com哈哈哈[调皮][流汗][偷笑][再见][可爱][色][害羞][委屈][委屈][抓狂][酷][酷][嘘][嘘][龇牙][大哭][大哭][大哭][龇牙][嘘][嘘][调皮][调皮]哈哈哈哈[嘘][调皮][调皮]18637963241他大舅他二舅都是舅,高桌子地板头都是木头"  
  4. #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\\.\\-~!@#$%^&*+?:_/=<>]*)?)"  
  5. #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}"  
  6. #import "ZLStatus.h"  
  7. #import "ZLFrame.h"  
  8. #import "LxButton.h"  
  9. @interface ViewController ()<UITextViewDelegate>  
  10. @property(nonatomic,strong)ZLStatusTextView *textview;  
  11. @end  
  12.   
  13. @implementation ViewController  
  14.   
  15. - (void)viewDidLoad {  
  16.     [super viewDidLoad];  
  17.     // Do any additional setup after loading the view, typically from a nib.  
  18.   
  19.     self.textview =[[ZLStatusTextView alloc]initWithFrame:CGRectMake(20100250200)];  
  20.     ZLStatus *status = [[ZLStatus alloc]init];  
  21.     status.text = kTempText;  
  22.   
  23.   
  24.     ZLFrame *zlFrame =[[ZLFrame alloc]init];  
  25.     zlFrame.frameX = self.textview.frame.origin.x;  
  26.     zlFrame.frameY = self.textview.frame.origin.y;  
  27.     zlFrame.maxWidth = self.textview.frame.size.width;  
  28.     zlFrame.status = status;  
  29.   
  30.     self.textview.zlFrame = zlFrame;  
  31.     //设置最大行数用于展开  
  32.     self.textview.maxLine = 3;  
  33.     self.textview.isShowAll = YES;  
  34.     [self.view addSubview:self.textview];  
  35.     self.textview.backgroundColor =[UIColor lightGrayColor];  
  36.   
  37.   
  38.     LxButton *button =[LxButton LXButtonWithTitle:@"限制最大行数" titleFont:[UIFont systemFontOfSize:15] Image:nil backgroundImage:nil backgroundColor:[UIColor brownColor] titleColor:[UIColor blueColor] frame:CGRectMake(204015040)];  
  39.   
  40.     [self.view addSubview:button];  
  41.     __weak ViewController *weakSelf = self;  
  42.     [button addClickBlock:^(UIButton *button) {  
  43.   
  44.         weakSelf.textview.isShowAll =!weakSelf.textview.isShowAll;  
  45.     }];  
  46.   
  47. }  
  48.   
  49.   
  50. @end  

demo地址:富文本检索表情,网址,替换链接,限制最大输入行





链接:http://www.jianshu.com/p/77c3b27f8858
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值