gpt4 book ai didi

swift - 更改方向时更改 collectionViewCell 标签字体大小

转载 作者:行者123 更新时间:2023-11-28 15:12:28 25 4
gpt4 key购买 nike

我在 Collection View 中显示我的数据。我的应用程序支持所有方向、横向和纵向。我有带有标签的自定义 collectionViewCell。我的单元格大小是

CGSize(width: self.view.frame.width / 3 - 40, height: self.view.frame.width / 3 - 40)`  

因此,当我将设备旋转到横向模式时,它会增加单元格大小。

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
guard let flowLayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout else {
return
}
flowLayout.invalidateLayout()
}

我在单元格中有一个标签

class ChannelViewCell: UICollectionViewCell {

let label : UILabel = {
let label = UILabel()
label.numberOfLines = 2
label.font = UIFont(name: "ProRegular", size: 13)
label.textAlignment = .center
label.textColor = UIColor.white
label.lineBreakMode = .byWordWrapping
return label
}()

override init(frame: CGRect) {
super.init(frame: frame)


addConstraintsWithFormat(format: "H:|-5-[v0]-5-|", views: label)
addConstraintsWithFormat(format: "V:|-5-[v0]-5-|", views: label)
}
}

我想在横向模式下将字体大小从 13 增加到 25。每次旋转设备时都可以更新字体大小吗?

最佳答案

override func  layoutSubviews() {
super.layoutSubviews()

if UIDevice.currentDevice().orientation.isLandscape.boolValue {
label.font = UIFont(name: "ProRegular", size: 25)
} else {
label.font = UIFont(name: "ProRegular", size: 13)
}
}

关于swift - 更改方向时更改 collectionViewCell 标签字体大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47401307/

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