【问题标题】:Let Button title label size according the device in swift (iPad or iPhone)让按钮标题标签大小根据设备快速(iPad或iPhone)
【发布时间】:2018-08-05 14:09:27
【问题描述】:

我对按钮标题标签大小有疑问。
我想放大 iPad 中按钮标题标签的字体大小。
我尝试使用以下代码,但似乎无法放大标签。
如果我想根据设备改变字体大小,我该怎么办?

以图片为例。
标签大小如下图。

这段代码在 iPhone 上工作的大小很好,但在 iPad 上似乎很小。

func CustomButton() -> UIButton {
    let ui = UIButton()
    ui.translatesAutoresizingMaskIntoConstraints = false
    ui.contentMode = UIViewContentMode.scaleAspectFit
    return ui
}

class FullWidthButton: UIView {

var button:UIButton = { () -> UIButton in
    let ui:UIButton = CustomButton()
    ui.titleLabel?.numberOfLines = 1
    ui.titleLabel?.textAlignment = .center
    ui.titleLabel!.minimumScaleFactor = 0.5
    ui.titleLabel?.textColor = UIColor.white
    ui.titleLabel?.font = UIFont.systemFont(ofSize: 16.0)
    ui.titleLabel?.adjustsFontSizeToFitWidth = true
    ui.titleLabel?.lineBreakMode = .byClipping
    ui.backgroundColor = defaultButtonOrangeColor
    ui.layer.cornerRadius = defaultButtonRadius
    ui.layer.masksToBounds = true
    return ui
}()
override init(frame: CGRect) {
    super.init(frame:frame)
    translatesAutoresizingMaskIntoConstraints = false
    loadContent()
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}

override func layoutSubviews() {
    super.layoutSubviews()

    loadVFL()
}

func loadContent() {

    addSubview(button)
}

func loadVFL() {

    button.snp.makeConstraints { (make) in
        make.width.height.leading.trailing.top.bottom.equalToSuperview()
    }   
} 
}

【问题讨论】:

标签: ios swift uibutton autolayout uilabel


【解决方案1】:

只需替换

ui.titleLabel?.font = UIFont.systemFont(ofSize: 16.0)

if UIDevice.current.userInterfaceIdiom == .pad{
        ui.titleLabel?.font = UIFont.systemFont(ofSize: 20.0)
}else if  UIDevice.current.userInterfaceIdiom == .phone{
        ui.titleLabel?.font = UIFont.systemFont(ofSize: 16.0)
}else{
        //Unspecified
        ui.titleLabel?.font = UIFont.systemFont(ofSize: 16.0)
}

【讨论】:

    【解决方案2】:
    switch UIDevice.current.userInterfaceIdiom { 
        case .phone:
        case .pad:
        case .unspecified:
    }
    

    【讨论】:

      【解决方案3】:

      这样试试

      if UIScreen.mainScreen().bounds.size.height == 480 {
          // iPhone 4
          label.font = label.font.fontWithSize(20)     
      } else if UIScreen.mainScreen().bounds.size.height == 568 {
          // IPhone 5
          label.font = label.font.fontWithSize(20)
      } else if UIScreen.mainScreen().bounds.size.width == 375 {
          // iPhone 6
          label.font = label.font.fontWithSize(20)
      } else if UIScreen.mainScreen().bounds.size.width == 414 {
          // iPhone 6+
          label.font = label.font.fontWithSize(20)
      } else if UIScreen.mainScreen().bounds.size.width == 768 {
          // iPad
          label.font = label.font.fontWithSize(20)
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-11-29
        • 1970-01-01
        • 2016-09-02
        • 1970-01-01
        • 1970-01-01
        • 2016-04-22
        • 2021-07-15
        相关资源
        最近更新 更多