浏览代码

修正网页和号码识别的处理

heavyrain2012 6 年之前
父节点
当前提交
d9c154af92

+ 7 - 0
wfuikit/WFChatUIKit/Category/AttributedLabel.h

@@ -8,6 +8,13 @@
 
 #import <UIKit/UIKit.h>
 
+@protocol AttributedLabelDelegate <NSObject>
+@optional
+- (void)didSelectUrl:(NSString *)urlString;
+- (void)didSelectPhoneNumber:(NSString *)phoneNumberString;
+@end
+
 @interface AttributedLabel : UILabel
+@property(nonatomic, weak)id<AttributedLabelDelegate> attributedLabelDelegate;
 - (void)setText:(NSString *)text;
 @end

+ 37 - 0
wfuikit/WFChatUIKit/Category/AttributedLabel.m

@@ -31,6 +31,23 @@
             NSInteger i=[self.rangeArray indexOfObject:value];
             NSString *str = self.stringArray[i];
             NSLog(@"touch url %@", str);
+            
+            NSString *pattern =@"[0-9]{5,12}";
+            
+            
+            NSPredicate*pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",pattern];
+            
+            BOOL isNumber = [pred evaluateWithObject:str];
+            
+            if (isNumber) {
+                if ([self.attributedLabelDelegate respondsToSelector:@selector(didSelectPhoneNumber:)]) {
+                    [self.attributedLabelDelegate didSelectPhoneNumber:str];
+                }
+            } else {
+                if ([self.attributedLabelDelegate respondsToSelector:@selector(didSelectUrl:)]) {
+                    [self.attributedLabelDelegate didSelectUrl:str];
+                }
+            }
         }
     }
     [super touchesBegan:touches withEvent:event];
@@ -65,6 +82,25 @@
         [rangeArr addObject:[self rangesOfString:str inString:subStr]];
     }
     
+    NSString *pattern =@"[0-9]{5,11}";
+    regex = [NSRegularExpression regularExpressionWithPattern:pattern
+                                                      options:NSRegularExpressionCaseInsensitive
+                                                        error:&error];
+    
+    arrayOfAllMatches = [regex matchesInString:string options:0 range:NSMakeRange(0, [string length])];
+    
+    for (NSTextCheckingResult *match in arrayOfAllMatches) {
+        NSString* substringForMatch;
+        substringForMatch = [string substringWithRange:match.range];
+        [arr addObject:substringForMatch];
+    }
+    
+    subStr=string;
+    for (NSString *str in arr) {
+        [rangeArr addObject:[self rangesOfString:str inString:subStr]];
+    }
+    
+    
     NSMutableAttributedString *attributedText;
     attributedText=[[NSMutableAttributedString alloc]initWithString:subStr attributes:@{NSFontAttributeName :self.font}];
     
@@ -74,6 +110,7 @@
         [attributedText addAttribute:NSLinkAttributeName value:[NSURL URLWithString:[arr objectAtIndex:index]] range:range];
         [attributedText addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:range];
     }
+    
     return attributedText;
 }
 

+ 13 - 0
wfuikit/WFChatUIKit/CommonVC/WFCUBrowserViewController.m

@@ -20,6 +20,12 @@
     self.webView = [[WKWebView alloc] initWithFrame:self.view.bounds];
     [self.view addSubview:self.webView];
     [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.url]]];
+    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"..." style:UIBarButtonItemStyleDone target:self action:@selector(onRightBtn:)];
+}
+
+- (void)onRightBtn:(id)sender {
+    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"浏览器打开" otherButtonTitles:nil, nil];
+    [actionSheet showInView:self.view];
 }
 
 - (void)didReceiveMemoryWarning {
@@ -37,4 +43,11 @@
 }
 */
 
+#pragma mark -  UIActionSheetDelegate <NSObject>
+- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
+    if(buttonIndex == 0) {
+        [[UIApplication sharedApplication] openURL:[[NSURL alloc] initWithString:self.url]];
+        [self.navigationController popViewControllerAnimated:NO];
+    }
+}
 @end

+ 3 - 1
wfuikit/WFChatUIKit/MessageList/Cell/WFCUMessageCell.m

@@ -283,8 +283,10 @@
     if (!_bubbleView) {
         _bubbleView = [[UIImageView alloc] init];
         [self.contentView addSubview:_bubbleView];
-        [_bubbleView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTaped:)]];
+        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTaped:)];
+        [_bubbleView addGestureRecognizer:tap];
         [_bubbleView addGestureRecognizer:[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(onLongPressed:)]];
+        tap.cancelsTouchesInView = NO;
         [_bubbleView setUserInteractionEnabled:YES];
     }
     return _bubbleView;

