gpt4 book ai didi

iphone - 向 UITextField 添加不可编辑的文本后缀

转载 作者:可可西里 更新时间:2023-11-01 03:09:40 25 4
gpt4 key购买 nike

我有一个 UITextField,我想添加一个“?”所有输入文本的后缀。

用户不应该删除这个“?”或在其右侧添加文本。

解决这个问题的最佳方法是什么?

最佳答案

使用 UITextFieldDelegate每当编辑字段时更改字符串的协议(protocol)。这是一个快速的尝试;这需要一些工作,但它应该可以帮助您入门。

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString * currentText = [textField text];
if( [currentText characterAtIndex:[currentText length] - 1] != '?' ){
NSMutableString * newText = [NSMutableString stringWithString:currentText];
[newText replaceCharactersInRange:range withString:string];
[newText appendString:@"?"];
[textField setText:newText];
// We've already made the replacement
return NO;
}
// Allow the text field to handle the replacement
return YES;
}

关于iphone - 向 UITextField 添加不可编辑的文本后缀,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8288278/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com