如果NSTextView爲空(用戶在textView中沒有輸入任何內容),但無法找到合適的內置方法,則試圖顯示消息。在NSTextFiled的情況下,它很簡單。檢查OSX中的NSTextView是否爲空
if(countElements(emailTextField.stringValue) == 0)
是否有MAC爲NSTextView替代
如果NSTextView爲空(用戶在textView中沒有輸入任何內容),但無法找到合適的內置方法,則試圖顯示消息。在NSTextFiled的情況下,它很簡單。檢查OSX中的NSTextView是否爲空
if(countElements(emailTextField.stringValue) == 0)
是否有MAC爲NSTextView替代
可以訪問由NSTextView
舉行的NSTextStorage
對象,並使用(只讀)length
屬性來計算在存儲對象的基礎string
元素屬性:
// Mark: NSTextViewDelegate implementation
func textDidChange(notification: NSNotification) {
if let textView = notification.object as? NSTextView {
if let storage = textView.textStorage {
if storage.length == 0 {
// text-view is empty
}
}
}
}
如果你看一下NSTextStorage
類的引用,你會看到,它實際上只是一個NSAttributedString
子類。 NSAttributedString
有一個string
屬性和一個length
屬性 - 它是我們用來確定字符串是否爲'空'的length
屬性。
非常感謝很多人工作完美.. :) – soldiershin 2015-04-01 12:13:40
您在獲取textview的內容或驗證內容時遇到問題嗎? – Cristik 2015-04-01 10:27:49