Original Post: November 8, 2015 (Swift 2)
This post shows how to list the PingFang TC(Traditional Chinese) font with 6 different weights in a table for comparison.
蘋果公司於9月正式推出的iOS9及OS X 10.11 El Capitan (酋長巨石)中,新增了「蘋方」中文字型,有正體(TC)、香港(HK)及簡體(SC)三個版本,每個版本皆有6種不同的粗細變化。
以下的Swift程式以表格列出不同粗細的「蘋方」字體:
ViewController.swift
Update: August 3, 2017 (Swift 3.1 + Xcode 8.3.3)
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
//font names
let fontArray : [String] = [".PingFangTC-Ultralight",".PingFangTC-Thin",".PingFangTC-Light",".PingFangTC-Regular",".PingFangTC-Medium",".PingFangTC-Semibold"]
override func viewDidLoad() {
super.viewDidLoad()
let tableView = UITableView(frame: CGRect(x: 0, y: 50, width: view.bounds.width, height: view.bounds.height-50))
tableView.dataSource = self
tableView.delegate = self
//Do not draw line separators for empty cells.
tableView.tableFooterView = UIView()
view.addSubview(tableView)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 6
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "myIdentifier")
cell.textLabel?.text = "蘋方字型測試 永 正體"
cell.textLabel?.font = UIFont(name: fontArray[indexPath.row], size: 28)
cell.textLabel?.textAlignment = NSTextAlignment.center
return cell
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
Original Post: November 8, 2015 (Swift 2)
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
//font names
let fontArray : [String] = [".PingFangTC-Ultralight",".PingFangTC-Thin",".PingFangTC-Light",".PingFangTC-Regular",".PingFangTC-Medium",".PingFangTC-Semibold"]
override func viewDidLoad() {
super.viewDidLoad()
let tableView = UITableView(frame: CGRectMake(0, 50, view.bounds.width, view.bounds.height-50))
tableView.dataSource = self
tableView.delegate = self
//Do not draw line separators for empty cells.
tableView.tableFooterView = UIView()
view.addSubview(tableView)
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 6
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "myIdentifier")
cell.textLabel?.text = "蘋方字型測試 永 正體"
cell.textLabel?.font = UIFont(name: fontArray[indexPath.row], size: 30)
return cell
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
結果如下:
在樹莓派上安裝中文字型 Install Chinese Fonts with Raspberry Pi
Google 推新版字體網站,共 804 字體供大家免費取用!
Google Fonts
解決字體顯示問題!蘋果、Google、微軟、Adobe 攜手開發字體(inside.com.tw)
No comments:
Post a Comment