繁体   English   中英

以编程方式更改集合视图单元格中标签的字体

[英]Change font of label in collection view cell programmatically

我有一个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,但我正在手动获取我在故事板中设置的字体。 那么有没有一种方法可以在controller中以编程方式更改字体,或者我是否需要在CustomCell更改它

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

这是我的问题。 其解决方案非常简单 我们要做的就是首先用正确的字体字符串名称指定字体,然后添加字符串即cell.testLabel.font = UIFont(name: "HelveticaNeue", size: 18) cell.testLabel.text = dogs[indexPath.row]

直接将“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
}

使用此代码它将帮助您

label.font = label.font.fontWithSize(20) //20 is font size

斯威夫特 3:

label.font = label.font.withSize(20)

或尝试

UIFont.systemFont(ofSize: 18)

使用awakeFromNib方法,来自苹果文档

nib 加载基础结构向从 nib 存档重新创建的每个对象发送一条awakeFromNib 消息,但前提是存档中的所有对象都已加载并初始化。 当一个对象接收到一个awakeFromNib 消息时,它保证已经建立了它的所有出口和动作连接。

您可以在此方法中更改标签字体。

class CustomCell: UICollectionViewCell {
    @IBOutlet weak var testLabel: UILabel!

    override func awakeFromNib() {
        super.awakeFromNib()
        testLabel.font = UIFont(name: "HelveticaNeue", size: 18)
    }
}

您需要将cellForItemAt更改为:

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

      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CustomCell", for: indexPath) as! CustomCell

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

      return cell
}

然后请检查以下内容:

  1. 您的重用标识符已在故事板中正确设置。
  2. 您的 collectionView 委托已设置。
  3. 您的 collectionView 数据源已设置。
  4. 标签的出口已连接。
  5. 您重新加载 collectionView 数据
  6. 检查字体名称是否正确,可以使用mac上的FontBook进行拼写检查。

首先,我不明白你为什么要在cellForItemAt delegate方法中编写func viewDidLayoutSubviews() 只需从那里删除它。

其次,您不需要在viewDidLoad()调用collectionView.reloadData()因为它会第一次使用您提供的数据自动加载collectionView

在继续更改字体之前,请检查系统中是否有您需要的字体(HelveticaNeue) 如果没有,请先安装字体并将其添加到您的项目中。

现在来改变labelfont 它可以通过两种方式完成:

1.在你的CustomCell使用awakeFromNib() ,即

class CustomCell: UICollectionViewCell
{
    @IBOutlet weak var testLabel: UILabel!

    override func awakeFromNib()
    {
        super.awakeFromNib()
        self.testLabel.font = UIFont(name: "HelveticaNeue", size: 18)
    }
}

这将仅在从 nib 加载单元格时设置单元格的字体一次 如果label's font在配置一次后没有进一步变化,建议使用。

2.cellForItemAt配置字体,即

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CustomCell", for: indexPath) as! CustomCell
    cell.testLabel.text = dogs[indexPath.row]
    cell.testLabel.font = UIFont(name: "HelveticaNeue", size: 18)
    return cell
}

这将在每次调用cellForItemAt delegate方法时设置单元格的字体,即每次重新加载单元格时。 如果您想在重新加载时对单元格进行任何有条件的更改,请使用此选项。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM