如何阻止用户不要在UITextView iOS中的文本开头键入空格?

时间:2016-04-23 01:21:35

标签: ios swift uitextfield uitextview

我想限制在iOS中文本字段开头使用空格键。我尝试使用下面的逻辑,但不允许在单词之间的任何位置使用空格。在这种情况下请帮助我。

if self.rangeOfCharacterFromSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()) != nil {
    return false
}
return true

6 个答案:

答案 0 :(得分:2)

如果您需要执行上述操作,可以使用textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String)中的UITextViewDelegate方法。

示例:

func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
    // as @nhgrif suggested, we can skip the string manipulations if 
    // the beginning of the textView.text is not touched.
    guard range.location == 0 else {
        return true
    }

    let newString = (textView.text as NSString).stringByReplacingCharactersInRange(range, withString: text) as NSString
    return newString.rangeOfCharacterFromSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()).location != 0
}

首先,我们构造将在textView

中显示的新字符串

然后我们检查它是否以空格,制表符或换行符开头。

如果是这样,我们会返回false,以便textView不会将新文本放入。

否则,将新文本放入textView

注意:我们需要检查整个字符串,而不是检查replacementText来处理复制粘贴操作。

另一种可能的方法是限制用户键入的文本,但在需要使用该值时修剪结果文本。

let myText = textView.text.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())

编辑:添加guard子句,以便根据@ nhgrif的评论使该方法更具效果。

答案 1 :(得分:0)

在textView委托中编写此内容:

 Sub WorksheetLoop()

 Dim Current As Range
 Dim Element As Range
 Dim wgRow As Integer
 Dim wbRow As Integer
 Dim Warengruppe As String
 Dim Warengruppebezeichnung As String

 Dim Stamm1 As String
 Dim Stamm2 As String
 Dim Stamm3 As String
 Dim Summe As String

 wgRow = 2
 wbRow = 2

 For Each Element In Sheets("Artikelstammdaten").Range("R:R")

    Warengruppebezeichnung = Sheets("Warengruppen").Cells(wbRow, 1).Value
    Warengruppe = Sheets("Artikelstammdaten").Cells(wgRow, 18).Value

    If Warengruppe = Warengruppebezeichnung Then
        Stamm1 = Sheets("Warengruppen").Cells(wbRow, 2).Value
        Stamm2 = Sheets("Warengruppen").Cells(wbRow, 3).Value
        Stamm3 = Sheets("Warengruppen").Cells(wbRow, 4).Value
        Summe = Stamm1 + ">" + Stamm2 + ">" + Stamm3

       Sheets("Artikelstammdaten").Cells(wgRow, 18).Value = Summe

       wbRow = 2
       wgRow = wgRow + 1
    Else
        wbRow = wbRow + 1

    End If
    Next

 End Sub

答案 2 :(得分:0)

对于TextField

Swift 4.2

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    guard range.location == 0 else {
        return true
    }

    let newString = (textField.text! as NSString).replacingCharacters(in: range, with: string) as NSString
    return newString.rangeOfCharacter(from: CharacterSet.whitespacesAndNewlines).location != 0
}

答案 3 :(得分:0)

Swift 4.1

使用单行代码就很简单,您可以停止空格

对于所有UITextView

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {

    textView.text = textView.text.replacingOccurrences(of: " ", with: "")
}

对于单个UITextView

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {

    if textView == textViewArea
    {
        textView.text = textView.text.replacingOccurrences(of: " ", with: "")
    }
    return true
}

答案 4 :(得分:0)

此处的答案有所改善您可以禁用空格直到第一个字符输入

c

此后,在保存最终字符串之前,您可以从字符串结尾处修剪空格和换行符。

OR

使用::

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
    let whitespaceSet = NSCharacterSet.whitespaces
    let range = text.rangeOfCharacter(from: whitespaceSet)
    if let _ = range {
        let txt = textView.text ?? ""
        if txt.isBlankByTrimming || txt.isEmpty {
            return false
        }else {
            return true
        }
    }
    else {
        return true
    }
}

答案 5 :(得分:0)

public func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {        
        if (range.location == 0 && string == " ") {
            return false
        }
}