向UITextField添加一个永久字符 [英] Add a permanent Character to UITextField

查看:85
本文介绍了向UITextField添加一个永久字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法永久性地将字母添加到用户无法删除的UITextField?我想添加一个字符而用户无法删除,但他们仍然可以添加字母。

Is there a way to permanently add a letter to a UITextField where the user cannot delete it? I want to add a single character and the user not be able to delete it but they can still add letters after.

干杯,

ps这适用于iOS

p.s. This is for iOS

推荐答案

A UITextField 有一个名为should的委托方法更改范围内的字符,这个方法基本上问,我应该添加还是删除下一个字符?从中你可以决定你想要什么。下面是一些示例代码。

A UITextField has a delegate method called should change characters in range, this method basically ask, should i add or remove the next character? and from that you can dictate what you would like. Here is some example code.

 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

    BOOL shouldChangeCharacters = ( (textField.text.length + string.length) > 1 ); 
    return shouldChangeCharacters;

}

此代码表示添加的新字符加上当前text大于1然后可以进行更改,如果文本不大于1,那么我们将不会进行更改...

This code means if the new character being added plus the current text is greater than 1 then it is okay to make the change, if the text is not greater than one, then we will not make the change...

现在,假设某人可能会尝试粘贴您的角色,仍会调用此委托方法,但您必须执行一些操作。

Now, under the assumption that someone may try to paste over your character, this delegate method is still called but you have to do a few things.

if (range.location == 0) {
    NSString* firstCharacter = [string substringToIndex:1];
    BOOL firstCharacterIsDesiredCharacter = [firstCharacter isEqualToString:@"#"];
    if ( !firstCharacterIsDesiredCharacter ) {
        NSString* symbolWithText = [NSString stringWithFormat:@"#%@",text];

        //  ******************** \\

        [textView setText:symbolWithText];
        return NO;

        // or we could do this

        string = symbolWithText;

        //  ******************** \\

    }

}

两种情况都在修改参数的值...有些人不喜欢这样做。这可能是一个糟糕的编程习惯..或者如果你要修改它们,你应该先做一些过程。

Both scenarios, are modifying the values of the parameters... some people don't like doing that. It could be a bad programming practice.. or if you are going to modify them there's some process you should do first.

有了这个,我们只需要运行这个代码如果他们试图替换第一个字符,我替换了哈希标记符号,第一个字符来自位置0和长度1的范围。因此,如果范围等于0,那么我们运行我们的代码来修复它。此代码还考虑到它们可能会粘贴特殊符号。因此,如果 UITextField 读取#work,并且他们尝试复制work或#work,则需要考虑这两种情况,如果哈希标记是,则完全跳过代码第一个字符。

With that, we only need to run this code if they are trying to replace the first character, i substituted the hash tag symbol, the first character is from a range of location 0 and length of 1. So if the range is equal to 0, then we run our code to fix it. This code also takes into consideration that they might be pasting the special symbol with it. so if the UITextField read #work, and they tried to copy "work" or "#work" it takes both scenarios into consideration and completely skips the code if the hash mark is the first character.

UITextField Reference

这篇关于向UITextField添加一个永久字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