最后如何从textview中删除空白,以及如何在swift2.0中从enter键中删除空白

最后如何从textview中删除空白,以及如何在swift2.0中从enter键中删除空白,swift2,ios7.1,xcode7.3,Swift2,Ios7.1,Xcode7.3,我是swift新手。我想删除文本视图中的空白,也要删除文本视图中的新行空白。因此,请给出建议 提前感谢。对于新行或按下回车按钮时,您可以使用委托方法 func textView(textView:UITextView,shouldChangeTextInRange:NSRange,replacementText:String)->Bool 将此添加到方法中 如果文本==“\n” { self.view.endEditing(true) 返回错误 } 对于最后的空格,请使用下面的代码 var s

我是swift新手。我想删除文本视图中的空白,也要删除文本视图中的新行空白。因此,请给出建议


提前感谢。

对于新行或按下回车按钮时,您可以使用委托方法

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

将此添加到方法中
如果文本==“\n”
{
self.view.endEditing(true)
返回错误
}

对于最后的空格,请使用下面的代码

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

希望有帮助。

纠正我的错误,是否要在文本结尾处修剪空格和换行?或者,当没有其他单词时,您想将textView显示为空,然后显示空白和换行符?是的。就像堆栈溢出注释框一样@萨希尔