美文网首页APP设计模式
iOS 改变UITextField placeholder颜

iOS 改变UITextField placeholder颜

作者: _Waiting_ | 来源:发表于2020-07-23 15:32 被阅读0次

    iOS11之后,使用UITextField时,它的私有属性_placeholderLabel被禁止访问了。所以我们无法像以前一样使用如下方法改变placeholderLabel的属性了

    [textField setValue:[UIColor greenColor] forKeyPath:@"_placeholderLabel.textColor"];

    How do we do?

    可以利用runtime进行解决。
    代码如下:

    OC

    .h

    #import <UIKit/UIKit.h>
    
    NS_ASSUME_NONNULL_BEGIN
    
    @interface HanPlaceholderTextField : UITextField
    @property(nonatomic, strong) UIColor *placeholderColor;
    @property(nonatomic, strong) UIFont  *placeholderFont;
    @end
    

    .m

    #import "HanPlaceholderTextField.h"
    #import <objc/runtime.h>
    
    @interface HanPlaceholderTextField ()
    @end
    
    @implementation HanPlaceholderTextField
    -(void)changePlaceholder{
        Ivar ivar = class_getInstanceVariable([UITextField class], "_placeholderLabel");
        UILabel *placeholderLabel = object_getIvar(self, ivar);
        placeholderLabel.textColor = _placeholderColor;
        placeholderLabel.font = _placeholderFont;
    }
    -(void)setPlaceholderColor:(UIColor *)placeholderColor{
        _placeholderColor = placeholderColor;
        [self changePlaceholder];
    }
    -(void)setPlaceholder:(NSString *)placeholder{
        [super setPlaceholder:placeholder];
        [self changePlaceholder];
    }
    -(void)setPlaceholderFont:(UIFont *)placeholderFont{
        _placeholderFont = placeholderFont;
        [self changePlaceholder];
    }
    @end
    

    用法

    ...
    #import "HanPlaceholderTextField.h"
    ...
    
    ....
    HanPlaceholderTextField *textField = [[HanPlaceholderTextField alloc] initWithFrame:CGRectMake(0, 100, [UIScreen mainScreen].bounds.size.width, 40)];
    textField.placeholderColor = [UIColor redColor];
    textField.placeholderFont = [UIFont systemFontOfSize:10];
    textField.textColor = [UIColor blueColor];
    textField.placeholder = @"你好";
    [self.view addSubview:textField];
    ....
    

    Swift

    import UIKit
    
    class HanPlaceholderTextField: UITextField {
        
        public var placeholderColor:UIColor?{
            didSet{
                self.changePlaceholder();
            }
        }
        public var placeholderFont:UIFont?{
            didSet{
                self.changePlaceholder();
            }
        }
        
        override var placeholder: String?{
            didSet{
                let p = placeholder
                self.changePlaceholder()
                super.placeholder = p
            }
        }
    
        private func changePlaceholder(){
            let ivar = class_getInstanceVariable(object_getClass(UITextField()), "_placeholderLabel")
            let placeholderLabel  = object_getIvar(self, ivar!) as? UILabel
            placeholderLabel?.textColor = placeholderColor
            placeholderLabel?.font = placeholderFont
        }
        
        
    
    }
    

    用法

    import UIKit
    
    class ViewController: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view.
            
            
            
            let textField:HanPlaceholderTextField = HanPlaceholderTextField.init(frame: CGRect.init(x: 0, y: 100, width: UIScreen.main.bounds.size.width, height: 44))
            textField.backgroundColor = UIColor.black
            textField.placeholderColor = UIColor.red
            textField.placeholderFont =  UIFont.systemFont(ofSize: 10)
            textField.textColor = UIColor.blue
            textField.placeholder = "你好";
            self.view.addSubview(textField)
        }
    
    
    }
    

    =====================

    发现有一个神奇的属性
    textField.attributedPlaceholder
    这个属性可以利用富文本改变textField的Placeholder

    相关文章

      网友评论

        本文标题:iOS 改变UITextField placeholder颜

        本文链接:https://www.haomeiwen.com/subject/veeclktx.html