In the 01.CustomFont project, the main learning is the installation and use of SnapKit, the automatic layout library.
1.SnapKit installation
The content of Podfile is as follows:
source 'https://github.com/CocoaPods/Specs.git' platform :ios, '10.0' use_frameworks! target 'CustomFont' do pod 'SnapKit' end
Locate the directory of Podfile in the terminal and execute pod install.
If SnapKit cannot be used in swfit5 environment, please refer to SnapKit error after conversion to swift 5 is available Solve.
2. Use of snapkit
Note: you must add the control to the view before adding constraints to the control. For example:
self.view.addSubview(label) label.snp.makeConstraints { (make) in make.top.equalTo(100) make.centerX.equalTo(self.view) }
Property Description:
. equalTo: equal to . lessthankorequalto: less than or equal to . greaterthankorequalto: greater than or equal to . edges:: edge . size: size . center: center . inset: internal displacement correction . offset: external displacement correction . multiplicedby: multiplier correction
Basic use:
let viewOne = UIView() viewOne.snp.makeConstraints { (make) in make.left.top.equalToSuperview() make.width.height.equalTo(40) } //The current view is the same as the title Center (centerX and center) make.center.equalTo(title) //The width and height of the current view is greater than or equal to the title make.size.greaterThanOrEqualTo(title) //The top, left, bottom, right of the current view is equal to the title make.edges.equalTo(title) //The distance between the current view and the title view is 20, 20, 20 and 30, respectively make.edges.equalTo(title).inset(UIEdgeInsetsMake(20, 20, 20, 30)) //The current view is half of the title View make.size.equalTo(title).multipliedBy(0.5) //Center horizontally and vertically in the parent view make.center.equalTo(superview.snp.center) //Center horizontally and vertically in the parent view make.center.equalTo(superview)
Update, remove, and reset constraints:
//Save reference to constraint var constraint:Constraint? title.snp.makeConstraints { (make) -> Void in self.constraint = make.width.height.equalTo(150).constraint make.center.equalTo(self.view) } //Remove constraints self.constraint?.deactivate() //1. Update modification constraints self.constraint?.update(offset: 60) //2. View constraint update override func updateViewConstraints() { self.title.snp.updateConstraints{ (make) -> Void in //View width equal to screen width make.width.equalTo(self.view) } super.updateViewConstraints() } //Redo constraint title.snp.remakeConstraints { (make) -> Void in make.width.height.equalTo(100) }
The source code of ViewController is as follows:
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)") } } } }
Learning source code from https://github.com/nimomeng/30-swift-projects-in-30-days , its Swift language version is 4.1.
During the learning process, I will change the code language version to Swift 5, code address: https://github.com/dong706/30-swift-projects-in-30-days/ .
Reference article: The use of Snapkit in iOS development
The difference between center and snp.center when using SnapKit