Swift - iOS中所有内置的字体

2016-09-07  本文已影响167人  Hesse_Huang
class TableViewController: UITableViewController {
    
    // 所有字体,这是一个(String, [String])元组的数组
    let fonts: [(String, [String])] = UIFont.familyNames().map({ ($0, UIFont.fontNamesForFamilyName($0)) })

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return fonts.count
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return fonts[section].1.count
    }

    override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return fonts[section].0
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("font", forIndexPath: indexPath)
        let fontName = fonts[indexPath.section].1[indexPath.row]
        cell.textLabel?.text = fontName
        cell.textLabel?.font = UIFont(name: fontName, size: 17)
        return cell
    }

}
上一篇 下一篇

猜你喜欢

热点阅读