2016-11-09 72 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

相關問題