我正在制作一个具有 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 来执行此操作,正如我所怀疑的那样,它不起作用。