I am trying to create UIButton and display a text in it, as well as designate a font size but it will not show up in my Storyboard, using the setTitleColor and the titleLabel!. It will work if I comment those 2 lines out and uncomment the forgot.setTitle (That is currently commented out) however with that I cannot set the UIButton text fontsize. Any suggestions? I have added the subview to my view in my viewdidload.
let forgotPassword: UIButton = {
let forgot = UIButton()
forgot.translatesAutoresizingMaskIntoConstraints = false
//forgot.setTitle("Forgot your password?", for: .normal)
forgot.titleLabel! .font = UIFont(name:"Forgot your password?", size: 14)
forgot.setTitleColor(.white, for: .normal)
forgot.addTarget(self, action: #selector(forgot(_ :)), for: .touchUpInside)
forgot.sizeToFit()
//forgot.backgroundColor = .orange
return forgot
}()
You need to uncomment the line that sets the title - without a title it's not surprising that nothing turns up. You also need to change the font line - the name is meant to be the name of the font, not the text string to display. The below works:
let button = UIButton()
button.setTitle("Forgot your password?", for: .normal)
button.sizeToFit()
button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
button.setTitleColor(.white, for: .normal)
The button is not visible because it has no text.
The problem here is that these two lines do not actually set the text, only the appearance.
forgot.titleLabel!.font = UIFont(name: "System", size: 14) // This is the font, not the text itself
forgot.setTitleColor(.white, for: .normal)
You have to call this line as well to actually set any text to appear.
forgot.setTitle("Forgot your password?", for: .normal)
Related
I'm having a hard time changing the text color for the placeholder label that is used as a hint when not in focus and as a label on top when in focus. (Photo below)
The is reason I'm switching out all my MDC-TextFields and MDC-TextInputControllers is because they are all being deprecated for the New MDC-Outlined Textfields.
The code below is a function within an extension that would simply setup the general background & sub-label colors for all MDC-Outlined Textfields throughout the app.
I have tried a number of functions with no luck(commented out below).
extension MDCOutlinedTextField {
func setUpGeneralBackgroundColors(){
//Text color
self.setTextColor(UIColor.white, for: .normal)
self.setTextColor(UIColor.white, for: .editing)
//Border color
self.setOutlineColor(UIColor.white, for: .normal)
self.setOutlineColor(UIColor.white, for: .editing)
//self.setFloatingLabelColor(UIColor.white, for: .normal)
//self.setFloatingLabelColor(UIColor.white, for: .editing)
//self.setFloatingLabelColor(UIColor.white, for: .disabled)
// self.setNormalLabelColor(UIColor.purple, for: .normal)
// self.setNormalLabelColor(UIColor.purple, for: .editing)
// self.setNormalLabelColor(UIColor.purple, for: .disabled)
// self.label.tintColor = UIColor.purple
//self.label.textColor = UIColor.systemPink
//self.label.shadowColor = UIColor.cyan
//self.label.backgroundColor = UIColor.red
//Changes icon colors within the text field if any
self.tintColor = .green
}
}
I figured the problem, in the storyboard where I have these new textfields I had a raw string for the placeholder which override the swift code behind the scenes and prevented me from changing colors.
Note: This problem will NOT occur with the now deprecated MDC-Textfields.
If you guys have a placeholder value in the storyboard when using these MDC-Outlined Textfields in storyboards, (not to mixed up with the swift code) then get rid of them.
I want to change the tint color of a button.
button.setTitle("✸", for: .normal)
button.setTitleColor(.green, for: .normal)
This is working fine with the code above. However when I remove the first line (setTitle), it does not work anymore.
I have set a title for the button in the interfacebuilder/storyboard. I can't figure out what is the reason for that.
More research. Just one of these options work when the title is set via the interface builder. But when I change the title of the Button, option2 does not longer work.
// option 1
button.setTitle("✸", for: .normal)
button.setTitleColor(.green, for: .normal)
// option 2
button.tintColor = .red
I want to change the tint color of a button.
So why don't you set the tintColor?
button.tintColor = .red
...this works even if you set the title only in the interface builder.
I do my swift project using code only no storyboards, so no storyboard solutions please I have an agree to terms button and when selected the checkmark is covered in the tint color I am trying to prevent the tint color highlighting the image when selected I am not sure why this is happening here is the button code
let agreeToTermsAndConditions : UIButton = {
let button = UIButton(type: .system)
button.tintColor = .gray
button.backgroundColor = .white
button.adjustsImageWhenHighlighted = false
button.setImage(UIImage(named:"unchecked" )!, for: .normal)
button.setImage(UIImage(named:"agreechecked" )!, for: .selected)
button.addTarget(self, action: #selector(userAgreedToTerms), for: .touchUpInside)
return button
}()
The code is running on xcode 10.2.1 using swift 4 and iOS 12 deployment
Change
UIButton(type: .system)
to
UIButton(type: .custom)
Now you have complete control over the look and behavior of the button.
I have been struggling with this for hours. I am new to Swift so all I want to do is add an image of a shopping cart and a string to the .rightBarButtonItem and I cannot get it. So far I have this:
let cartIcon = UIBarButtonItem(image: UIImage(named: "shopping_cart_icon"), style: .plain, target: self, action: #selector(didTapCart(_:)))
let cartTotal = UIBarButtonItem(title: "\(cart_total)", style: .plain, target: self, action: #selector(didTapCart(_:)))
navigationItem.rightBarButtonItems = [cartIcon, cartTotal]
But with this, my icon and number is 50px apart! I also tried creating a custom UIView using this:
let view = UIView()
let button = UIButton(type: .system)
button.layer.shadowRadius = 5.0
// autolayout solution
button.translatesAutoresizingMaskIntoConstraints = false
button.widthAnchor.constraint(equalToConstant: 40).isActive = true
button.heightAnchor.constraint(equalToConstant: 40).isActive = true
button.semanticContentAttribute = .forceRightToLeft
button.setImage(UIImage(named: "shopping_cart"), for: .normal)
button.setTitle("\(cartItemArray.count)", for: .normal)
//button.addTarget(self, action: #selector(openDocuments), for: .touchUpInside)
button.sizeToFit()
view.addSubview(button)
view.frame = button.bounds
navigationItem.rightBarButtonItem = UIBarButtonItem(customView: view)
But the icon is massive and when I shrink it, the cart total is again really far away. In both cases, the original image asset is green but it takes on the system blue tint upon building. How do I sort this out?
You are really trying to coerce a very standard iOS UIElement into being something totally different. You need to create you own button and place it where the right bar button item is at. Don't make it a barButtonItem. Just your own Button.
Here is a nice answer showing how to do so:
https://stackoverflow.com/a/31912446/793607
I am configuring my tableview cell like I mentioned below. It works, but the problem is there is unwanted space before the title 'Closed' since I am removing the image. How do I remove the unwanted space, so that my 'Closed' title will be centered?
if (condition) {
self.myButton.setImage(UIImage(named: "activeImage"), forState: .Normal)
self.myButton.setTitle("Active", forState: .Normal)
}
else {
self.myButton.setImage(nil, forState: .Normal)
self.myButton.setTitle("Closed", forState: .Normal)
}
You might be missing this in else condition
self.myButton.contentHorizontalAlignment = .Center
self.myButton.contentVerticalAlignment = .Center