+ 3 - 0
wfuikit/WFChatUIKit/MessageList/Cell/WFCUMessageCellBase.h

@@ -17,6 +17,9 @@
 - (void)didLongPressMessageCell:(WFCUMessageCellBase *)cell withModel:(WFCUMessageModel *)model;
 - (void)didLongPressMessagePortrait:(WFCUMessageCellBase *)cell withModel:(WFCUMessageModel *)model;
 - (void)didTapResendBtn:(WFCUMessageModel *)model;
+
+- (void)didSelectUrl:(WFCUMessageCellBase *)cell withModel:(WFCUMessageModel *)model withUrl:(NSString *)urlString;
+- (void)didSelectPhoneNumber:(WFCUMessageCellBase *)cell withModel:(WFCUMessageModel *)model withPhoneNumber:(NSString *)phoneNumber;
 @end
 
 @interface WFCUMessageCellBase : UICollectionViewCell

+ 14 - 0
wfuikit/WFChatUIKit/MessageList/Cell/WFCUTextCell.m

@@ -14,6 +14,10 @@
 #define TEXT_LABEL_TOP_PADDING 3
 #define TEXT_LABEL_BUTTOM_PADDING 5
 
+@interface WFCUTextCell () <AttributedLabelDelegate>
+
+@end
+
 @implementation WFCUTextCell
 + (CGSize)sizeForClientArea:(WFCUMessageModel *)msgModel withViewWidth:(CGFloat)width {
   WFCCTextMessageContent *txtContent = (WFCCTextMessageContent *)msgModel.message.content;
@@ -43,10 +47,20 @@
 - (UILabel *)textLabel {
     if (!_textLabel) {
         _textLabel = [[AttributedLabel alloc] init];
+        ((AttributedLabel*)_textLabel).attributedLabelDelegate = self;
         _textLabel.numberOfLines = 0;
         _textLabel.font = [UIFont systemFontOfSize:18];
+        _textLabel.userInteractionEnabled = YES;
         [self.contentArea addSubview:_textLabel];
     }
     return _textLabel;
 }
+
+#pragma mark - AttributedLabelDelegate
+- (void)didSelectUrl:(NSString *)urlString {
+    [self.delegate didSelectUrl:self withModel:self.model withUrl:urlString];
+}
+- (void)didSelectPhoneNumber:(NSString *)phoneNumberString {
+    [self.delegate didSelectPhoneNumber:self withModel:self.model withPhoneNumber:phoneNumberString];
+}
 @end

+ 33 - 1
wfuikit/WFChatUIKit/MessageList/ViewController/WFCUMessageListViewController.m

@@ -20,7 +20,7 @@
 #import "WFCUCallSummaryCell.h"
 #import "WFCUStickerCell.h"
 #import "WFCUVideoCell.h"
-
+#import "WFCUBrowserViewController.h"
 #import <WFChatClient/WFCChatClient.h>
 #import "WFCUProfileTableViewController.h"
 
@@ -1002,6 +1002,38 @@
         [self sendMessage:model.message.content];
     }
 }
+
+- (void)didSelectUrl:(WFCUMessageCellBase *)cell withModel:(WFCUMessageModel *)model withUrl:(NSString *)urlString {
+    WFCUBrowserViewController *bvc = [[WFCUBrowserViewController alloc] init];
+    bvc.url = urlString;
+    [self.navigationController pushViewController:bvc animated:YES];
+}
+
+- (void)didSelectPhoneNumber:(WFCUMessageCellBase *)cell withModel:(WFCUMessageModel *)model withPhoneNumber:(NSString *)phoneNumber {
+    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:[NSString stringWithFormat:@"我猜%@是一个电话号码", phoneNumber] message:nil preferredStyle:UIAlertControllerStyleActionSheet];
+    
+    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
+    UIAlertAction *callAction = [UIAlertAction actionWithTitle:@"呼叫" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
+        NSURL *url = [[NSURL alloc] initWithString:[NSString stringWithFormat:@"telprompt:%@", phoneNumber]];
+        [[UIApplication sharedApplication] openURL:url];
+    }];
+    
+    UIAlertAction *copyAction = [UIAlertAction actionWithTitle:@"复制号码" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
+        UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
+        pasteboard.string = phoneNumber;
+    }];
+    
+//    UIAlertAction *addContactAction = [UIAlertAction actionWithTitle:@"添加到通讯录" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
+//
+//
+//    }];
+    
+    [alertController addAction:cancelAction];
+    [alertController addAction:callAction];
+    [alertController addAction:copyAction];
+//    [alertController addAction:addContactAction];
+    [self presentViewController:alertController animated:YES completion:nil];
+}
 #pragma mark - AVAudioPlayerDelegate
 - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag {
     NSLog(@"player finished");