Ios 生成具有静态和属性前缀的UITextField
我想制作一个Ios 生成具有静态和属性前缀的UITextField,ios,uitextfield,nsmutableattributedstring,Ios,Uitextfield,Nsmutableattributedstring,我想制作一个UITextField,它有一个静态前缀,用户不能编辑或删除,同时它也带有浅灰色字体颜色 文本字段的可编辑部分应始终以黑色显示 下面给出了一个例子: 它基本上是用来输入一个用户名,并带有一个不断加前缀的域 我已经尝试将textField shouldClear和textField:shouldChangeCharactersRange:replacementString:委托方法与NSMutableAttributedString一起使用,但根本无法破解它: - (BOOL)te
UITextField
,它有一个静态前缀,用户不能编辑或删除,同时它也带有浅灰色字体颜色
文本字段的可编辑部分应始终以黑色显示
下面给出了一个例子:
它基本上是用来输入一个用户名,并带有一个不断加前缀的域
我已经尝试将
textField shouldClear
和textField:shouldChangeCharactersRange: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;
}
我确信已经有人做过类似的事情。简单地将
UILabel
(样式如您所愿)放在UITextField
的左侧,并为UITextField的
文本设置rect
下面的代码从永久文本中创建一个属性字符串,将其涂成灰色,并将其添加到文本字段中。然后在
shouldChangeCharactersRange:
中进行范围比较,以确定该范围是否在“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;
}
我已经尝试使用…
向我们展示,然后我们会发现错误。我希望您可以将UILabel
添加为UITextField
的leftView
,以显示不可编辑的文本。UITextField
没有contentInset
属性。是的,对不起,这是UITextView的,要实现这一点,对于UITextField,您应该将UITextField子类化。此解决方案快速且肮脏,并导致不适当的行为:编辑textfield时,标签始终可见,从而消除了无法编辑的简单前缀的错觉。这与我尝试过的非常接近。但是,当在textField:shouldChangeCharactersRange:
方法中设置attributedText
属性时,“光标”会移动到文本的前面。不,我没有。我刚刚尝试了一个“新鲜”文本字段——同样的事情也发生了。你有可能分享Xcode项目吗?谢谢!在iOS 7下工作对我来说也很好,而不是在iOS 6下?那将非常感谢!让我们
@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;
}