将不可编辑的文本后缀添加到UITextField

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

用户不应该删除此“?” 或者在其右侧添加文字。

最好的方法是什么?

您可能需要inheritanceUITextField并覆盖其drawText:方法以绘制一个额外的“?” 实际文本右侧的字符。 (而不是实际在视图文本中添加“?”。

每当编辑字段时,使用UITextFieldDelegate协议来更改字符串。 这是一个快速刺伤它; 这将需要工作,但它应该让你开始。

 - (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; } 

我有这个问题,我写了一个子类来添加这个function: https : //github.com/sbaumgarten/UIPlaceholderSuffixField 。 希望你现在已经找到了解决方案,但如果你没有,这应该可行。

对于单行UITextField,您应该能够测量NSString的大小(它在某处有一个测量函数)并将UILabel移动到正确的位置。

我会添加一个在编辑完成时调用的方法:

 `- (void)editDidFinish { NSString* str=[[NSString alloc] init]; str=myEdit.text; [str stringByAppendingString:@"?"]; myEdit.text=str; }` 

我意识到这个答案已经很晚了,但我发现其中大部分都不适用于我的场景。 我有一个UITextField,我只想强制拥有一个用户无法编辑的后缀。 但是,我不想子类UITextView,修改它如何处理绘图等。我只是想阻止用户修改后缀。

首先,我确保在编辑时在文本字段中设置后缀。 这可以通过多种方式完成,具体取决于您的方案。 对于我的,我从一开始就想要它,所以我只是在视图加载时将textfield的text属性设置为等于后缀,然后将后缀的长度存储起来。 例如:

 - (void)viewDidLoad { [super viewDidLoad]; myTextField.text = "suffix"; _suffixLength = myTextField.text.length; } 

然后我使用了上面提到的Josh建议的UITextFieldDelegate协议,但是使用字符串的长度和范围来确保没有编辑后缀:

 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { // Determine starting location of the suffix in the current string int suffixLocation = textField.text.length - _suffixLength; // Do not allow replacing anything in/past the suffix if (range.location + range.length > suffixLocation) { return NO; } // Continue with delegate code... } 

这适用于您分配给文本字段的任何后缀值。

我用Objective-C编写的这个类方法可以帮助你向UITextField添加后缀文本。 为了使其正常工作,您需要将UILabel初始化为UITextFieldLabel的前缀或后缀,如下所示:

 myTextField.rightView = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 0.0f, myTextField.frame.size.height)]; myTextField.rightViewMode = UITextFieldViewModeAlways; [MyClass UpdateUITextFieldSuffix:myTextField withString:@"My Suffix!"]; 

一旦我们将UILabel附加到UITextField ,您就可以使用此类方法更新文本,并且此文本将自动resize以适合该字段。

 + (BOOL)UpdateUITextFieldSuffix:(UITextField*)textField withString:(NSString*)string { BOOL returnUpdateSuffix = NO; if (string != nil && [string respondsToSelector:@selector(length)] && [string length] > 0) { NSObject *labelSuffix = textField.rightView; if (labelSuffix != nil && [labelSuffix respondsToSelector:@selector(setText:)]) { [(UILabel*)labelSuffix setTextAlignment:NSTextAlignmentRight]; [(UILabel*)labelSuffix setText:string]; [(UILabel*)labelSuffix setBackgroundColor:[UIColor redColor]]; { NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys: ((UILabel*)labelSuffix).font, NSFontAttributeName,nil]; CGRect frame = [((UILabel*)labelSuffix).text boundingRectWithSize:CGSizeMake(0.0f, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributesDictionary context:nil]; CGSize size = frame.size; CGRect newFrame = [(UILabel*)labelSuffix frame]; newFrame.size.width = size.width; [(UILabel*)labelSuffix setFrame:newFrame]; [(UILabel*)labelSuffix setNeedsLayout]; [(UILabel*)labelSuffix layoutIfNeeded]; } returnUpdateSuffix = YES; } } return returnUpdateSuffix; } 

我编写了以下方法来实现将不可编辑的后缀放到UITextField的上述任务:

  - (void)setSuffixText:(NSString *)suffix { UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero]; [label setBackgroundColor:[UIColor clearColor]]; [label setFont:[UIFont fontWithName:self.tfdDistance.font.fontName size:self.tfdDistance.font.pointSize]]; [label setTextColor:self.tfdDistance.textColor]; [label setAlpha:.5]; [label setText:suffix]; CGSize suffixSize = [suffix sizeWithFont:label.font]; label.frame = CGRectMake(0, 0, suffixSize.width, self.tfdDistance.frame.size.height); [self.tfdDistance setRightView:label]; [self.tfdDistance setRightViewMode:UITextFieldViewModeAlways]; }​ 

好吧,我肯定太晚了,但也许我可以帮助某人出去:实现这一目标的预期方法是使用自定义的NSFormatter。 inheritance人的文档: https : //developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSFormatter_Class/Reference/Reference.html

基本思想是:您创建NSFormatter的子类,并至少覆盖两个工作方法:

 -stringObjectForValue: 

这将从存储在对象中的值生成dipsplay-String(即在此处添加问号)

 -objectValue:ForString:errorDescription 

在这里,您需要将显示字符串转换为您要存储的对象,即删除问号

然后,格式化程序可用于将来自存储对象的数据转换为适合呈现给用户的字符串。 最大的优点是,只要字符串出现在UI中,您就可以使用格式化程序。 它不限于某些UI元素,例如您在UITextField中覆盖-drawText的解决方案。 它只是hella方便。