Swift 设置UITextField的placeholder的字体大小、颜色

扩展类代码

//
//  UITextField+Extension.swift
//  KriFationClient
//  Created by qingxun on 2017/9/5.
//  Copyright © 2017年 BruceLv. All rights reserved.
//

import Foundation
import UIKit

extension UITextField{

    //MARK:-设置暂位文字的颜色
    var placeholderColor:UIColor {

        get{
            let color =   self.value(forKeyPath: "_placeholderLabel.textColor")
            if(color == nil){
                return UIColor.white;
            }
            return color as! UIColor;

        }

        set{

          self.setValue(newValue, forKeyPath: "_placeholderLabel.textColor")
        }


    }

//MARK:-设置暂位文字的字体
    var placeholderFont:UIFont{
        get{
            let font =   self.value(forKeyPath: "_placeholderLabel.font")
            if(font == nil){
                return UIFont.systemFont(ofSize: 14);
            }
            return font as! UIFont;
        }


        set{


         self.setValue(newValue, forKeyPath: "_placeholderLabel.font")
        }

    }


}

调用示例

        let tf = UITextField()
        tf.placeholder = "一段暂位文字"
        tf.placeholderFont = FONT(32);
        tf.placeholderColor = UIColor.white;
        tf.textAlignment = .center;
        tf.textColor = .white;

你可能感兴趣的:(Swift)