2015-04-01 58 views
1

如果NSTextView爲空(用戶在textView中沒有輸入任何內容),但無法找到合適的內置方法,則試圖顯示消息。在NSTextFiled的情況下,它很簡單。檢查OSX中的NSTextView是否爲空

if(countElements(emailTextField.stringValue) == 0) 

是否有MAC爲NSTextView替代

+0

您在獲取textview的內容或驗證內容時遇到問題嗎? – Cristik 2015-04-01 10:27:49

回答

1

可以訪問由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屬性。

+0

非常感謝很多人工作完美.. :) – soldiershin 2015-04-01 12:13:40