gpt4 book ai didi

ios - 以编程方式更改 Collection View 单元格中标签的字体

转载 作者:搜寻专家 更新时间:2023-11-01 06:15:04 24 4
gpt4 key购买 nike

我有一个 CollectionViewController 如下

class CollectionViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource{

var dogs = ["Dog1", "Dog2","Dog3"]

@IBOutlet weak var collectionView: UICollectionView!

override func viewDidLoad() {
super.viewDidLoad()
collectionView.delegate = self
collectionView.dataSource = self
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CustomCell", for: indexPath) as! CustomCell
cell.testLabel.text = dogs[indexPath.row]

return cell
}

CustomCell如下

class CustomCell: UICollectionViewCell {

@IBOutlet weak var testLabel: UILabel!
}

我想将字体大小更改为上面的 18,但我正在获取我在 Storyboard中手动设置的字体。那么有没有一种方法可以在 controller 中以编程方式更改字体,或者我是否需要在 CustomCell

中更改它

附言刚发现会发生这种情况,当我在可用的设备尺寸上手动设置字体时,如果我删除这些手动设备尺寸,它就会起作用。无论如何我可以覆盖它并同时拥有两者吗?

最佳答案

直接在“cellForItemAt”方法中更改为

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CustomCell", for: indexPath) as! CustomCell
cell.testLabel.text = ["Dog1", "Dog2", "Dog3"]

cell.testLabel.font = UIFont(name: "HelveticaNeue", size: 18)

return cell
}

关于ios - 以编程方式更改 Collection View 单元格中标签的字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47051127/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com