我有一个UITextField,我想添加一个“?”输入所有文字的后缀。
用户不应该删除此“?”或在其右侧添加文本。
最好的方法是什么?
答案 0 :(得分:4)
使用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;
}
答案 1 :(得分:2)
您可能需要继承UITextField并覆盖其drawText:
方法以绘制额外的“?”实际文本右侧的字符。 (而不是实际在视图文本中添加“?”。
答案 2 :(得分:1)
我有这个问题,我写了一个子类来添加这个功能:https://github.com/sbaumgarten/UIPlaceholderSuffixField。 希望你现在已经找到了解决方案,但如果你没有,这应该可行。
答案 3 :(得分:0)
对于单行UITextField,您应该能够测量NSString的大小(它在某处有一个测量函数)并将UILabel移动到正确的位置。
答案 4 :(得分:0)
我会添加一个在编辑完成时调用的方法:
`- (void)editDidFinish {
NSString* str=[[NSString alloc] init];
str=myEdit.text;
[str stringByAppendingString:@"?"];
myEdit.text=str;
}`
答案 5 :(得分:0)
我意识到这个答案已经很晚了,但我发现其中大部分都不符合我的情况。我有一个UITextField,我只想强制拥有一个用户无法编辑的后缀。但是,我不想继承UITextView,修改它如何处理绘图等。我只是想阻止用户修改后缀。
首先,我确保在编辑时在文本字段中设置后缀。这可以通过多种方式完成,具体取决于您的方案。对于我的,我从一开始就想要它,所以我只是在视图加载时将textfield的text属性设置为等于后缀,然后将后缀的长度存储起来。例如:
- (void)viewDidLoad {
[super viewDidLoad];
myTextField.text = "suffix";
_suffixLength = myTextField.text.length;
}
然后我使用了UITextFieldDelegate协议,就像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...
}
这适用于您分配给文本字段的任何后缀值。
答案 6 :(得分:0)
我用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
后,您可以使用此类方法更新文本,此文本将自动调整大小以适合该字段。
+ (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;
}
答案 7 :(得分:0)
我编写了以下方法来实现将不可编辑的后缀放到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];
}
答案 8 :(得分:-1)
好的,我确定为时已晚,但也许我可以帮助某人: 实现此目的的方法是使用自定义NSFormatter。继承人的文件: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSFormatter_Class/Reference/Reference.html
基本思路是这样的:你创建一个NSFormatter的子类,并至少覆盖两个worker方法:
-stringObjectForValue:
这将从存储在对象中的值产生dipsplay-String(即在此处添加问号)
-objectValue:ForString:errorDescription
在这里,您需要将显示字符串转换为您要存储的对象,即删除问号
然后,格式化程序可用于将存储对象中的数据转换为适合呈现给用户的字符串。 最大的优点是,只要字符串出现在UI中,您就可以使用格式化程序。它不限于某些UI元素,例如您在UITextField中覆盖-drawText的解决方案。它只是hella方便。