Find text color of button in swift4 - ios

I am working on app and I give UIButton text color from storyboard (Different for default and selected), I want both text color in a variable.
Anyone help me with this problem.
I have one solution for this problem " Take 'color code' of this color and give it to variable"
Anyone has any other short method for this problem.
Because if I use 'color code' then in future when I change color of button text then Then also work on that 'color code'.

You need to call titleColor() method of UIButton
let color = btnOutletExample.titleColor(for: .normal) or your desired state

try this
let titleColor: UIColor = yourButton.currentTitleColor

At the end of viewDidLoad, you can add the following to capture the color information:
let defaultTextColor = myButton.titleColor(for: .normal)
let selectedTextColor = myButton.titleColor(for: .selected)

You could pull the colour out of the element you configured in your storyboard, using the following code.
let btnTextColor = self.myBtnOutlet.titleLabel.titleColor(for: .normal)

Related

UIButton highlight color overlaps text when the button is pressed

This is a normal state of my UIButton:
But when I press on the UIButton, its highlight color should be changed to a different one. And here is what I have:
So, if you can notice, my white text becomes overlapped with the new color. But what I should have as a result is just a different color of the highlight and always the white text. Like so:
What I am doing so far is:
In Attribute Inspector in my xib file I changed the Highlight Color to a new one.
I also use updateButton.setTitleColor(.white, for: .highlighted) in my code but it doesn't help.
I've also taken a look at this question: UIButton background color overlaps text on highlight but the accepted answer didn't help much.
Similar to myButton.setTitleColor(.white, for: .normal) please try setting title colour for .highlighted state. That should work for you.
You can refer:
https://developer.apple.com/documentation/uikit/uibutton/1623993-settitlecolor
This myButton.setTitleColor(.white, for: .normal) doesnt change title color when highlighted , Its for normal state. If you want to change textColor when button is highlighted, you need to use myButton.setTitleColor(.white, for: .highlighted)
Also when you change state config in storyboard
you can set Text Color
If its a custom Button you can override isHighlighted method and do what you need like
class MyButton : UIButton {
override var isHighlighted: Bool{
didSet {
tintColor = isHighlighted ? UIColor.red : UIColor.white
backgroundColor = UIColor.blue
.....
// do what you need
}
}
As your button is subclass of UIView class CellButton: UIView {}, you need to handle touches begin and end events in custom CellButton and handle the appearance of the view. If it was subclass of UIButton then it would have handled automatically for you :)
You can also try with gestures, for more info you can refer: https://developer.apple.com/documentation/uikit/touches_presses_and_gestures/handling_uikit_gestures/handling_tap_gestures

changing image color when clicked

I have for example this icon:
this is the color of the icon when its not clicked .. when i click it it will change the color as shown below:
what is the best way to achieve that? is there is a way to do it as a font icon and change the color of it? or shall i just change the image when clicked and unclicked?
can someone please tell me the best way to do it...
Use rendered image
var renderedIcon: UIImage? = UIImage(named:"myImage")?.withRenderingMode(.alwaysTemplate )
imageView.image = renderedIcon
imageView.tintColor = //Normal Color
Change the tintColor when button clicked.
OR
You could set
myButton.setImage(UIImage(named : "unselectedImage"), forState: UIControlState.Normal)
myButton.setImage(UIImage(named : "selectedImage"), forState: UIControlState.Selected)

How to Edit UIButton Attributes Outside of UIButton [duplicate]

This question already has answers here:
Change button background color using swift language
(12 answers)
Closed 6 years ago.
I am trying to make a settings page where if you select the 'Red' theme, then the red button's background goes red and the 'Blue' theme button's background goes white and vice versa. My problem is that I do not know how to edit another UIButton outside of the actual UIButton code. I know to edit the background color of regular UIButton you would write:
sender.backgroundColor = .red
However I am not sure how to do this outside the UIButton's code.
I have tried this:
themeCRed(sender.backgroundColor = .white)
(ThemeCRed is the button's name)
But I get the error, "Cannot convert value of type '()' to expected argument type 'UIButton'"
How can I edit the attributes of the other UIButton outside of it's function block?
If you know the intended outcome and there's few variations then forget about the sender and just set the intended uicolor to the desired uicolor
I don't sure that i understand you correct, but may be just make
#IBOutlet var myButton: UIButton!
in your ViewController or something...
and
myButton.backgroundColor = .white
hm?
You just need to identify the event of theme change and need to change the background colour of the buttons like below
yourButton.backgroundColor = UIColor.red

How to set clean Image on Button in ios(Xamarin)

I am new to Xamarin and iOS. I am setting Image on Button via both the way design and programatically.
Design Way :
my Image name is Pen.png.
Programatically Way :
btnEditAccount.SetImage(UIImage.FromFile("Pen.png"), UIControlState.Normal);
But in Both case My Image not set well. It Complete White Image. But on Button it display below Image.
Output :
I Expect :
Is there any Property I miss for my understing ?
How to set full white Image on Button.
Any Help be Appreciated.
button.setImage(UIImage.init(named: "icon (5).png"), forState: .Normal)
button.tintColor = UIColor.clearColor()
Finally I set TintColor to Button Image and it will solve the Probelm.
Programatically this way
btnEditAccount.SetImage(UIImage.FromFile("Pen.png"), UIControlState.Normal);
btnEditAccount.TintColor = UIColor.White;

UITableViewRowAction customise title font size in Swift

I have an edit and a delete tableViewRowAction in my tableViewCell. Currently I'm using the built in emojis to use as my title, but it is too small. How do I make the font larger?
I know we can only customise a limited amount of things in the tableViewRowAction. But is there a way to go around it to make just the title font bigger?
I checked other threads and most of them used:
UIButton.appearance().setAttributedTitle(NSAttributedString(string: "Your Button", attributes: attributes), forState: .Normal)
with a set attribute that determines the font size of course. However, this affects ALL buttons, and that isn't good.
Any help is much appreciated! Thanks!
you are setting this to UIButton so it is set for all UIButton objects.
You can set it for specific button object like,
let myButton: UIButton = UIButton() //your button here
let attributedStr: NSAttributedString = NSAttributedString()
myButton.setAttributedTitle(attributedStr, forState: .Normal)
Update :
You can set title as attributed string.
check Apple documentation for that and refer this answer for more details
Hope this will help :)

Resources