Can I place noneditable text in UITextField

后端 未结 3 1090
-上瘾入骨i
-上瘾入骨i 2021-01-19 05:28

I\'d like to place some fixed text inside an UITextField, but before the insert point, sort of like this:


| He went (towaards)

...where \"towaards\"

3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-19 05:50

    I guess you can do that by implementing UITextFieldDelegate. There is one method – textField:shouldChangeCharactersInRange:replacementString:.

    In this method you can check each and every character which is entering by user (even backspace). So first you implement change in another NSString object then validate with your condition.

    Check whether new string passes through your validation then replace the old string with new string, otherwise leave it as it is.

    For example, check new string starts with your string or not. You can use this:

    [myString hasPrefix:@"He went"];

    Here in the same method you can also do one more thing, when user taps on UITextField check if @"(towaards)" string is there in textField then remove that text by code.

    Hope this is what you want.

提交回复
热议问题