【问题标题】:Detect hashtags and add to array检测主题标签并添加到数组
【发布时间】:2016-03-04 04:21:25
【问题描述】:

我有一个 UITextField,用户可以在其中编写描述。

示例:“这是我的#car 的图像。酷炫的#sunshine 背景也适合我的#fans。”

如何检测标签“汽车”、“阳光”和“粉丝”,并将它们添加到数组中?

【问题讨论】:

标签: ios swift hashtag


【解决方案1】:
let frame = CGRect(x: 0.0, y: 0.0, width: 100.0, height: 30.0)

let description = UITextField(frame: frame)
description.text = "This is a image of my #car. A cool #sunshine background also for my #fans."


extension String {
    func getHashtags() -> [String]? {
        let hashtagDetector = try? NSRegularExpression(pattern: "#(\\w+)", options: NSRegularExpressionOptions.CaseInsensitive)
        let results = hashtagDetector?.matchesInString(self, options: NSMatchingOptions.WithoutAnchoringBounds, range: NSMakeRange(0, self.utf16.count)).map { $0 }

        return results?.map({
            (self as NSString).substringWithRange($0.rangeAtIndex(1))
        })
    }
}

description.text?.getHashtags() // returns array of hashtags

来源:https://github.com/JamalK/Swift-String-Tools/blob/master/StringExtensions.swift

【讨论】:

  • 在本地很容易做的事情上,需要使用 3rd 方垃圾的理由为零。
  • 这是您在使用正则表达式找到这些项目后需要“散列标签” sh*t 的两个调用。我永远不会明白为什么你需要使用 Github 的垃圾替代品来代替需要两行代码来部署的东西: [attString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:NSUnderlineStyleSingle] range:NSMakeRange(wordRange.location+1 , wordRange.length-1)]; [attString addAttribute:NSLinkAttributeName 值:@"google.com" range:wordRange];
  • @Larcerax - 请详细说明为什么您的代码 sn-p 是一件艺术品,而上面发布的代码是 sh*t?不确定您是否了解第 3 方的真正含义。您的代码与上面发布的代码一样,适用于任何想要使用它的人!
  • 不,我的只是使用苹果写的IOS,就是这样,属性字符串的方法包括NSUnderlineStyleSingle和NSLinkAttributeName,那么你只需要RegEx进入hashtags,当找到hastags时,你添加下划线,然后将所需的 Web 链接或应用程序链接添加到视图控制器。您使用 UITextVeiws 中的委托拦截这些哈希标记发送的消息,该委托拦截附加了 NSLinkAttributeName 的文本,非常简单。
【解决方案2】:

Swift 4.2 版本。最后,我们返回一个没有# 的主题标签/关键字列表。

extension String {
func getHashtags() -> [String]? {
    let hashtagDetector = try? NSRegularExpression(pattern: "#(\\w+)", options: NSRegularExpression.Options.caseInsensitive)

    let results = hashtagDetector?.matches(in: self, options: .withoutAnchoringBounds, range: NSRange(location: 0, length: count))

    return results?.map({
        (self as NSString).substring(with: $0.range(at: 1)).capitalized
    })
}

}

例如

输入 #hashtag1 #hashtag2 #hashtag3 #hashtag4 #hashtag5

输出 [hashtag1, hashtag2, hashtag3, hashtag4, hashtag5]

【讨论】:

    【解决方案3】:

    检查这个 pod:https://cocoapods.org/pods/twitter-text

    TwitterText类中有一个方法(NSArray *)hashtagsInText:(NSString *)text checkingURLOverlap (BOOL)checkingURLOverlap

    Twitter 创建了这个 pod 来查找 #、@、URL,所以在我看来,没有更好的方法可以做到这一点。 :)

    【讨论】:

      【解决方案4】:

      @Anurag 答案的 Swift 3 版本:

      extension String {
      func getHashtags() -> [String]? {
          let hashtagDetector = try? NSRegularExpression(pattern: "#(\\w+)", options: NSRegularExpression.Options.caseInsensitive)
          let results = hashtagDetector?.matches(in: self, options: NSRegularExpression.MatchingOptions.withoutAnchoringBounds, range: NSMakeRange(0, self.characters.count)).map { $0 }
      
          return results?.map({
              (self as NSString).substring(with: $0.rangeAt(1))
          })
      }
      

      }

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-03-17
        • 1970-01-01
        • 2014-08-05
        • 2012-11-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多