swift - 如何在 UITableView 上使用显示字体设置 UIFont 系列和名称?
问题描述
我尝试了这段代码并崩溃了它说打开它。那么我如何在左侧单元格上显示带有显示和名称的字体。
结果代码:
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)")
推荐阅读
- powershell - 在 PowerShell 中加载程序集时出现问题
- c# - SignalR:我可以向群组中的呼叫者发送消息吗?
- php - Laravel belongsToMany 关系条件
- java - 如何在 cmd 上请求要在 java 应用程序上使用的文件
- c# - 如何在 uwp 中关闭辅助视图
- java - 在 Nashorn JavaScript 中覆盖 Java 类的方法
- javascript - 对象属性“空白”,除非特别调用
- reactjs - 在渲染中反应状态异步
- javascript - 获取选择中元素的类型
- swift - 成功上传文件后,来自 Firebase 存储的 downloadURL 为零