简体   繁体   English

UITextField-开头和结尾均不可编辑的前缀字符

[英]UITextField - uneditable prefixed characters both in beginning & end

I have a UITextField, and I want to give it uneditable characters. 我有一个UITextField,我想给它不可编辑的字符。 For example I have "Hello..." , and I want the user to write in between o and . 例如,我有"Hello..." ,并且我希望用户在o和之间书写. such as "Hello userTypedStuff..." 例如"Hello userTypedStuff..."

I used this approach, however with that, user can longpress the beginning and for example go to the position inside Hello. 我使用了这种方法,但是,用户可以长按开头,例如转到Hello内的位置。 Thus, user can mess with the prefixed characters. 因此,用户可能会弄​​混前缀字符。

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
    let protectedRange = NSMakeRange(0, 5)
    let intersection = NSIntersectionRange(protectedRange, range)
    if intersection.length > 0 {

        return false
    }
    if range.location == 12 {
        return true
    }
    if range.location + range.length > 12 {
        return false
    }
    return true
}

How can I setup something like 'uneditable first 5 characters and last 3 characters'? 如何设置“不可编辑的前5个字符和后3个字符”之类的内容?


Edit: The UITextField's text-aligned center and is forming above a UILabel. 编辑:UITextField的文本对齐中心,并在UILabel上方形成。 However, if I give textField the width of label, textField won't get larger on width. 但是,如果给textField标签的宽度,则textField的宽度不会变大。 Thus, I gave its width view's width. 因此,我给出了其宽度视图的宽度。 Is there a way I can create UILabel on both ends having Hello and ... separately, and have textField (with text-aligned center) in the middle which automatically stretches width? 有没有一种方法,我可以在其两端创建的UILabel Hello...分开,并且具有自动伸展宽度中间文本框(文本对齐中心)?


Edit 2: 编辑2:

If I have my textview with two static UILabels on both sides, that can be a workaround. 如果我的文本视图的两边都带有两个静态UILabel,则可以解决。 However, this time the UITextField won't push UILabels to sides as user starts to type in. The TextField center textAligned. 但是,这一次,随着用户开始输入,UITextField不会将UILabel推向侧面。TextField居中为textAligned。 If I give it a constant width while creating it (programatically with CGRectMake ) , it won't stretch its width as user types in. Thus, I gave it the width of view. 如果我在创建它时给它一个恒定的宽度(使用CGRectMake编程),它将不会随着用户的输入而扩展其宽度。因此,我给它提供了视图的宽度。 Is there a way to auto-stretch the TextField's width as user types in that pushes the UILabels on the sides? 有没有一种方法可以在用户键入内容时自动拉伸TextField的宽度,从而将UILabel推向侧面?

 "Hello" [textfield] "..."
 uilabel             uilabel
class SpecialTextField: UITextField {

// MARK: - UITextField Observing
override internal func willMoveToSuperview(newSuperview: UIView!) {
    if newSuperview != nil {
        text = "Hello..."
        addTarget(self, action: #selector(SpecialTextField.didChangeText), forControlEvents: .EditingChanged)
    }
}

override internal func canPerformAction(action: Selector, withSender sender: AnyObject?) -> Bool {
    return false
}


private var newVal: String = "Hello..."

func didChangeText() {
    var userDidEditDefaultValue = true
    if (text?.characters.count > 7) { // Count of "Hello..."
        if text?.rangeOfString("Hello") != nil {
            if text?.rangeOfString("...") != nil {
                userDidEditDefaultValue = false
            }
        }
    }
    if userDidEditDefaultValue {
        text = newVal
    }
    newVal = text ?? "Hello..."
}
}

This will not allow user to change default value of "Hello..." OR "Hello userEntry..." 这将不允许用户更改默认值“ Hello ...”或“ Hello userEntry ...”

You can use delegate method to insert text in the middle. 您可以使用委托方法在中间插入文本。

Hope it helped. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM