Попытка динамически изменить размер шрифта внутри UITableViewCell
в зависимости от размера устройства.
Код для создания таблицы внутри UIView (в SKScene):
suitsView = UIView()
suitsView.backgroundColor = .purple
suitsView.alpha = 0
self.scene?.view?.addSubview(suitsView)
suitsTableView.register(suitsTableCell.self, forCellReuseIdentifier: "cellSuits")
suitsTableView.separatorColor = UIColor(red: 153/255, green: 255/255, blue: 153/255, alpha: 1)
suitsTableView.tableFooterView = UIView()
suitsTableView.allowsMultipleSelection = true
suitsTableView.reloadData()
suitsTableView.alpha = 0
suitsTableView.populateTable()
displayText() // Displays Text
suitsTableView.rowHeight = UITableViewAutomaticDimension
suitsTableView.estimatedRowHeight = 600
suitsView.addSubview(suitsTableView)
Это мой UITableView:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let selectedIndexPaths = tableView.indexPathsForSelectedRows
let rowIsSelected = selectedIndexPaths != nil && selectedIndexPaths!.contains(indexPath)
let cell : SuitsTableCell = tableView.dequeueReusableCell(withIdentifier: "cellSuits", for: indexPath as IndexPath) as! SuitsTableCell
cell.lblName.text = self.items[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
{
return self.bounds.height/5
}
Это мой UITableViewCell:
override init(style: UITableViewCellStyle, reuseIdentifier: String?)
{
super.init(style: style, reuseIdentifier: reuseIdentifier)
selectionStyle = UITableViewCellSelectionStyle.none
backgroundColor = UIColor.yellow
contentView.backgroundColor = UIColor.yellow
// Font size
var fontSize : CGFloat = 20.0
let marginGuide = contentView.layoutMarginsGuide
lblName = UILabel()
lblName.textColor = UIColor.black
lblName.font = UIFont(name:"Noteworthy-Bold", size: fontSize)
// CONSTRAINTS
lblName.translatesAutoresizingMaskIntoConstraints = false
lblName.topAnchor.constraint(equalTo: marginGuide.topAnchor).isActive = true
lblName.leadingAnchor.constraint(equalTo: marginGuide.leadingAnchor).isActive = true
lblName.bottomAnchor.constraint(equalTo: marginGuide.bottomAnchor).isActive = true
lblName.widthAnchor.constraint(equalToConstant: 200)
lblName.numberOfLines = 0
lblName.adjustsFontSizeToFitWidth = true
lblName.contentScaleFactor = 0.5
contentView.addSubview(lblName)
}
На iPhone 8 моя таблица выглядит так:
А вот на iPad это выглядит так:
Мне нужен больший шрифт в iPad,Как настроить размер шрифта в зависимости от ограничений или высоты строки?
Спасибо.
Редактировать: Я не использую раскадровку;пожалуйста, не предлагайте это.
Интересно, если у меня есть этот код внутри моего метода cellForRow:
cell.lblName.font = UIFont(name:"Noteworthy-Bold", size: (tableView.bounds.height/14).rounded())
Я могу настроить размер шрифта, но первая строка (Нет) всегда пуста...