继续做仿造着别人的第二个
1.首先下载 一些字体 网上搜索 "造字工房"
2.把下载的相应字体文件放到工程之中,就Ok了
不多说 效果如下
可以下面这个方法 检索项目里面所有的字体
for family in UIFont.familyNames { for font in UIFont.fontNames(forFamilyName: family) { print(font) } }
代码如下
import UIKit class ViewController: UIViewController { lazy var contentLabel = UILabel()
lazy var changeBtn = UIButton()
var tag: Int = override func viewDidLoad() {
super.viewDidLoad() /* for family in UIFont.familyNames { for font in UIFont.fontNames(forFamilyName: family) { print(font) } }*/ self.view.backgroundColor = UIColor.black
contentLabel.frame = CGRect(x: , y:, width:UIScreen.main.bounds.size.width - , height: )
contentLabel.numberOfLines =
contentLabel.textColor = UIColor.white
contentLabel.font = UIFont.systemFont(ofSize: )
self.view.addSubview(contentLabel)
let content: String = "30 Days Swift\n\n 目前授权个人免费非商业使用\n\n 所以捐款了1元下了3款字体用来做试验\n\n 分别是造字工房劲黑,致黑和童心;"
contentLabel.text = content changeBtn.backgroundColor = UIColor.orange
changeBtn.frame.size = CGSize(width: , height: )
changeBtn.frame.origin = CGPoint(x: UIScreen.main.bounds.size.width/ - , y: UIScreen.main.bounds.size.height - )
changeBtn.setTitle("改变字体", for: .normal)
changeBtn.layer.masksToBounds = true
changeBtn.layer.cornerRadius = 50.0
changeBtn.addTarget(self, action: #selector(changeFont), for: .touchUpInside)
self.view.addSubview(changeBtn)
} //MARK: - 点击事件
func changeFont() {
var fontName:String
switch tag {
case :
fontName = "MFQingShu_Noncommercial-Regular"
tag =
case :
fontName = "MFYueYuan_Noncommercial-Regular"
tag =
case :
fontName = "MFWenYan_Noncommercial-Regular"
tag =
default:
fontName = "AppleSDGothicNeo-Regular"
tag =
} contentLabel.font = UIFont.init(name: fontName, size: )
} override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
} }