Change UIAppearance of default UILabels only - ios

I'm building an app with a theme switcher and I'd like to use Appearance to switch the textColor of UILabels that have the default color (black -> white).
I assumed this would be possible and created subclasses for non-default labels, so I simply have to target UILabels with either no subclass or that kept the default color.
When I change the appearance with the following :
UILabel.appearance().textColor = UIColor.white
Every single Label, including subclasses and system labels become white, UIAppearance overriding control-level customization. I feel like it makes sense for UIAppearance to be the default state and for any customization to be applied on top of it.
Would there be any way to use UIAppearance to solve this? Or do I have to manually edit every label I have to add a subclass and a custom property?
Thanks for your help!

The only way I found to overcome this problem is to make the textColor variable read only. But it works:
UILabel.appearance().textColor = UIColor.orange
And the custom class:
class MyLabel: UILabel {
override var textColor: UIColor! {
get { return UIColor.blue }
set {}
}
}

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

Border color doesn't change when changing themes in iOS 13

I recently developed app which is compatible with Dark mode.
And dark mode also works fine.
Btw when I change from dark->light, light->dark mode from device, all colors change as expected except border color.
Let's say border color is black when light mode and white when dark mode and system setting is dark mdoe.
When I change system setting to light mode and return to app, all border colors stay white which is supposed be black.
Has anyone ever faced this issue and could you please help me solve this problem?
This is serious problem when I want to implement real-time theme update in app.
Thanks.
Thanks to #KurtRevis, I finally managed to solve the problem.
You need to listen to traitCollectionDidChange. If you want to change borderColor when appearance changes, you need code something like this.
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
if #available(iOS 13.0, *) {
if (traitCollection.hasDifferentColorAppearance(comparedTo: previousTraitCollection)) {
// ColorUtils.loadCGColorFromAsset returns cgcolor for color name
layer.borderColor = ColorUtils.loadCGColorFromAsset(colorName: "CellBorderColor")
}
}
}
Hope this helps others
Depending on your circumstance you could make use of UIColor(dynamicProvider:), especially if traitCollectionDidChange(_:) is not an option (e.g. in a UIButton extension). Whenever the dynamicProvider is called upon to deliver the title color, it gives us an opportunity to update our border color as well. The example below just updates the border color to match the title color, but any other color could be used.
extension UIButton {
func applyLoginStyle() {
let titleColorProvider = UIColor { [weak layer] traitCollection in
let titleColor = UIColor(named: "myColor")!.resolvedColor(with: traitCollection)
layer?.borderColor = titleColor.cgColor
return titleColor
}
setTitleColor(titleColorProvider, for: .normal)
}
}

UIImageView.appearance is overriding UISegmentedControl.appearance

I've been trying to use the appearance proxy API to apply some default colors to some controls, but I've run into a problem.
When I apply a tint color to UISegmentedControl using something like...
UISegmentedControl.appearance().tintColor = UIColor.red
It generates this...
All good, but when I add...
UIImageView.appearance().tintColor = UIColor.green
it changes to...
Just to be clear, I have BOTH this lines in my code
UISegmentedControl.appearance().tintColor = UIColor.red
UIImageView.appearance().tintColor = UIColor.green
It doesn't matter in what order I call them, the result is the same, the UIImageView properties override the UISegmentedControls
I've spent over half a day trying to find a solution to this problem but can't seem to find anything that works.
Running Xcode 8.2, iOS 10, Swift 3
What am I doing wrong and how can I fix?
I am not sure about this, but I guess, UISegmentedControl uses UIImageView to create segments, i.e. the segments we see inside segmented control are UIImageViews and not UIViews. UISegmentedControl even has methods to setImage for a particular segment.
If above is true, we can use appearanceWhenContainedIn API of UIAppearance to set image view tint colour like this:
UIImageView.appearance(whenContainedInInstancesOf: [UISegmentedControl.self]).tintColor = UIColor.red
UIImageView.appearance().tintColor = UIColor.green

Update segmented control in ios without change interface

