Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 如何使用UITableView上的显示字体设置UIFont系列和名称?_Swift_Uitableview_Fonts - Fatal编程技术网

Swift 如何使用UITableView上的显示字体设置UIFont系列和名称?

Swift 如何使用UITableView上的显示字体设置UIFont系列和名称?,swift,uitableview,fonts,Swift,Uitableview,Fonts,我试过这段代码,结果崩溃了,它说我把它拆开了。那么我如何在左侧单元格中显示带有显示和名称的字体 结果代码: import UIKit class ViewController: UIViewController { let names = UIFont.familyNames.sorted() @IBOutlet weak var FontTableView: UITableView! override func viewDidLoad() { super.viewDidLo

我试过这段代码,结果崩溃了,它说我把它拆开了。那么我如何在左侧单元格中显示带有显示和名称的字体

结果代码:

import UIKit

 class ViewController: UIViewController {

let names = UIFont.familyNames.sorted()


@IBOutlet weak var FontTableView: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
}
然后,我添加代码来设置显示字体的名称。然后就是这个

}

 extension ViewController: UITableViewDataSource, UITableViewDelegate {

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    names.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
    let cells = FontTableView.dequeueReusableCell(withIdentifier: "Cells")
    

    
    for family in UIFont.familyNames.sorted() {
      
        let names = UIFont.fontNames(forFamilyName: family)
        
        cells?.textLabel?.text = ("Family: \(family)")
        
        cells?.detailTextLabel?.text = ("Family: \(names)")
    }
    
    
    return cells!
}



}

我不知道你要在一排放映什么。在代码cellForRowAt中,每一行都将运行循环。我想您不应该使用循环,而应该使用数据源名称。此外,fontNames也是一个字符串数组。因此,根据您的要求(第一个可能是),分配给detailsLabel

       let family = names[indexPath.row] 
        
        let fontNames = UIFont.fontNames(forFamilyName: family)
        
        cells?.textLabel?.text = ("Family: \(family)")
        
        cells?.detailTextLabel?.text = ("Family: \(fontNames)")
    

很好,但我需要在文本标签上显示字体类型。。。我试过这个,但字体名称有错误,因为我需要两者都小到足以看到它。let family=names[indexPath.row]let fontNames=UIFont.fontNames(forFamilyName:family)单元格?.textlab?.text=(“(family)”)单元格?.textlab?.font=UIFont(名称:family,大小:10)单元格?.detailtextlab?.text=(“(fontNames)”)cells?.detailTextLabel?.font=UIFont(名称:fontNames,大小:10)正如我所说的fontNames是一个数组,您应该只传递第一个元素-cells?.detailTextLabel?.font=UIFont(名称:fontNames.first!,大小:10)