gpt4 book ai didi

ios - 制作具有静态和属性前缀的 UITextField

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:02:53 28 4
gpt4 key购买 nike

我想制作一个UITextField,它有一个静态前缀,用户不能编辑或删除,同时也带有一个灯灰色字体颜色。

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

例子如下:

enter image description here

它本质上是用于输入用户名,并带有一个固定的前缀域。


我已经尝试过将 textFieldShouldCleartextField:shouldChangeCharactersInRange:replacementString: 委托(delegate)方法与 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;
}

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

最佳答案

下面的代码从您的永久文本中创建一个属性字符串,将其着色为灰色,并将其添加到文本字段中。然后在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;
}

编辑:

好吧,我没想到会这么痛苦,坦率地说,我不好意思发布这段代码,因为它真的很脏,但它确实比以前做得更好.

我最后做的是创建一个属性来引用 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;
}

关于ios - 制作具有静态和属性前缀的 UITextField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18333052/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com