iOS數碼管顯示數字(Swift)

寫這個demo是在Xcode11beta工具中創建的,可能看起來結果不像之前Xcode創建的項目那麼順眼,這不重要,重要的是實現數字時鐘的方法與Xcode *無關。JKDigitalFont 下載

1. 在項目中把DigitalFont文件夾中的4個字體文件.TTF文件引入項目

2. 在項目的info.plist文件中添加key爲 “Fonts provided by application”類型爲array,在該key下添加4個item(值爲4個字體文件名),類型爲String

3. 這就是使用的地方了,可以下載Demo查看HomeVC中如何使用的。(使用的時候採用富文本使用方式對UILabel賦值就行,沒什麼可講的)HomeVC.swift文件內容如下:

//

import UIKit


// 設備屏幕寬
public let ScreenW = UIScreen.main.bounds.width
// 設備屏幕高
public let ScreenH = UIScreen.main.bounds.height

class HomeVC: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = UIColor.init(white: 0.89, alpha: 1.0)

        let lab = UILabel.init(frame: CGRect(x: 10, y: 100, width: ScreenW - 20, height: 80))
        lab.backgroundColor = UIColor.white
        lab.textAlignment = .center
        view.addSubview(lab)

        // 使用方式 顏色、大小和自行調整
        let numStr = "0.123456789"
        let mutAttStr = NSMutableAttributedString.init(string: numStr, attributes: [NSAttributedString.Key.font: UIFont.init(name: "DS-Digital", size: 66) ?? 28, NSAttributedString.Key.foregroundColor: UIColor.orange])
        lab.attributedText = mutAttStr
    }
}

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章