1. 程式人生 > >【30-swift-projects-in-30-days】swift 5 學習 01.CustomFont

【30-swift-projects-in-30-days】swift 5 學習 01.CustomFont

在 01.CustomFont 專案中主要學習的是自動佈局庫SnapKit的安裝和使用。

1.SnapKit安裝

    Podfile內容如下:

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '10.0'
use_frameworks!
target 'CustomFont' do
    pod 'SnapKit'
end

    在終端定位到Podfile所在目錄,執行pod install即可。

    若碰到SnapKit在swfit5環境下無法使用,可參考 

Conversion to Swift 5 is availiable 後SnapKit報錯 解決。

2.SnapKit的使用

    注意:必須先將控制元件新增到檢視中,再給控制元件新增約束。例如:    

        self.view.addSubview(label)
        label.snp.makeConstraints { (make) in
            make.top.equalTo(100)
            make.centerX.equalTo(self.view)
        }

屬性說明:

        .equalTo:等於
        .lessThanOrEqualTo:小於等於
        .greaterThanOrEqualTo:大於等於
        .edges::邊緣  
        .size:尺寸
        .center:中心
        .inset:內位移修正
        .offset:外位移修正
        .multipliedBy:倍率修正

基本使用:

        let viewOne = UIView()
        viewOne.snp.makeConstraints { (make) in
            make.left.top.equalToSuperview()
            make.width.height.equalTo(40)
        }
        //當前檢視與 title中心相同 (centerX 和 centerY)
        make.center.equalTo(title)
        //當前檢視寬高大於等於 title
        make.size.greaterThanOrEqualTo(title)
        //當前檢視 的 上下左右(top,left,bottom,right) 等於 title
        make.edges.equalTo(title)
        //當前檢視距離title檢視上、左、下、右邊距分別是20、20、20、30
        make.edges.equalTo(title).inset(UIEdgeInsetsMake(20, 20, 20, 30)) 
        //當前檢視為title檢視的一半
        make.size.equalTo(title).multipliedBy(0.5)



        //在父檢視中水平垂直居中
        make.center.equalTo(superview.snp.center)
        //在父檢視中水平垂直居中
        make.center.equalTo(superview)

更新、移除、重設約束:

        //儲存約束的引用
        var constraint:Constraint?
        title.snp.makeConstraints { (make) -> Void in
            self.constraint = make.width.height.equalTo(150).constraint
            make.center.equalTo(self.view)
        }
        
        //移除約束
        self.constraint?.deactivate()
        //1.更新修改約束
        self.constraint?.update(offset: 60)      
        //2.檢視約束更新
        override func updateViewConstraints() {
              self.title.snp.updateConstraints{ (make) -> Void in
              //檢視寬度與螢幕等寬
                    make.width.equalTo(self.view)
               }
              super.updateViewConstraints()
          }

         //重做約束
          title.snp.remakeConstraints { (make) -> Void in
              make.width.height.equalTo(100)
         }

ViewController原始碼如下:

import UIKit
import SnapKit

var isSystemFont:Bool = true;

class ViewController: UIViewController {
    let label = UILabel()

    override func viewDidLoad() {
        super.viewDidLoad()
        label.text = "I am the king of the world"
        label.textAlignment = NSTextAlignment.center
        self.view.addSubview(label)
        label.snp.makeConstraints { (make) in
            make.top.equalTo(100)
            make.centerX.equalTo(self.view)
        }
        label.font = UIFont.systemFont(ofSize: 30)
        
        let changeBtn = UIButton(type: .custom)
        changeBtn.setTitle("Change Font Family", for: UIControl.State.normal)
        changeBtn.addTarget(self, action: #selector(changeFontFamily), for: UIControl.Event.touchUpInside)
        changeBtn.setTitleColor(UIColor.blue, for: UIControl.State.normal)
        self.view.addSubview(changeBtn)
        changeBtn.layer.borderColor = UIColor.blue.cgColor
        changeBtn.layer.borderWidth = 1
        changeBtn.layer.cornerRadius = 5
        changeBtn.snp.makeConstraints { (make) in
            make.top.equalTo(500)
            make.centerX.equalTo(self.view)
            make.width.equalTo(200)
        }
        printAllSupportedFontNames()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @objc func changeFontFamily() {
        if isSystemFont {
            label.font = UIFont(name: "Savoye LET", size: 30)
            isSystemFont = false
        }else{
            label.font = UIFont.systemFont(ofSize: 30)
            isSystemFont = true
        }
        
    }
    
    func printAllSupportedFontNames() {
        let familyNames = UIFont.familyNames
        for familyName in familyNames {
            print("++++++ \(familyName)")
            let fontNames = UIFont.fontNames(forFamilyName: familyName)
            for fontName in fontNames {
                print("----- \(fontName)")
            }
        }
    }

}

學習原始碼來自 https://github.com/nimomeng/30-swift-projects-in-30-days ,其Swift語言版本為 4.1 。

學習過程我會將程式碼語言版本改成Swift 5,程式碼地址:https://github.com/dong706/30-swift-projects-in-30-days/ 。

 

參考文章:iOS開發Snapkit的使用

               使用SnapKit時 center 和snp