【问题标题】:change collectionViewCell Label font size when change orientation更改方向时更改 collectionViewCell 标签字体大小
【发布时间】:2018-05-04 05:17:54
【问题描述】:

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

【问题讨论】:

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


    【解决方案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)
        } 
    }
    

    【讨论】:

    • 太棒了!谢谢@Grzegorz Krukowski
    猜你喜欢
    • 2016-10-30
    • 2015-10-27
    • 2017-08-05
    • 2022-09-28
    • 1970-01-01
    • 2017-11-29
    • 1970-01-01
    • 2019-09-11
    • 2016-04-25
    相关资源
    最近更新 更多