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

标签 swift uilabel orientation font-size custom-cell

我在 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/

相关文章:

swift - 如何以编程方式使标签更大?

ios - 尝试更改文本值时连接的 UILabel 错误 EXC_BAD_EXCEPTION

ios - 如何仅旋转子类 mpmovieplayer Controller 并将其他 View 固定为纵向

class - 更改符合 'generate()' 的类的子类的 'SequenceType' 方法

ios - UITableView 和 UINavigationBar 自定义混淆。关于如何完成的任何建议?

swift - 如何检索请求的 Alamofire 响应 header

iphone - 在 iOS 中选择 UILabel 时推送到另一个 View Controller

ios - 从横向启动应用程序时在 IOS 中锁定纵向方向

youtube - 如何从 mp4 视频中删除或编辑 Exif?

profiling - 如何在 Swift 中检测和调试强引用循环?