How do I make my UIButton title have some padding? - ios

Let's say I have a UIButton with a size of 120 wide by 30 high. I set an attributed title on it with centered text and I set a background color on the text of the attributed text for the .highlighted state.
When the user highlights the button, I want the background that shows around the title to have the same frame as the button.
I've tried positive and negative values for the button's titleEdgeInsets and contentEdgeInsets with no luck. Any suggestions here? Thanks

The easiest (meaning most controllable) way in my opinion is to use a button subclass and override titleRect. It's called when highlighted and when unhighlighted so you get a chance to change your answer depending on the state.
However, that won't change the size of the background color of attributed text, because it applies only to the text. You'd need to change the background color of the title label itself.

Related

Changing a button's Alpha to 0 while still being able to click it on xCode/Swift

When I change a button's Alpha to 0 I can no longer click it, is there another attribute I need to change so the button becomes hidden while still being clickable?
enter image description here
Change the background color to clear color
btn.backgroundColor = .clear
The system treats a button as disabled when it's alpha is 0. That's usually what you want.
As Ibrahim suggested, you should be able to set the text color and/or background color to clear to make your button invisible but still clickable.

UItextfield not rendering background color

When UITextfield is visible on viewdidload method, It's background color is visible. However, If the text field is kept hidden until some event occurs, It's background color is not visible.
In the image, I have given text field white color background with an alpha value.
In first case color is visible, however in the second case, background color seems to be the clear color.
These text fields are inside UIStackView.
When textfield is in stackView call
self.field1.isHidden = false
self.field1.setNeedsDisplay()

How to set the background of a textView to a dark color and continue to see clearly the text selection?

I would like to set the colors for a textView to dark to avoid to stress the eyes when working on text.
If I set for example the field background to be black and the text to be white, the text color selection becomes a bit hard to see.
This is the code I'm using:
mainTextField.backgroundColor = UIColor.black
mainTextField.textColor = UIColor.white
Is there a way to have te selection of the text more visible?
I believe the text selection color is managed by the tintColor. You could set it to whatever you like by setting mainTextField.tintColor = UIColor.orange
UITextView text selection highlight color is determined by either the global or the view tint color.
You can set the color in Storyboard, by going to file inspector and setting the global tint color, or in attributes inspector of UITextView to apply it specifically for that view.
I also suggest that you set text color to Light Text Color.

CorePlot - Change x-axis Label after calling barTouchUpAtRecordIndex:

I have an iOS app that displays a CPTBarPlot. My plot bar responds to barTouchUpAtRecordIndex: by changing the background bar color but I also want to change the text label background and font color for that index. How can I change it?
The font color is part of the text style for the label. CPTTextLayer (the layer class used to display text in Core Plot) is a subclass of CPTBorderedLayer so it can have a border line and a fill behind the text. Set the padding to control how much of the fill is visible around the text.

XCode UIButton confusion

I'm having some confusion about UIButton on the storyboard. I create a new button, change the background color, and the color is always darker than what I choose. In this example, the actual color that is being displayed is #163ca1 (22, 60, 161), even though I clearly picked something lighter:
Wassup with that? The thing is, if I set the background color programmatically it displays the correct color. This issue extends to other view elements as well.
Also, what does Tint color do? It seems like it has a different function depending on the element in question, but in this case it seems to have no effect. I figured it was for the highlighted state, but no matter what I change it to the button text maintains a light blue color on click.

Resources