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

时间:2017-11-20 21:24:42

标签: swift uilabel orientation font-size custom-cell

我在集合视图中显示我的数据。我的应用程序支持所有方向,横向和纵向。我有自定义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 。每次旋转设备时都可以更新字体大小吗?

1 个答案:

答案 0 :(得分:1)

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)
    } 
}