2016-11-09 73 views
1

我有一个UITextView与许多不同的单词相邻。当用户输入的屏幕,我要开始凸显一些话,如:如何在Swift中逐个更改我的UITextView中某些单词的样式?

第一是他看到的是文本的墙:

one two three #four five six #seven eight nine ten 
eleven #twelve thirteen fourteen fifteen sixteen 
some other words #example test whatever #some thing 

然后,在一秒钟后,这个词four会改变风格(颜色),所以,他会看到:

one two three #FOUR five six #seven eight nine ten 
eleven #twelve thirteen fourteen fifteen sixteen 
some other words #example test whatever #some thing 

然后,在一秒钟后,另一个词将突出(并加入已经着色four):

one two three #FOUR five six #SEVEN eight nine ten 
eleven #twelve thirteen fourteen fifteen sixteen 
some other words #example test whatever #some thing 

等等。所以几秒钟后用户会看到:

one two three #FOUR five six #SEVEN eight nine ten 
eleven #TWELVE thirteen fourteen fifteen sixteen 
some other words #EXAMPLE test whatever #SOME thing 

然后文本应该保持这样。 我该如何做到这一点?

我想过通过单词循环并检查它们是否等于预定义的单词,但我不知道如何咬它 - 你能帮我一下吗?

=====编辑

因此,为了使事情变得更容易为我自己,我决定与#符号来标记高亮的话。

我有延伸突出的是,在与TextView的开头#所有单词:

extension UITextView { 

func formatTextInTextView() { 
    self.isScrollEnabled = false 
    let selectedRange = self.selectedRange 
    let text = self.text 
    let font = UIFont(name: "AppleSDGothicNeo-Light", size: 16.0) 
    let titleDict: NSDictionary = [NSFontAttributeName: font!] 
    // This will give me an attributedString with the desired font 
    let attributedString = NSMutableAttributedString(string: text!, attributes: titleDict as! [String : AnyObject]) 
    let regex = try? NSRegularExpression(pattern: "#(\\w+)", options: []) 
    let matches = regex!.matches(in: text!, options: [], range: NSMakeRange(0, (text?.characters.count)!)) 
    for match in matches { 
     let matchRange = match.rangeAt(0) 

     let titleDict: NSDictionary = [NSForegroundColorAttributeName: orangeColor] 

     attributedString.addAttributes(titleDict as! [String : AnyObject], range: matchRange) 
    } 
    self.attributedText = attributedString 
    self.selectedRange = selectedRange 
    self.isScrollEnabled = true 
} 
} 

,但我不知道如何分开显示的每个字有一秒的延迟

+0

您对循环的想法是正确的。尝试一些东西。用一些相关的代码更新你的问题,至少展示一些基本的尝试。清楚地描述你需要帮助。 – rmaddy

+0

@maddy,我编辑了我的问题,谢谢指出! – user3766930

+0

由于您使用的是正则表达式,该代码一次突出显示所有单词。你需要一些方法来创建一个你想要突出显示的单词列表(数组),然后遍历该数组。对于每个单词,您随后都会找到该单词的所有匹配项。 – rmaddy

回答

1

用一个定时器。藏在地产中的匹配。存储属性中未被高亮显示的属性字符串。现在让你的计时器突出显示第一场比赛,并在1秒内重新打出自己的位置,突出显示第二场比赛并重复,直到没有比赛结束。

func highlight (to index: Int = 0) { 
     guard index < matches.count else { 
      return 
     } 
     let titleDict: NSDictionary = [NSForegroundColorAttributeName: orangeColor] 
     let attributedString = NSMutableAttributedString(attributedString: storedAttributedString) 
     for i in 0..< index { 
      let matchRange = matches[i].rangeAt(0) 
      attributedString.addAttributes(titleDict as! [String : AnyObject], range: matchRange) 
     } 
     self.attributedText = attributedString 
     let _ = Timer.scheduledTimer(withTimeInterval: 1, repeats: false) { _ in 
      self.highlight(to: index + 1) 
     } 
    } 
+0

感谢乔希,我试过你的代码,但它带给我很多错误,请查看http://imgur.com/pOY6UYp你知道我该如何修复它们? – user3766930

+1

我在上面的示例中为NSMutableAttributedString()和定时器上的闭包添加了缺少的参数标签。你在匹配上的错误是因为匹配应该是一个属性,它应该在你的代码中定义为let matches = regex!.matches(in:text !, options:[],range:NSMakeRange(0,(text ?. characters.count)!)),它产生一种[NSTextCheckingResult]而不是[String] –

+0

谢谢你,尽管最后两个问题 - 这条线是自我。如果#Asvailable(iOS 10.0,*){'与定时器的行 - 现在我有一个错误,因为它是一个无法解析的标识符:(和第二件事 - 我不得不添加' else'block'with'// Fallback on early versions' - 什么是最好的选择来处理它?我能不知道如何突出显示没有计时器的所有单词吗? – user3766930

相关问题