0

I have a question about button title label size.
I want to enlarge the font size which button title label in iPad.
I try to use following code, but it seems not working to make label enlarge.
If I want to according the device to change font size, what should I do?

Take image for example.
Label size like following image.
image

This code is nice size working in iPhone, but it seems very small in 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()
    }   
} 
}
JimmyLee
  • 507
  • 2
  • 7
  • 24
  • 1
    https://stackoverflow.com/questions/39523476/changing-the-font-size-according-to-each-iphone-size – Nitish Feb 26 '18 at 07:09
  • https://stackoverflow.com/questions/48630776/uitextview-font-to-always-be-fixed-size/48646121#48646121' you can get answer – McDonal_11 Feb 26 '18 at 07:10
  • Possible duplicate of [changing the font size according to each iPhone size](https://stackoverflow.com/questions/39523476/changing-the-font-size-according-to-each-iphone-size) – user28434'mstep Feb 26 '18 at 11:28

3 Answers3

0

Just replace

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

with

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)
}
Taimoor Suleman
  • 1,588
  • 14
  • 29
0
switch UIDevice.current.userInterfaceIdiom { 
    case .phone:
    case .pad:
    case .unspecified:
}
Aleksander Aleksic
  • 1,282
  • 3
  • 12
  • 18
0

Try Like this

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)
}
Prajnaranjan Das
  • 1,378
  • 1
  • 9
  • 26