The text color starts out as red and changes to blue after clearing the text.
@interface ViewController ()
@property (nonatomic, strong) UITextField *textField;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.textField = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 100, 30)];
self.textField.textColor = UIColor.blueColor;
self.textField.text = @"hello world";
NSAttributedString *attributedText = self.textField.attributedText;
NSMutableAttributedString *mAttributedText = attributedText.mutableCopy;
[attributedText enumerateAttribute:NSForegroundColorAttributeName inRange:NSMakeRange(0, attributedText.length) options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired usingBlock:^(id _Nullable value, NSRange range, BOOL * _Nonnull stop) {
[mAttributedText addAttribute:NSForegroundColorAttributeName value:UIColor.redColor range:range];
}];
self.textField.attributedText = mAttributedText;
[self.view addSubview:self.textField];
}
@end
The expectation is that the text should always remain red. Why does UITextField textColor changed after clearing the text?