Swift UITExtField의 placeholder 글꼴 크기, 색상 설정

2914 단어 Swift

확장 클래스 코드

//
//  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;

좋은 웹페이지 즐겨찾기