Ilow
November 30, 2022, 2:16pm
1
Hi
I am in Xcode in the interface builder storyboard. In an action I wanted when the user presses a button the buttons title becomes an X at a certain font size. I have tried for a long long time and still couldn’t find anything. I will be forever grateful if you could help me .
@IBAction func testX(_ sender: UIButton) {
sender.titleLabel?.font = [UIFont withSize(75.0)];
}
I tried this it doesn’t work.
Looks like google says to,
myButton.titleLabel?.font = UIFont(name: "GillSans-Italic", size: 20)
So try deleting the name: portion if you don’t want a specific name. I don’t personally know UI code at all though.
Ilow
December 1, 2022, 2:05am
3
Thank you anyways . It’s not so simple to do that in UIKit.
@Ilow
Welcome to the community.
Here is a method I found to achieve a change in Font size when you tap on the Button.
Create an @IBOutlet for your button that you have added in storyboard. eg
@IBOutlet weak var myButton: UIButton!
In your @IBAction that you have for your button, do this:
@IBAction func testX(_ sender: Any) {
let customButtonTitle = NSMutableAttributedString(string: "MyButton", attributes: [
NSAttributedString.Key.font: UIFont.systemFont(ofSize: 75.0),
])
myButton.setAttributedTitle(customButtonTitle, for: .normal)
}
When you tap on the button the font should change to 75 points.