使UITextField有一个静态和属性前缀

我想创build一个UITextField ,它有一个静态的前缀,并且用户不能编辑或删除,同时它也被归为浅灰色的字体颜色。

文本字段的可编辑部分应始终以黑色显示。

下面给出一个例子:

在这里输入图像说明

它本质上是用于input一个用户名,带有一个不断加前缀的域。


我已经尝试使用textFieldShouldCleartextField:shouldChangeCharactersInRange:replacementString:NSMutableAttributedString一起委托方法,但根本无法破解它:

 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { NSMutableAttributedString *text = [textField.attributedText mutableCopy]; [text addAttribute:NSForegroundColorAttributeName value:[UIColor grayColor] range:NSMakeRange(0, 7)]; if (textField.text.length > 7) { [text addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(7, textField.attributedText.length - 7)]; } [text addAttribute:NSFontAttributeName value:[UIFont gothamFontForSize:textField.font.pointSize andWeight:kGothamLight] range:NSMakeRange(0, textField.attributedText.length)]; return range.location > 6; } - (BOOL)textFieldShouldClear:(UITextField *)textField { NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:@"kombit\\"]; [text addAttribute:NSForegroundColorAttributeName value:[UIColor grayColor] range:NSMakeRange(0, 7)]; [text addAttribute:NSFontAttributeName value:[UIFont gothamFontForSize:textField.font.pointSize andWeight:kGothamLight] range:NSMakeRange(0, text.length)]; textField.attributedText = text; [textField setSelectedTextRange:[textField textRangeFromPosition:[textField endOfDocument] toPosition:[textField endOfDocument]]]; return NO; } 

我确定有人已经做了类似的事情。

下面的代码从永久文本中创build一个属性string,将其变为灰色,并将其添加到文本字段中。 然后在shouldChangeCharactersInRange:进行范围比较,以确定范围是否在应该由“kombit \”占据的区域内,如果不是,则将黑色着色属性传回到文本字段。

 - (void)viewDidLoad { [super viewDidLoad]; [self.textField setDelegate:self]; NSString *fixedString = @"kombit\\"; NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:fixedString]; [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor lightGrayColor] range:NSMakeRange(0, fixedString.length)]; [self.textField setAttributedText:attributedString]; } - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { NSRange substringRange = [textField.text rangeOfString:@"kombit\\"]; if (range.location >= substringRange.location && range.location < substringRange.location + substringRange.length) { return NO; } NSMutableAttributedString *attString = [textField.attributedText mutableCopy]; [attString addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(substringRange.length, textField.text.length - substringRange.length)]; [textField setAttributedText:attString]; return YES; } 

编辑:

好吧,这不是我曾经预料过的那种痛苦,坦率地说,我很尴尬地发布这个代码,因为它真的很脏,但它似乎比以前做得更好。

我最终做的是创build一个引用BOOL的属性,以保持文本是否应该更新(阻止recursion),并向文本字段的UIControlEventEditingChanged控件事件添加一个目标,以便在发生编辑时获得callback。

然后,由于某种原因,没有一个正常的做法似乎在这里工作,我正在移动“游标”到文本字段的结尾,直接调用resFirstResponder之后成为FirstFirstResponder,令人惊讶的这是相当多的工作。 有一个问题,文字大小略有变化,我还没有识别,但它应该只是添加额外的属性来匹配所需的字体大小的问题

对不起,我不能再解决这个问题了,这是一个很好奇的问题。 而且我通常不愿提出这个build议,但是花了这么多时间去做一些应该是微不足道的事情之后,我开始认为在NSAttributedString中有一些bug。 无论如何,让我知道如果这个代码最终导致你的答案,我会真正感兴趣的解决scheme。

 @property (weak, nonatomic) IBOutlet UITextField *textField; @property (nonatomic, assign) BOOL shouldUpdateAttributes; - (void)viewDidLoad { [super viewDidLoad]; [self.textField setDelegate:self]; [self.textField setSpellCheckingType:UITextSpellCheckingTypeNo]; [self setShouldUpdateAttributes:YES]; NSString *fixedString = @"kombit\\ "; [self.textField setText:fixedString]; [self.textField addTarget:self action:@selector(textFieldDidChangeText:) forControlEvents:UIControlEventEditingChanged]; [self textFieldDidChangeText:self.textField]; } - (void)textFieldDidChangeText:(UITextField *)sender { if (self.shouldUpdateAttributes) { [self setShouldUpdateAttributes:NO]; NSString *fixedString = @"kombit\\ "; NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:sender.text]; [attributedString setAttributes:@{NSForegroundColorAttributeName: [UIColor lightGrayColor]} range:NSMakeRange(0, fixedString.length)]; [attributedString setAttributes:@{NSForegroundColorAttributeName: [UIColor blackColor]} range:NSMakeRange(fixedString.length, sender.text.length - fixedString.length)]; [sender setAttributedText:attributedString]; [sender resignFirstResponder]; [sender becomeFirstResponder]; } } - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { [self setShouldUpdateAttributes:YES]; NSString *fixedString = @"kombit\\"; NSRange substringRange = [textField.text rangeOfString:fixedString]; if (range.location >= substringRange.location && range.location < substringRange.location + substringRange.length) { return NO; } return YES; } 

简单地把UILabel (像你想的样式)放在UITextField的左边,并为UITextField's文本设置rect。

http://alwawee.com/wordpress/2013/02/18/uitextfield-edge-insets/