YLBaseChat icon indicating copy to clipboard operation
YLBaseChat copied to clipboard

聊天界面里是如何识别 URL 的呢?

Open honeybeeSwinging opened this issue 8 years ago • 2 comments

你好,请教一下,请问在聊天界面里是如何识别 URL 的呢?谢谢!我的 QQ:834537795

honeybeeSwinging avatar Jun 06 '17 08:06 honeybeeSwinging

用正则表达式 // 识别网址 let regexURL = try! NSRegularExpression(pattern: "[a-zA-z]+://[^\s]*", options: NSRegularExpression.Options(rawValue: 0))

    let regexArrayURL = regexURL.matches(in: content, options: NSRegularExpression.MatchingOptions.reportProgress, range: NSRange(location: 0,length: content.characters.count))
    
    for result:NSTextCheckingResult in regexArrayURL {
        
        let range = result.range
        
        let border = YYTextBorder(fill: UIColor.lightGray, cornerRadius: 0)
        let highlight = YYTextHighlight()
        highlight.setColor(UIColor.blue)
        highlight.setBackgroundBorder(border)
        
        mutableText.yy_setColor(UIColor.blue, range: range)
        mutableText.yy_setTextHighlight(highlight, range: range)
        
    }

February12 avatar Jun 06 '17 09:06 February12

谢谢,看 swift 还不是太习惯啊,有 OC 的吗?

honeybeeSwinging avatar Jun 13 '17 07:06 honeybeeSwinging