Iphone 向UITextField添加不可编辑的文本后缀
我有一个UITextField,我想在输入的所有文本中添加一个“?”后缀 用户不能删除此“?”或在其右侧添加文本Iphone 向UITextField添加不可编辑的文本后缀,iphone,objective-c,ios,uitextfield,Iphone,Objective C,Ios,Uitextfield,我有一个UITextField,我想在输入的所有文本中添加一个“?”后缀 用户不能删除此“?”或在其右侧添加文本 最好的方法是什么?您可能需要将UITextField子类化并重写其drawText:方法,以便在实际文本的右侧绘制一个额外的“?”字符。(而不是在视图的文本中实际添加“?”。对于单行UITextField,您应该能够测量NSString的大小(它在其中的某处具有测量功能)并将UILabel移动到正确的位置。我将添加一个在编辑完成时调用的方法: `- (void)editDidFini
最好的方法是什么?您可能需要将UITextField子类化并重写其
drawText:
方法,以便在实际文本的右侧绘制一个额外的“?”字符。(而不是在视图的文本中实际添加“?”。对于单行UITextField,您应该能够测量NSString的大小(它在其中的某处具有测量功能)并将UILabel移动到正确的位置。我将添加一个在编辑完成时调用的方法:
`- (void)editDidFinish {
NSString* str=[[NSString alloc] init];
str=myEdit.text;
[str stringByAppendingString:@"?"];
myEdit.text=str;
}`
无论何时编辑字段,都可以使用协议更改字符串。这里有一个快速尝试;这需要做一些工作,但应该可以让您开始
- (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;
}
我有这个问题,我写了一个子类来添加这个功能:。
希望您现在已经找到了解决方案,但如果您还没有找到,这应该会起作用。好的,我肯定太迟了,但也许我可以通过任何一种方式帮助他人: 实现这一点的预期方法是使用自定义NSFormatter。以下是文档: 其基本思想是:创建NSFormatter的子类,并重写至少两个辅助方法:
-stringObjectForValue:
这将根据存储在对象中的值生成dipsplay字符串(即,在此处添加问号)
这里,您需要将显示字符串转换为要存储的对象,即删除问号
然后可以使用格式化程序将存储对象中的数据转换为适合向用户显示的字符串。
最大的优点是,无论字符串出现在UI中的什么位置,都可以使用格式化程序。它不仅限于某些UI元素,例如在UITextField中覆盖-drawText的解决方案。这非常方便。我意识到这个答案很晚,但我发现大多数都不适用于我的场景。我有一个UITextField我想强制使用用户无法编辑的后缀。但是,我不想子类化UITextView,修改它处理图形的方式,等等。我只想阻止用户修改后缀 首先,我确保在进行编辑时在textfield中设置后缀。根据您的场景,可以通过多种方式进行设置。对于我的情况,我希望从一开始就设置后缀,因此我只需将textfield的text属性设置为视图加载时的后缀,并存储后缀的长度以备以后使用。例如:
- (void)viewDidLoad {
[super viewDidLoad];
myTextField.text = "suffix";
_suffixLength = myTextField.text.length;
}
然后,我按照Josh的建议使用了协议,但使用字符串的长度和范围来确保不编辑后缀:
- (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...
}
这应该适用于您分配给textfield的任何后缀值。我用Objective-C编写的这个类方法可以帮助您将后缀文本添加到
UITextField
。
为了使其正常工作,您需要将UILabel
初始化为uiextfieldlabel
中的前缀或后缀,如下所示:
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
后,您可以使用此类方法更新文本,并且此文本将自动调整大小以适合该字段
+ (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];
}
与我自己的解决方案相比,我更喜欢这个解决方案。+1:-)Jesse,我们如何绘制这个字符?@GabrielMolter,使用任何一个普通的UIKit字符串绘制API。如果能给出一个工作示例而不是一些提示来说明这在理论上应该是如何实现的,那就太好了。当在UITextField中添加不可编辑的后缀时,我真的不知道如何使用NSFormatter
- (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];
}