iOS 富文本及拦截富文本点击事件

介绍两种方式

一. WKWebView

  1. 目标文本设置为标签:
  • 加行内样式
  • 加自定义scheme
    如:
///文本是《隐私政策》
《隐私政策》
//
  1. 拦截点击
#pragma mark - WKNavigationDelegate
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{

    NSString *scheme = [navigationAction.request.URL scheme];
    if ([scheme isEqualToString:@"dear"]) {
        //此处添加事件
        decisionHandler(WKNavigationActionPolicyCancel);
        return;
    }
    decisionHandler(WKNavigationActionPolicyAllow);
}

二. UITextView

不需要计算位置, 也是利用HTML

  1. 设置HTML文本, 如
NSString *string = @"亲爱的, 你那里下雪了吗打电话";
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc]initWithData:[string dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType} documentAttributes:nil error:nil];

: UITextView需要设置这两个属性:

editable = NO
selectable = YES
  1. 实现代理方法:
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange{
    NSLog(@"url is %@",URL.absoluteString);
  //处理事件
    return NO;
}

: 上面做法, 默认在iOS 9必须长按目标文字, 才会触发事件; iOS 10可以单击触发事件

  1. 实现iOS 9 单击可触发事件, 简单封装一个textView


#import "YHEventTextView.h"

@interface YHEventTextView()

/// 触发事件的字符串
@property (nonatomic, copy) NSString *eventString;

@end

@implementation YHEventTextView

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self initialise];
    }
    return self;
}

- (void)initialise{
    if ([UIDevice currentDevice].systemVersion.floatValue <10.0) {
        //解决ios9 点击不响应, 必须长按才响应事件
        UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)];
        [self addGestureRecognizer:tapRecognizer];
    }

}

#pragma mark --- tap
- (void)tapped:(UIGestureRecognizer *)ges{
    if ([ges isKindOfClass:[UITapGestureRecognizer class]]) {
        UITextView* textView = (UITextView *)ges.view;
        CGPoint tapLocation = [gestureRecognizer locationInView:textView];
        UITextPosition *textPosition = [textView closestPositionToPoint:tapLocation];
        NSDictionary *atts = [textView textStylingAtPosition:textPosition inDirection:UITextStorageDirectionForward];
        NSURL *url = atts[NSLinkAttributeName];
        if(url) {
            NSRange range = [self.text rangeOfString:self.eventString];
            if ([self.delegate conformsToProtocol:@protocol(UITextViewDelegate)] && [self.delegate respondsToSelector:@selector(textView:shouldInteractWithURL:inRange:)]) {
                [self.delegate textView:self shouldInteractWithURL:url inRange:range];
            }
        }
    }
}


#pragma mark --- public method
+ (instancetype)yh_eventTextViewWithEventText:(NSString *)eventText{
    YHEventTextView *textView = [[self alloc] init];
    textView.eventString = eventText;
    return textView;
}

@end
  1. 如果图片过大, 则在textView中将无法展示, 此时需要设置img标签的size:
[NSString stringWithFormat:@"%@", textView.width, html]

以上

你可能感兴趣的:(iOS 富文本及拦截富文本点击事件)