簡體   English   中英

如何最后從TextView中刪除空白以及如何在swift2.0中從Enter鍵刪除空白

[英]How to delete Whitespace from textview in the end and also delete white space from enter keys in swift2.0

我是swift的新手,我想刪除空白並且還要從TextView中刪除換行空白,所以請提出建議。

提前致謝。

對於換行或按Enter鍵時,可以使用委托方法

func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool

if text == "\\n" { self.view.endEditing(true) return false }請將其添加到方法中

最終對於空格,請使用以下代碼。

var stringFromText = textView.text

    var lastChar = stringFromText.characters.last


    while lastChar == " " {
        stringFromText = String(stringFromText.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()))
        print(stringFromText!)
       if stringFromText != "" {
            lastChar = stringFromText?.characters.last
        }
        else
        {
            lastChar = "A" //Any random char
        }

        print(lastChar!)
    }
    textComment.text = stringFromText

希望能幫助到你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM