[英]Detect Link or URL in ASTextNode AsyncDisplayKit
我一直在尝试使用AsyncDisplayKit
框架,我需要检测文本中的url。
我使用过ASTextNode
但找不到任何api来检测链接。
我读到有用于url检测的属性linkAttributeNames
但无法找到任何示例如何执行此操作。
有人可以帮助我如何使用上述课程吗?
谢谢
对于Swift 3.0
func addLinkDetection(_ text: String, highLightColor: UIColor, delegate: ASTextNodeDelegate) {
self.isUserInteractionEnabled = true
self.delegate = delegate
let types: NSTextCheckingResult.CheckingType = [.link]
let detector = try? NSDataDetector(types: types.rawValue)
let range = NSMakeRange(0, text.characters.count)
if let attributedText = self.attributedText {
let mutableString = NSMutableAttributedString()
mutableString.append(attributedText)
detector?.enumerateMatches(in: text, range: range) {
(result, _, _) in
if let fixedRange = result?.range {
mutableString.addAttribute(NSUnderlineColorAttributeName, value: highLightColor, range: fixedRange)
mutableString.addAttribute(NSLinkAttributeName, value: result?.url, range: fixedRange)
mutableString.addAttribute(NSForegroundColorAttributeName, value: highLightColor, range: fixedRange)
}
}
self.attributedText = mutableString
}
}
将委托添加到viewController:
/// Delegate function for linkDetection
func textNode(_ textNode: ASTextNode, shouldHighlightLinkAttribute attribute: String, value: Any, at point: CGPoint) -> Bool {
return true
}
func textNode(_ textNode: ASTextNode, tappedLinkAttribute attribute: String, value: Any, at point: CGPoint, textRange: NSRange) {
guard let url = value as? URL else { return }
}
对于链接检测,您需要使用外部库。 我建议https://github.com/twitter/twitter-text你可以用cocoapods安装它。
然后你需要将TwitterTextEntity *转换为NSTextCheckingResult *。
您可以使用此类NSString:
- (NSArray <NSTextCheckingResult *>*)textCheckingResultsForURLs {
NSArray *twitterEntitiesArray = [TwitterText URLsInText:self];
NSMutableArray *textCheckingResultsArray = [[NSMutableArray alloc] initWithCapacity:[twitterEntitiesArray count]];
for (TwitterTextEntity *twitterTextEntity in twitterEntitiesArray) {
NSString *textCheckingResultUTF8 = [[self substringWithRange:twitterTextEntity.range] stringPercentEncode];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@", textCheckingResultUTF8]];
NSTextCheckingResult *result = [NSTextCheckingResult linkCheckingResultWithRange:twitterTextEntity.range URL:url];
[textCheckingResultsArray addObject:result];
}
return textCheckingResultsArray;
}
像这样使用它:
NSArray *links = [yourString textCheckingResultsForURLs];
然后你需要将计算范围添加到NSMutableAttributedString,如下所示:
for (NSTextCheckingResult *textCheckingResult in links) {
NSMutableDictionary *linkAttributes = [[NSMutableDictionary alloc] initWithDictionary:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
linkAttributes[@"TextLinkAttributeNameURL"] = [NSURL URLWithString:textCheckingResult.URL.absoluteString];
[string addAttributes:linkAttributes range:textCheckingResult.range];
}
然后,您需要配置ASTextNode节点以突出显示特定范围。 所以在父节点中添加:
_textLabelNode.delegate = self;
_textLabelNode.userInteractionEnabled = YES;
_textLabelNode.linkAttributeNames = @[@"TextLinkAttributeNameURL"];
+
- (void)didLoad {
// For text node
self.layer.as_allowsHighlightDrawing = YES;
[super didLoad];
}
#pragma mark - ASTextNodeDelegate
- (BOOL)textNode:(ASTextNode *)richTextNode shouldHighlightLinkAttribute:(NSString *)attribute value:(id)value atPoint:(CGPoint)point {
return YES;
}
- (void)textNode:(ASTextNode *)richTextNode tappedLinkAttribute:(NSString *)attribute value:(NSURL *)URL atPoint:(CGPoint)point textRange:(NSRange)textRange {
NSLog(@"TODO");
}
这适合我。 希望,不要忘记任何事情。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.