介绍两种方式
一. WKWebView
- 目标文本设置为<a>标签:
- 加行内样式
- 加自定义
scheme
如:
///文本是《隐私政策》
<a href='dear://product.com' style='color: #ff0000; text-decoration: none'>《隐私政策》</a>
//
- 拦截点击
#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
- 设置HTML文本, 如
NSString *string = @"<span style='color: #999999;'>亲爱的, 你那里下雪了吗<a href='dear://xryh.com' style='color: #007aff; text-decoration: none'>打电话</a><span>";
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc]initWithData:[string dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType} documentAttributes:nil error:nil];
注: UITextView
需要设置这两个属性:
editable = NO
selectable = YES
- 实现代理方法:
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange{
NSLog(@"url is %@",URL.absoluteString);
//处理事件
return NO;
}
注: 上面做法, 默认在iOS 9必须长按目标文字, 才会触发事件; iOS 10可以单击触发事件
- 实现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
- 如果图片过大, 则在textView中将无法展示, 此时需要设置
img
标签的size:
[NSString stringWithFormat:@"<head><style>img{width:%f !important;height:auto}</style></head>%@", textView.width, html]
网友评论