简体   繁体   中英

UITextField - uneditable prefixed characters both in beginning & end

I have a UITextField, and I want to give it uneditable characters. For example I have "Hello..." , and I want the user to write in between o and . such as "Hello userTypedStuff..."

I used this approach, however with that, user can longpress the beginning and for example go to the position inside 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'?


Edit: The UITextField's text-aligned center and is forming above a UILabel. However, if I give textField the width of label, textField won't get larger on width. 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?


Edit 2:

If I have my textview with two static UILabels on both sides, that can be a workaround. However, this time the UITextField won't push UILabels to sides as user starts to type in. The TextField center 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. Is there a way to auto-stretch the TextField's width as user types in that pushes the UILabels on the sides?

 "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..."

You can use delegate method to insert text in the middle.

Hope it helped.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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