// 将HTML字符串转换为NSAttributedString对象
NSError *error = nil;
NSAttributedString *attributedText = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUTF8StringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType} documentAttributes:nil error:&error];
// 将NSAttributedString对象设置为UILabel的attributedText属性
label.attributedText = attributedText;
// 检测富文本字符串中的链接和电话号码,并为它们添加手势识别器
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)];
[label addGestureRecognizer:tapGestureRecognizer];
- (void)handleTapGesture:(UITapGestureRecognizer *)tapGestureRecognizer {
UILabel *label = (UILabel *)tapGestureRecognizer.view;
CGPoint tapLocation = [tapGestureRecognizer locationInView:label];
NSAttributedString *attributedText = label.attributedText;
NSUInteger characterIndex = [self characterIndexAtPoint:tapLocation attributedText:attributedText];
NSDictionary *attributes = [attributedText attributesAtIndex:characterIndex effectiveRange:nil];
NSURL *url = attributes[NSLinkAttributeName];
if (url) {
// 检测到链接,执行相应操作
if ([url.scheme isEqualToString:@"tel"]) {
// 拨打电话
NSString *phoneNumber = url.resourceSpecifier;
NSURL *telURL = [NSURL URLWithString:[NSString stringWithFormat:@"tel:%@", phoneNumber]];
[[UIApplication sharedApplication] openURL:telURL options:@{} completionHandler:nil];
} else {
// 打开链接
[[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
}
}
}
- (NSUInteger)characterIndexAtPoint:(CGPoint)point attributedText:(NSAttributedString *)attributedText {
NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];
NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX)];
[layoutManager addTextContainer:textContainer];
NSTextStorage *textStorage = [[NSTextStorage alloc] initWithAttributedString:attributedText];
[textStorage addLayoutManager:layoutManager];
NSUInteger characterIndex = [layoutManager characterIndexForPoint:point inTextContainer:textContainer fractionOfDistanceBetweenInsertionPoints:nil];
return characterIndex;
}
// 将 HTML 字符串转换为 NSAttributedString 对象
guard let htmlData = htmlString.data(using: .utf8) else { return }
let options: [NSAttributedString.DocumentReadingOptionKey: Any] = [.documentType: NSAttributedString.DocumentType.html, .characterEncoding: String.Encoding.utf8.rawValue]
let attributedText = try? NSAttributedString(data: htmlData, options: options, documentAttributes: nil)
// 将 NSAttributedString 对象设置为 UILabel 的 attributedText 属性
label.attributedText = attributedText
// 检测富文本字符串中的链接和电话号码,并为它们添加手势识别器
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleTapGesture(_:)))
label.addGestureRecognizer(tapGestureRecognizer)
@objc func handleTapGesture(_ tapGestureRecognizer: UITapGestureRecognizer) {
guard let label = tapGestureRecognizer.view as? UILabel, let attributedText = label.attributedText else { return }
let tapLocation = tapGestureRecognizer.location(in: label)
let characterIndex = characterIndex(at: tapLocation, attributedText: attributedText)
let attributes = attributedText.attributes(at: characterIndex, effectiveRange: nil)
if let url = attributes[.link] as? URL {
// 检测到链接,执行相应操作
if url.scheme == "tel" {
// 拨打电话
let phoneNumber = url.resourceSpecifier
if let telURL = URL(string: "tel:\(phoneNumber)"), UIApplication.shared.canOpenURL(telURL) {
UIApplication.shared.open(telURL, options: [:], completionHandler: nil)
}
} else {
// 打开链接
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
}
}
}
func characterIndex(at point: CGPoint, attributedText: NSAttributedString) -> Int {
let textStorage = NSTextStorage(attributedString: attributedText)
let layoutManager = NSLayoutManager()
textStorage.addLayoutManager(layoutManager)
let textContainer = NSTextContainer(size: CGSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude))
layoutManager.addTextContainer(textContainer)
textContainer.lineFragmentPadding = 0
textContainer.maximumNumberOfLines = label.numberOfLines
let glyphIndex = layoutManager.glyphIndex(for: point, in: textContainer)
return layoutManager.characterIndexForGlyph(at: glyphIndex)
}
网友评论