Hacer UITextField que tiene un prefijo estático y atribuido
Me gustaría hacer unUITextField
, que tiene un prefijo estático y que el usuariono poder Editar o eliminar, que al mismo tiempo también se atribuye con un color de fuente gris claro.
La parte editable del campo de texto siempre debe mostrarse en color negro.
A continuación se da un ejemplo:
Es esencialmente para escribir un nombre de usuario, con un dominio constantemente prefijado.
Ya he intentado usar eltextFieldShouldClear
ytextField:shouldChangeCharactersInRange:replacementString:
métodos de delegado junto conNSMutableAttributedString
, pero simplemente no han podido romperlo:
- (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;
}
Estoy seguro de que alguien ya ha hecho algo similar.