Changing text of UIButton programmatically swift
In Swift 3, 4, 5:
button.setTitle("Button Title", for: .normal)
Otherwise:
button.setTitle("Button Title", forState: UIControlState.Normal)
Also an @IBOutlet
has to declared for the button
.
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.
Swift 3 changing the title of a UIButton
Your button needs to be an IBOutlet rather than an IBAction. When you ctrl-drag from Interface Builder to your code, make sure you select the 'Outlet' option as shown below:
Then you can change the button title like this:
else {
startButton.setTitle("Button Title", for: .normal)
stopFuncs = true
}
How to change a button's title programmatically
Well....This is awkward but you have to set your UIButton title to Plain in storyboard to have it changed
ProfilePhoneNumberLabel.setTitle(PhoneNumbers[MyIndex], for: .normal)
+
Plain UIButton
Title
=
Happy Camper
is it possible to update UIButton title/text programmatically?
Do you have the button specified as an IBOutlet in your view controller class, and is it connected properly as an outlet in Interface Builder (ctrl drag from new referencing outlet to file owner and select your UIButton object)? That's usually the problem I have when I see these symptoms.
Edit: While it's not the case here, something like this can also happen if you set an attributed title to the button, then you try to change the title and not the attributed title.
Programmatically change button text
it should be like this :
For Objective C :
[_button setTitle:@"example text" forState:UIControlStateNormal];
For Swift :
_button.setTitle("example Text", forState: UIControlState.Normal)
How to change UIButton state programmatically in Swift
I think you're confused. UIControlState
is used to set up target/actions, or to change the appearance of the button for specific states (So, for example, you can specify the image that's used when it's selected, and a different image for when the button is not in the selected state.)
If you want to change the state to selected, you just set the selected property.
button.isSelected = true
Likewise for isEnabled:
button.isEnabled = false //disable the button
Why is UIButton.titleLabel?.text always switching back after I changed it in Swift 5 (UIKit)?
It seems, UIButton
does not allow to configure text
or color
of titleLabel
.
From apple doc.s
Do not use the label object to set the text color or the shadow color.
Instead, use the setTitleColor(:for:) and setTitleShadowColor(:for:)
methods of this class to make those changes. To set the actual text of
the label, use setTitle(_:for:) (button.titleLabel.text does not let
you set the text).
Change a UIButton's text (padding) programmatically in Swift
Still valid for:iOS 12/Xcode 10/Swift 4.2/OSX 10.13.2
iOS 9.1/Xcode 7.1/Swift 2.1/OSX 10.10.5
:
The method titleEdgeInsets
didn't work for me. My button's frame tightly hugs the text (I didn't specify a frame size), and the frame has a red background color. After doing:
myButton.titleEdgeInsets = UIEdgeInsetsMake(10,10,10,10)
the red background shrunk inwards by 10 pixels on each side, which meant that now some of the text was outside the red background. Using negative values had no effect on the original frame size.
I was able to get padding around the text, effectively making the frame bigger, by doing:
myButton.contentEdgeInsets = UIEdgeInsetsMake(5,5,5,5)
Related Topics
Center Content of Uiscrollview When Smaller
Uitableview with Static Cells Does Not Appear
How to Rotate Orientation Programmatically in Swift
Will Items in iOS Keychain Survive App Uninstall and Reinstall
How I Get Whatsapp Abid to Compose a Message to a Specific User in iOS
How to Fix the Xcrun Unable to Find Simctl Error
Where to Remove Observer for Nsnotification in Swift
Converting Between Nsdata and Base64 Strings
iPhone "Slide to Unlock" Animation
In iOS 12, When Does the Uicollectionview Layout Cells, Use Autolayout in Nib
Install Simulator Sdk 4.3 to Xcode 4.4 on Mountain Lion
Open Phone Settings Programmatically in iOS9
Uibutton Fails to Properly Register Touch in Bottom Region of iPhone Screen