Swift4.0 全局修改字体 kvocontroller监听

参考 V2ex ->https://github.com/Finb/V2ex-Swift

在此进行了简化,适合萌新学习,包括我自己

import UIKit
import KVOController   //oc的框架

class TTTViewController: UIViewController {
    
    @IBOutlet weak var lb: UILabel!
    @IBAction func Largeaction(_ sender: Any) {
        
        model.setFontSize(size: Model.modelLarge)
        
        
    }
    
    
    @IBAction func MAction(_ sender: Any) {
        

        model.setFontSize(size: Model.modelMiddle)

    }
    
    
    @IBAction func sAction(_ sender: Any) {
        

        model.setFontSize(size: Model.modelSmall)

    }
    
    
    let model = Model.sharedInstance;

    override func viewDidLoad() {
        super.viewDidLoad()

        ///每个要改字体的页面要有这个block
        self.fontChangedHandler = { font in
            
            print("TTT字体大小改变:\(font)   ---------------- 5")

            self.lb.font = UIFont.systemFont(ofSize: CGFloat(font));
            
        }
        
    
    
    }

    override func touchesBegan(_ touches: Set, with event: UIEvent?) {
        
        self.dismiss(animated: true, completion: nil)
    }
}

import UIKit


protocol FontSizrProtocol {
    
    var sizeLarge:CGFloat {get}
    var sizeMiddle:CGFloat{get}
    var sizeSmall:CGFloat{get}
}


class Model: NSObject,FontSizrProtocol {
   
    
    static let sharedInstance = Model()
    override init() {
        
        FontSize = "Middle";
        super.init();

    }
    
    static let modelLarge = "Large"
    static let modelMiddle = "Middle"
    static let modelSmall = "Small"

    @objc dynamic var FontSize:String

   var font = 17;
    
    var sizeLarge: CGFloat{
        
        return 30;
    }
    
    var sizeMiddle: CGFloat{
        
        return 20;
    }
    
    var sizeSmall: CGFloat{
        
        return 10
    }
    

    func setFontSize(size:String) {
        
        if FontSize == size {
            
            return
        }
        
        
        if size == "Large"{
            font = 30;
        }
        
        if size == "Middle"{
            
            font = 20
        }
        
        
        if size == "Small" {
            
            font = 10
        }
        
        self.FontSize = size;
        
    }
    
}



extension NSObject {
    
    fileprivate struct AssociatedKeys {
        static var thmemChanged = "thmemChanged"
        static var fontChanged = "fontChanged"
    }
    
    
    
    public typealias FontChangedClosure = @convention(block) (_ fontSize:Float) ->Void
    
    
    var fontChangedHandler:FontChangedClosure?{
        
        get{
            print("-------------- get now  ---------- 4 ")
            
            let closureObject: AnyObject? = objc_getAssociatedObject(self, &AssociatedKeys.fontChanged) as AnyObject?
            guard closureObject != nil else{
                return nil
            }
            let closure = unsafeBitCast(closureObject, to: FontChangedClosure.self)
            return closure        }
        
        set{
            print("------------ set now -----------  2 ")
            guard let value = newValue else{
                return
            }
            
            let dealObject: AnyObject = unsafeBitCast(value, to: AnyObject.self)
            objc_setAssociatedObject(self, &AssociatedKeys.fontChanged,dealObject,objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
            
            
            self.kvoController.observe(Model.sharedInstance, keyPath: "FontSize", options: [.initial,.new] , block: {[weak self] (nav, color, change) -> Void in
                
                print("--------监听到了------  3")
                
                self?.fontChangedHandler?(Float(Model.sharedInstance.font))
                
                
                }
            )
            
        }
        
    }
    
    
}

你可能感兴趣的:(Swift4.0 全局修改字体 kvocontroller监听)