简体   繁体   English

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

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

I have a CollectionViewController as follows我有一个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
}

and CustomCell as followsCustomCell如下

class CustomCell: UICollectionViewCell {

    @IBOutlet weak var testLabel: UILabel!
}

I like to change the font size as 18 above, but I am getting the font that I set in the storyboard manually.我喜欢将字体大小更改为上面的 18,但我正在手动获取我在故事板中设置的字体。 So is there a way that I can change font programatically in the controller or do I need to change it in CustomCell那么有没有一种方法可以在controller中以编程方式更改字体,或者我是否需要在CustomCell更改它

PS Just found out this happens, when I set manually fonts on device sizes available and if I remove those manual device sizes it works. PS 刚刚发现发生这种情况,当我在可用的设备尺寸上手动设置字体时,如果我删除这些手动设备尺寸,它就可以工作。 Is there anyway I can override this and have both at same time?无论如何我可以覆盖它并同时拥有两者吗?

this was my problem.这是我的问题。 and its solution very simple All we have to do is first specify font with correct font string name then add string ie cell.testLabel.font = UIFont(name: "HelveticaNeue", size: 18) cell.testLabel.text = dogs[indexPath.row]其解决方案非常简单 我们要做的就是首先用正确的字体字符串名称指定字体,然后添加字符串即cell.testLabel.font = UIFont(name: "HelveticaNeue", size: 18) cell.testLabel.text = dogs[indexPath.row]

Directly change in "cellForItemAt" method as直接将“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
}

use this code it will help you使用此代码它将帮助您

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

Swift 3:斯威夫特 3:

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

or try或尝试

UIFont.systemFont(ofSize: 18)

use awakeFromNib method, from apple documentation ,使用awakeFromNib方法,来自苹果文档

The nib-loading infrastructure sends an awakeFromNib message to each object recreated from a nib archive, but only after all the objects in the archive have been loaded and initialized. nib 加载基础结构向从 nib 存档重新创建的每个对象发送一条awakeFromNib 消息,但前提是存档中的所有对象都已加载并初始化。 When an object receives an awakeFromNib message, it is guaranteed to have all its outlet and action connections already established.当一个对象接收到一个awakeFromNib 消息时,它保证已经建立了它的所有出口和动作连接。

you can change the label font in this method.您可以在此方法中更改标签字体。

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

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

You need to change cellForItemAt to be this:您需要将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
}

Then please check the following:然后请检查以下内容:

  1. Your reuse identifier is setup correctly in the storyboard.您的重用标识符已在故事板中正确设置。
  2. Your collectionView delegate is set.您的 collectionView 委托已设置。
  3. Your collectionView datasource is set.您的 collectionView 数据源已设置。
  4. The outlet for the label is connected.标签的出口已连接。
  5. You reload the collectionView data您重新加载 collectionView 数据
  6. Check the font name is correct, you can use FontBook on the mac to check spelling.检查字体名称是否正确,可以使用mac上的FontBook进行拼写检查。

First of all I don't understand why have you written func viewDidLayoutSubviews() inside cellForItemAt delegate method.首先,我不明白你为什么要在cellForItemAt delegate方法中编写func viewDidLayoutSubviews() Just remove it from there.只需从那里删除它。

Secondly, you don't need to call collectionView.reloadData() in viewDidLoad() since it will automatically load the collectionView first time with the data you provided.其次,您不需要在viewDidLoad()调用collectionView.reloadData()因为它会第一次使用您提供的数据自动加载collectionView

Before proceeding to changing the font, check if your required font (HelveticaNeue) is available in the system.在继续更改字体之前,请检查系统中是否有您需要的字体(HelveticaNeue) If not first install the font and add it to your project.如果没有,请先安装字体并将其添加到您的项目中。

Now coming to changing the font of the label .现在来改变labelfont It can be done in 2 ways:它可以通过两种方式完成:

1. Use awakeFromNib() in your CustomCell , ie 1.在你的CustomCell使用awakeFromNib() ,即

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

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

This will set the cell's font only once when the cell is loaded from the nib .这将仅在从 nib 加载单元格时设置单元格的字体一次 It is recommended if there are no further changes in the label's font after configuring it once.如果label's font在配置一次后没有进一步变化,建议使用。

2. Configure the font in cellForItemAt , ie 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
}

This will set the cell's font each time cellForItemAt delegate method is called , ie every-time the cell is reloaded.这将在每次调用cellForItemAt delegate方法时设置单元格的字体,即每次重新加载单元格时。 Use this if you want to make any conditional changes in the cells when reloading.如果您想在重新加载时对单元格进行任何有条件的更改,请使用此选项。

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

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