[英]Adding an uneditable text suffix to a UITextField
我有一個UITextField,我想添加一個“?” 輸入的所有文本的后綴。
用戶不應該刪除此“?” 或在其右側添加文本。
最好的方法是什么?
每當編輯字段時,使用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;
}
您可能需要繼承UITextField並覆蓋其drawText:
方法以繪制一個額外的“?” 實際文本右側的字符。 (而不是實際在視圖文本中添加“?”。
我有這個問題,我寫了一個子類來添加這個功能: https : //github.com/sbaumgarten/UIPlaceholderSuffixField 。 希望你現在已經找到了解決方案,但如果你沒有,這應該可行。
我意識到這個答案已經很晚了,但我發現其中大部分都不適合我的方案。 我有一個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...
}
這應該適用於您分配給文本字段的任何后綴值。
好吧,我肯定已經太晚了,但也許我可以幫助某人出去:實現這一目標的預期方法是使用自定義的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方便。
我在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;
}
我編寫了以下方法來實現將不可編輯的后綴放到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];
}
對於單行UITextField,您應該能夠測量NSString的大小(它在某處有一個測量函數)並將UILabel移動到正確的位置。
我會添加一個在編輯完成時調用的方法:
`- (void)editDidFinish {
NSString* str=[[NSString alloc] init];
str=myEdit.text;
[str stringByAppendingString:@"?"];
myEdit.text=str;
}`
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.