@符不見了
let datas = ["點(diǎn)擊一下改變字體,","字體就會(huì)改變痴脾,","你相信不,","不相信么,","點(diǎn)一下試試吧??艘儒!"]
let fontNames = ["MFTongXin_Noncommercial-Regular", "MFJinHei_Noncommercial-Regular", "MFZhiHei_Noncommercial-Regular", "Heiti SC"]
var fontNumber = 0
let reuseIdentifier = "FontCell"
StatusBar設(shè)置
樣式:
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
隱藏:
override var prefersStatusBarHidden: Bool {
return false
}
遵守代理
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
}
??
??: 空合運(yùn)算符, a ?? b,對(duì)可選類型a進(jìn)行判斷夫偶,為nil默認(rèn)值為b界睁,不為空就解封
let cell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifier) ?? UITableViewCell(style: .subtitle, reuseIdentifier: reuseIdentifier)
+拼接
let str = "當(dāng)前字體是:"+(cell?.textLabel?.font.fontName)!
<li><h1>學(xué)習(xí)代碼:<h1></li>
ViewController.swift
import UIKit
/*
導(dǎo)入字體步驟:
1.下載ttf文件,加入項(xiàng)目中
2.在info.plist中兵拢,添加一個(gè)字段:Fonts provided by application
3.再添加item翻斟,值寫入字體的名字
4.然后就可以通過名字使用了
*/
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
let tableView = UITableView(frame: CGRect(x: 0.0, y: 0.0, width: YHScreenWidth, height: YHScreenHeight*2/3), style: .plain)
let button = UIButton(frame: CGRect(x: 0.0, y: YHScreenHeight*2/3, width: YHScreenWidth, height: YHScreenHeight/3))
let datas = ["點(diǎn)擊一下改變字體,","字體就會(huì)改變说铃,","你相信不访惜,","不相信么嘹履,","點(diǎn)一下試試吧??!"]
let fontNames = ["MFTongXin_Noncommercial-Regular", "MFJinHei_Noncommercial-Regular", "MFZhiHei_Noncommercial-Regular", "Heiti SC"]
var fontNumber = 0
let reuseIdentifier = "FontCell"
override func viewDidLoad() {
super.viewDidLoad()
setupView()
}
// MARK: - 操作
//設(shè)置狀態(tài)欄樣式
/*設(shè)置狀態(tài)欄:
@available(iOS 7.0, *)
open var preferredStatusBarStyle: UIStatusBarStyle { get }//樣式
@available(iOS 7.0, *)
open var prefersStatusBarHidden: Bool { get }//是否隱藏
*/
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
func setupView() {
tableView.delegate = self
tableView.dataSource = self
tableView.backgroundColor = .black
button.setTitle("改變字體", for: .normal)
button.backgroundColor = .orange
button.addTarget(self, action: #selector(changFont), for: .touchUpInside)
view.addSubview(tableView)
view.addSubview(button)
}
func changFont() {
fontNumber = (fontNumber+1)%fontNames.count
tableView.reloadData()
}
// MARK: - delegate and dataSource
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return datas.count
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return YHScreenHeight*2.0/3.0/CGFloat(datas.count)
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
/*代碼創(chuàng)建债热,并且沒有注冊(cè)cell的情況下砾嫉,用dequeueReusableCell(withIdentifier identifier: String) -> UITableViewCell?
如果已經(jīng)注冊(cè)了,或者用的xib窒篱,就使用dequeueReusableCell(withIdentifier identifier: String, for indexPath: IndexPath) -> UITableViewCell
*/
//??空合運(yùn)算符焰枢,a ?? b,對(duì)可選類型a進(jìn)行判斷舌剂,為nil默認(rèn)值為b济锄,不為空就解封
let cell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifier) ?? UITableViewCell(style: .subtitle, reuseIdentifier: reuseIdentifier)
let text = datas[indexPath.row]
cell.textLabel?.text = text
cell.textLabel?.textColor = .white
cell.textLabel?.font = UIFont(name: fontNames[fontNumber], size: 24)
cell.backgroundColor = .black
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
//取消點(diǎn)擊效果
tableView.deselectRow(at: indexPath, animated: true)
let cell = tableView.cellForRow(at: indexPath)
//鏈?zhǔn)秸{(diào)用,最后得到一個(gè)可選的string,霍转!強(qiáng)制解包出來
let str = "當(dāng)前字體是:"+(cell?.textLabel?.font.fontName)!
print(str)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}