when I update segmented control text, the interface (segment's width) changed and cut some letters.
[segmentedcontoll setTitle:#"test" forSegmentAtIndex:1];
segmentedcontoll.apportionsSegmentWidthsByContent = YES;
How can I solve this ?
EDIT:
It looks like your content has outgrown the dimensions of the standard UISegmentedControl.
If you are okay with smaller font, it's possible to set the entire control to have a smaller font point size, seen here.
Another option is to configure the segments the other supported way.. With images. It's a little bit of a hack, but you can create images on the fly with the UIView Snapshotting API of views/labels configured however you want and set images for each segment instead of using text. This would allow you to create 2 line labels with fixed widths and set images for each section to be images generated from the label as the content changes. More work, but you would still be using the standard class.
The last option, which might work the best for you, is to create some other custom control that does what you would like. After all, UISegmentedControl really is just a nice button container. And it does somewhat seem like you are using the control in a non-standard way - both as a control and an input form section.
Others have gone this route before and created alternatives that you can use.
You can create a separate class as below,
class CustomSegmentedControl: UISegmentedControl {
//code for creating multi line
override func didMoveToSuperview()
{
for segment in subviews
{
for subview in segment.subviews
{
if let segmentLabel = subview as? UILabel
{
segmentLabel.numberOfLines = 0 //just change here the number of lines and check it.
}
}
}
}
}
and create an outlet in your viewcontroller as,
// Initialize
let items = ["Purple", "Green", "New Segment"]
let customSC = CustomSegmentedControl(items: items)
use customSC and do what ever you want to do, similar to segmentedControl object.

Change colour of dark grey highlight when holding down custom UIButton?

I have a custom UIButton which is a cloud, transparent black and white .png file, with no down state, just one image. When tapping and holding the finger over it, it turns dark grey. I'm trying to change that dark grey to something a little less oppressive. The button is out in the open in a view, not in a tab bar, tool bar, or navigation controller.
I've already tried setting tintColor (which the documentation helpfully informs me is only suitable for 'some' types of buttons, which no indication as to which).
I've also tried changing everything I can find in Interface Builder relating to highlight colours, default states, etc. Nothing has made a difference at all.
I've even tried setting the button's own image for its UIControlStateHighlighted state, but even this causes the dark grey overlay to appear when I hold my finger over it.
How can I change that colour? I've looked at numerous other issues here on SO and have been unable to find a solution that works for me. Any help would be greatly appreciated!
EDIT: I Solved the problem using a category of UIImage which adds a method that uses CoreGraphics to apply a tint to a provided UIImage. I then set THAT image as the highlight, and all is well. Seems a lot of hoop-la to change a colour Apple should've let us change, but c'est la vie.
You said you set a custom image for the UIControlStateHighlighted state. This should disable the default behaviour.
If you still have problems you can disable this effect by setting the adjustsImageWhenHighlighted property to NO and use whatever custom effect you want.
If adjustsImageWhenHighlighted = NO is not working,
set Button-Type to Custom (IB or programmatically).
Default Button-Type: System, changes behavior of highlighted button.
Swift 3:
myButton.adjustsImageWhenHighlighted = false
I was having a similar issue with a custom UIButton when the button was highlighting in grey every time it was pressed. I solved that problem by subclassing UIButton and in the implementation I overrode a single method, (void)setHighlighted: method and kept it empty:
- (void)setHighlighted:(BOOL)highlighted
{
// Leave empty to prevent super from doing whatever
// that it is doing to show the grey highlight.
}
That stopped any type of highlighting as I was not doing anything in the method. It's a better approach if all that you're trying to do is remove any highlighting effect.
So in your code, create a subclass of UIButton, override the setHighlighted method, and then make your custom button a subclass of this custom class.
You can write a custom button that does it
class ActionButton: UIButton {
var originalBackgroundColor: UIColor!
override var backgroundColor: UIColor? {
didSet {
if originalBackgroundColor == nil {
originalBackgroundColor = backgroundColor
}
}
}
override var isHighlighted: Bool {
didSet {
guard let originalBackgroundColor = originalBackgroundColor else {
return
}
backgroundColor = isHighlighted ? originalBackgroundColor.darken() : originalBackgroundColor
}
}

Resources