iOS经典讲解之通过setValue: forKeyPath:设置属性(iOS 13不支持该方式修改私有属性!!!)

 UITextField的placeholder的颜色和字体:

1、方案一(iOS13已不支持该方案,提倡方案二)
textField.placeholder = @"请输入用户名!";  
[textField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];  
[textField setValue:[UIFont boldSystemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"];

2、方案二 iOS6.0之后提供的attributedPlaceholder属性:
NSString *holderText = @"请输入密码!";
NSMutableAttributedString *placeholder = [[NSMutableAttributedString alloc] initWithString:holderText];
[placeholder addAttribute:NSForegroundColorAttributeName
              value:[UIColor redColor]
              range:NSMakeRange(0, holderText.length)];
[placeholder addAttribute:NSFontAttributeName
              value:[UIFont boldSystemFontOfSize:16]
              range:NSMakeRange(0, holderText.length)];
textField.attributedPlaceholder = placeholder;

UIAlertController 设置title、message 字体及颜色 

//修改title
    NSMutableAttributedString *alertControllerStr = [[NSMutableAttributedString alloc] initWithString:@"提示"];
    [alertControllerStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 2)];
    [alertControllerStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:17] range:NSMakeRange(0, 2)];
    [alertController setValue:alertControllerStr forKey:@"attributedTitle"];

//修改message
    NSMutableAttributedString *alertControllerMessageStr = [[NSMutableAttributedString alloc] initWithString:@"提示内容"];
    [alertControllerMessageStr addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(0, 4)];
    [alertControllerMessageStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20] range:NSMakeRange(0, 4)];
    [alertController setValue:alertControllerMessageStr forKey:@"attributedMessage"];

//修改按钮
    if (cancelAction valueForKey:@"titleTextColor") {
        [cancelAction setValue:[UIColor redColor] forKey:@"titleTextColor"];
    }

 

你可能感兴趣的:(Objective-C,iOS)