我有一个非常简单的 UITextView 子类,它添加了“占位符”功能,您可以在文本字段对象中找到该功能。这是我的子类代码:
import UIKit
import Foundation
@IBDesignable class PlaceholderTextView: UITextView, UITextViewDelegate
{
@IBInspectable var placeholder: String = "" {
didSet {
setPlaceholderText()
}
}
private let placeholderColor: UIColor = UIColor.lightGrayColor()
private var textColorCache: UIColor!
override init(frame: CGRect) {
super.init(frame: frame)
self.delegate = self
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.delegate = self
}
func textViewDidBeginEditing(textView: UITextView) {
if textView.text == placeholder {
textView.text = ""
textView.textColor = textColorCache
}
}
func textViewDidEndEditing(textView: UITextView) {
if textView.text == "" && placeholder != "" {
setPlaceholderText()
}
}
func setPlaceholderText() {
if placeholder != "" {
if textColorCache == nil { textColorCache = self.textColor }
self.textColor = placeholderColor
self.text = placeholder
}
}
}
更改班级后UITextView
身份检查器中的对象PlaceholderTextView
,我可以设置Placeholder
属性在属性检查器中很好。该代码在运行应用程序时效果很好,但不会在界面生成器中显示占位符文本。我还收到以下非阻塞错误(我认为这就是它在设计时不渲染的原因):
错误:IB Designables:无法更新自动布局状态:Interface Builder Cocoa Touch Tool 崩溃
错误:IB Designables:无法渲染 PlaceholderTextView 的实例:渲染视图花费的时间超过 200 毫秒。您的绘图代码可能会受到性能缓慢的影响。
我无法弄清楚是什么导致了这些错误。第二个错误没有任何意义,因为我什至没有重写drawRect()。有任何想法吗?
Interface Builder Cocoa Touch Tool 崩溃时会生成崩溃报告。论文位于~/Library/Logs/DiagnosticReports
并命名为IBDesignablesAgentCocoaTouch_*.crash
。就我而言,它们包含一个有用的堆栈跟踪,可以识别我的代码中的问题。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)