Zap*_*pel 2 uitextfield ios nsmutableattributedstring
我想做一个UITextField,它有一个静态前缀,用户不能编辑或删除,同时也用浅灰色字体颜色.
文本字段的可编辑部分应始终以黑色显示.
下面给出一个例子:

它主要用于输入用户名,带有不断添加前缀的域.
我已经尝试过使用textFieldShouldClear和textField: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;
}
Run Code Online (Sandbox Code Playgroud)
我确信有人已经做过类似的事了.
下面的代码从您的永久文本中创建一个属性字符串,将其颜色为灰色,并将其添加到文本字段中.然后在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;
}
Run Code Online (Sandbox Code Playgroud)
编辑:
好吧,这不是我曾经期望的那么大的痛苦,坦率地说,我很尴尬发布这个代码,因为它真的很脏,但它似乎确实比以前做得更好.
我最终做的是创建一个属性来引用BOOL以保存文本是否应该更新(阻止递归)并将目标添加到文本字段的UIControlEventEditingChanged控件事件以在编辑时进行回调.
然后,因为出于某种原因,没有任何正常的方法似乎在这里工作,我通过在调用resignFirstResponder之后直接调用becomeFirstResponder将"光标"移动到文本字段的末尾,并且令人惊讶地这非常有效.我还没有确定文本大小略有变化的问题,但它应该只是添加额外的属性以匹配您想要的字体大小
对不起,我无法帮助解决这个问题,这是一个非常奇怪的问题.而且我通常不愿意这样做,但是在花了那么多时间试图做一些应该是微不足道的事情后,我开始认为NSAttributedString中有一些错误.无论如何,让我知道如果这段代码最终导致你得到答案,我真的对解决方案感兴趣.
@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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3378 次 |
| 最近记录: |