如何在Swift中逐个更改UITextView中某些单词的样式? [英] How can I change style of some words in my UITextView one by one in Swift?

查看:74
本文介绍了如何在Swift中逐个更改UITextView中某些单词的样式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UITextView ,其中包含许多不同的单词。当用户进入该屏幕时,我想开始突出显示一些单词,例如:

I have a UITextView with many different words next to each other. When user enters that screen I want to start highlighting some words, e.g.:

他看到的第一个是文字墙:

the first what he sees is a wall of text:

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

然后,一秒后,单词会改变风格(颜色),所以他会看到:

then, after one second, the word four would change style (color), so he would see:

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

然后,一秒钟之后,另一个单词会突出显示(并加入已经着色的四个):

then, after one second, another word would highlight (and join already colored 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

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

and so on. So after couple seconds user would see:

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

然后文字应该保持这样。
我怎样才能实现这个目标?

and then the text should stay like this. How can I achieve this?

我想过循环翻译并检查它们是否等于预定义的单词但是我不知道如何咬它 - 可以你帮帮我了吗?

I thought about looping through words and checking whether they equals the predefined words but I have no idea how to bite it - can you help me with that?

=====编辑

所以为了让自己更容易决定用符号标记突出显示的单词。

So to makes things easier for myself I decided to mark highlighted words with # symbol.

我有扩展名以突出显示以<$ c开头的所有单词$ c>#在textView中:

I have extension to highlight all words that begin with # in the 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
}
}

但我不是确定如何用一秒延迟分别突出显示每个单词

but I'm not sure how to highlight each word separately with one second delay

推荐答案

使用计时器。在物业中存放火柴。在属性中存储未突出显示的基础字符串。现在让你的计时器突出显示第一场比赛并在1秒后再次自我调用,突出显示第二场比赛并重复直到没有比赛为止。

Use a timer. Stash matches in a property. Stash the base unhighlighted attributed string in a property. Now have your timer highlight the first match and call itself again in 1 second, highlighting up to the second match and repeat until there are no matches left.

    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)
        }
    }

这篇关于如何在Swift中逐个更改UITextView中某些单词的样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