ios - 动画更改 NSMutableAttributedString 的一部分

标签 ios animation swift nsmutableattributedstring

我正在制作一个具有 UITextView 的 iOS 应用程序。在关闭该 UITextView 中的括号时,我想向用户突出显示它配对的左括号。到目前为止,我已经使用 NSMutableAttributedString 并更改成对括号的字体大小来完成此操作,这可行但有点难看。 我真正想要的是对此进行动画处理,就像我在代码中关闭括号时 xcode 执行相同操作的方式一样。 有什么办法可以做到这一点吗?

非常感谢任何帮助,尽管我对此还很陌生,所以请不要以为我知道太多:)

这是我的代码:

    @IBAction func didPressClosingParentheses(sender: AnyObject) {
    appendStringToInputTextView(")")
    var count = 1
    let currentString = inputTextView.attributedText.string
    let characterArray = Array(currentString)
    let closingIndex = characterArray.count - 1
    for i in reverse(0...closingIndex-1) {
        if characterArray[i] == "(" {
            count--
        }
        else if characterArray[i] == ")" {
            count++
        }
        if count == 0 {
            let startingIndex = i
            var newString = NSMutableAttributedString(string: currentString)
            newString.addAttribute(NSFontAttributeName, value: UIFont(name: "HelveticaNeue-Thin", size: 28)!, range: NSMakeRange(0, newString.length))
            newString.addAttribute(NSForegroundColorAttributeName, value: UIColor(red: 243, green: 243, blue: 243, alpha: 1), range: NSMakeRange(0, newString.length))
            newString.addAttribute(NSFontAttributeName, value: UIFont(name: "HelveticaNeue-Thin", size: 35)!, range: NSMakeRange(startingIndex, 1))
            newString.addAttribute(NSFontAttributeName, value: UIFont(name: "HelveticaNeue-Thin", size: 35)!, range: NSMakeRange(closingIndex, 1))


            UIView.animateWithDuration(0.4, delay: 0.0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.8, options: nil, animations: {
                self.inputTextView.attributedText = newString

            }, nil)

            break
        }
    }
}

如您所见,我已经尝试使用 UIView.animateWithDuration 来执行此操作,但我怀疑这没有用。

最佳答案

另一种对我有用的解决方案 (Swift 4) 是生成多个属性字符串,将其中一个分配给标签,然后替换过渡动画 block 内的内容 (attributedText) .例如:

// MARK: Extension util which generates NSAttributedString by text,font,color,backgroundColor
extension NSAttributedString {
    class func generate(from text: String, font: UIFont = UIFont.systemFont(ofSize: 16), color: UIColor = .black, backgroundColor: UIColor = .clear) -> NSAttributedString {
        let atts: [NSAttributedStringKey : Any] = [.foregroundColor : color, .font : font, .backgroundColor : backgroundColor]
        return NSAttributedString(string: text, attributes: atts)
    }
}

// MARK: Sentence
let string1 = "Hi, i'm "
let string2 = "Daniel"

// MARK: Generate highlighted string
let prefixAttString = NSAttributedString.generate(from: string1)
let highlightedSuffixAttString = NSAttributedString.generate(from: string2, backgroundColor: .red)
let highlightedString = NSMutableAttributedString()
highlightedString.append(prefixAttString)
highlightedString.append(highlightedSuffixAttString)


// MARK: Generate regular string (Same prefix, different suffix)enter image description here
let regularSuffixAttString = NSAttributedString.generate(from: string2)
let regularString = NSMutableAttributedString()
regularString.append(prefixAttString)
regularString.append(regularSuffixAttString)

self.view.addSubview(label)
label.attributedText = regularString

// UIViewAnimationOptions.transitionCrossDissolve is necessary.
UIView.transition(with: self.label, duration: 4, options: [.transitionCrossDissolve], animations: {
    self.label.attributedText = highlightedString
}, completion: nil)

不要忘记在动画选项中使用 .transitionCrossDissolve

关于ios - 动画更改 NSMutableAttributedString 的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28034317/

相关文章:

ios - 在 iOS 上获取相机当前曝光持续时间值?

颜色动画的 jQuery 数学 - 特定颜色范围

jquery - 如何使用 JQuery 设置 HTML5 进度条的值的样式?

android - Android Market 中的 TextView 展开动画

ios - 在可重复使用的单元格中显示下载进度

ios - CoreData 完成后 Swift iOS 9 UITableView 重新加载数据

ios - Swift 3,成功传递数据但var在使用时返回nil

ios - RTF 中的 NSAttributedString 仅适用于 iOS 8,不适用于 iOS 7

ios - 您如何进行初始迁移以从非 Realm 持久层迁移?

ios - UINavigationController 内的 UISplitViewController