简体   繁体   English

检测ASTextNode AsyncDisplayKit中的链接或URL

[英]Detect Link or URL in ASTextNode AsyncDisplayKit

I been trying to use AsyncDisplayKit framework , I have an requirement to detect url's in text. 我一直在尝试使用AsyncDisplayKit框架,我需要检测文本中的url。

I have used ASTextNode but couldn't find any api to detect links. 我使用过ASTextNode但找不到任何api来检测链接。

I read that there is property linkAttributeNames used for url detection but unable to find any example how to do it. 我读到有用于url检测的属性linkAttributeNames但无法找到任何示例如何执行此操作。

Could someone help me how to use the above class? 有人可以帮助我如何使用上述课程吗?

thanks 谢谢

For Swift 3.0 对于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
    }        
}

Add the delegate to your viewController: 将委托添加到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 }
 }

For link detection you need to use external library. 对于链接检测,您需要使用外部库。 I'd recommend https://github.com/twitter/twitter-text You can install it with cocoapods. 我建议https://github.com/twitter/twitter-text你可以用cocoapods安装它。

Then you need to convert TwitterTextEntity* to NSTextCheckingResult*. 然后你需要将TwitterTextEntity *转换为NSTextCheckingResult *。

You can use this category of NSString: 您可以使用此类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;
}

Use it like this: 像这样使用它:

NSArray *links = [yourString textCheckingResultsForURLs];

Then you need to add calculated ranges to NSMutableAttributedString like this: 然后你需要将计算范围添加到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];
    }

Then you need to configure ASTextNode node to highlight specific ranges. 然后,您需要配置ASTextNode节点以突出显示特定范围。 So in parent node add: 所以在父节点中添加:

_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");
}

This works for me. 这适合我。 Hope, didn't forget about anything. 希望,不要忘记任何事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM