How to Change Button Text Size in iOS 8 Swift

How to change Button text size in iOS 8 swift

Use titleLabel instead of .font

outletLeaderboard.titleLabel!.font =  UIFont(name: "HelveticaNeue-Thin", size: 20)

How can I change text size of button?

Set the titleLabel.font property for your button.

playAgain.titleLabel!.font =  UIFont(name: "Helvetica", size: 20)

How to change font of UIButton with Swift

Use titleLabel instead. The font property is deprecated in iOS 3.0. It also does not work in Objective-C. titleLabel is label used for showing title on UIButton.

myButton.titleLabel?.font =  UIFont(name: YourfontName, size: 20)

However, while setting title text you should only use setTitle:forControlState:. Do not use titleLabel to set any text for title directly.

How to scale the button text based on screen design swift?

Are you sure it's not working?

Edit - After comments...

UIKit elements such as UILabel / UIButton / etc to not have a built-in "auto-adjust font height" property.

I don't work for Apple, so just guessing that is (at least in part) due to the fact that, in general...

Based on screen height, the UI is designed to either:

  • provide more or less information, e.g. more rows in a table, or
  • adjust vertical spacing between elements

That doesn't mean you can't or shouldn't adjust your font sizes... it just means you have to do it manually.

Couple options:

  • set the font-size at run-time, as suggested by Duncan
  • use a UIAppearance proxy to set the font-size, again at run-time

in either case, you could use a height-to-fontSize table or a "percentage" calculation.

Another option would be a custom class that sets the font-size based on the constrained button height.

Here's a simple example (note: for demonstration purposes only):

class AutoFontSizeButton: UIButton {

override func layoutSubviews() {

super.layoutSubviews()

guard let fnt = titleLabel?.font else { return }

// default system type button has 6-pts top / bottom inset
// and font size is 15/18ths of that height
let h = ((bounds.height - 12.0) * (15.0 / 18.0)).rounded()
let fs = fnt.pointSize

if h != fs {
titleLabel?.font = UIFont(descriptor: fnt.fontDescriptor, size: h)
}

}

}

Result - the top three (yellow) buttons are 30, 40 and 50-points in height, with the default font-size of 15. The bottom three (green) buttons are again 30, 40 and 50-points in height, but the font-size is automatically set at run-time:

Sample Image

How to change Button font size in localization condition with Swift 3

Well, one way is to check when the user changes the language. For example,

changeLanguage() // change language

if currentLanguage == "Chinese" {
buttonLogout.titleLabel?.font = UIFont.boldSystemFont(ofSize: 12)
} else {
buttonLogout.titleLabel?.font = UIFont.boldSystemFont(ofSize: 15)
}
enter code here

Depending on how you implement the changing of language, you can have some kind of flag that checks whatever the current language is then change the font size to what you want.

Or another way is:

buttonLogout.titleLabel.adjustsFontSizeToFitWidth = true

How do I make a SwiftUi Button's text larger and/or bold

Use the bold() Text modifier on a Button's label.

Example:

Button {
print("Button tapped!")
} label: {
Text("Tap me").bold()
}

To make the text larger and bold, just chain the modifiers:

Button {
print("Button tapped!")
} label: {
Text("Tap me")
.font(.title)
.bold()
}

How to adjust font size of text in a UIButton to fit the height programmatically in Swift?

Have a look at the baselineAdjustment property of UILabel:

It can be used with a UIButton as follows:

button.titleLabel?.baselineAdjustment = UIBaselineAdjustment.AlignCenters

UIButton auto-adjust Button font size Swift

With auto layout you can set the space between buttons, and the max and min size. In code for all labels use:

self.button.titleLabel.numberOfLines = 0;
self.button.titleLabel.adjustsFontSizeToFitWidth = YES;

With this all labels adjust the text will size.

For adjust button to titleLabel use auto layout constraint for titleLabel. For examples:

  [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.button.titleLabel
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeHeight
multiplier:0.5
constant:0]];

This constraint define the height of titleLabel to 50% of self.view height. Now you can adapte the constraint for that you need.

This work in you code?



Related Topics



Leave a reply



Submit