How to make the background of all UITextViews Purple without subclassing? - ios

I have an app that I want to debug. I'd like every UITextview within the app to have a purple background.
I think method swizzling is a possible solution, but I have not been able to get this to work.
Question: How can I do this without subclassing or manually setting each textview's background color?
Thank you

Something like this should do the trick:
[[UITextView appearance] setBackgroundColor:[UIColor purpleColor]];
The appearance proxy was introduced in iOS5 as a convenient way for styling core UIKit classes without subclassing. It can be used in a variety of really useful ways such as setting a UINavigationBar's tint color, button tints, etc.

Related

Defining colours programmatically and using them in Interface Builder

I want to customize the colours of an App reusing the same elements but changing the colours.
These elements are simple Views, UIButton, UILabel, etc. nothing fancy. The colours may come from a XML or plist I will preload and parse to UIColor.
Yes, I can create an outlet from each of them and set them manually, but I got hundreds of elements and I want to avoid this path.
I tried IBInspectable, without luck, I’m looking for a widespread solution, all views, all VCs.
I am coding in Objective-C by the way...
Could you suggest any approach on how should I do this?
Comment if you want more detailing…
Thank you all very much!
Couple options...
Subclass your UI elements, and use MYUIButton instead of UIButton, for example, or
Look at UIAppearance proxy - https://developer.apple.com/reference/uikit/uiappearance - Using that, you can set default appearance characteristics for the whole app.
Example:
[[UIButton appearance] setBackgroundColor:[UIColor yellowColor]];
[[UILabel appearance] setBackgroundColor:[UIColor orangeColor]];
If you include those two lines (often done in AppDelegate / didFinishLaunchingWithOptions), every UIButton in your app will have a yellow background, and every UILabel will have an orange background.
(However, you will not see these changes in Interface Builder)
Edit:
As Charles mentions in his comment, you can create subclasses and then apply appearance changes to only those classes.
Suppose you have 3 button "types" that you want to apply a "color scheme" to - dark-blue, medium-blue, light-blue or dark-red, medium-red, light-red, etc. You could take the approach of creating DarkUIButton, MediumUIButton and LightUIButton subclasses. Then, when you load your color scheme...
[[DarkUIButton appearance] setBackgroundColor:scheme.darkcolor];
[[MediumUIButton appearance] setBackgroundColor:scheme.mediumcolor];
[[LightUIButton appearance] setBackgroundColor:scheme.lightcolor];

how to change ui componts color according to iOS theme?

I am very new to iOS.I want to set the theme for my iOS app & according to theme i am changing the background image.I also want to change the color of other ui component according to background image.I have one way of doing it by saving value in UserDefaults & check for every UI Component that would be very lengthy process.Is there any kind of other simple way of doing it in iOS?
You can take advantage of appearance proxy. Most of UIKit components have an appearance proxy object.
Let's take for example a navigation bar.
You can have this code.
UIColor *barColor = [UIColor redColor];
[[UINavigationBar appearance] setTintColor:barColor];
Once you execute this, all navigation bars across your app will have a red tint colour. You can customise other things and this way you set your theme globally.

IOS UIAppearance

I have the following issue. I have a custom UIView which background I am trying to set using UIAppearance.
[[OptionsHeader appearance] setBackgroundColor:[UIColor headBackgroundColor]];
But when I use it like this, all the views turns to black and white.
[[UIView appearance] setBackgroundColor:[UIColor headBackgroundColor]];
Works nicely, but I need it to work nicely on that specific class.
Any help is welcome.
Kind Regards,
EZFrag
You need to implement the UIAppearance protocol in your custom class, as described here. Basically, you 'need' to augment your custom class and annotate it with UI_APPEARANCE_SELECTOR to define what (and how) the appearance delegate can interact with your class.
I say 'need' because it doesn't really seem to be required, but it's hard to be sure...

Some UI elements don't acquire UIAppearance traits

I am trying to use UIAppearance to get a uniform color theme in my iOS app. For example I try to set the text color of all UILabel objects as follows:
[[UILabel appearance] setTextColor:[UIColor colorWithRed:0.7 green:0.07 blue:0.12 alpha:1]];
This works fine for all objects statically defined in my storyboard/XIBs. However, sometimes I need to dynamically create a UILabel in a view. In these cases, the UIAppearance is not used. Instead the default text color (black) is used instead.
Has anyone run into this issue/ found a way around it other than resorting to the old "set every element manually" approach?
Seems that not all the classes support UIAppearance and UILabel is not one of those.
Check this question for more info:
UIAppearance not taking effect on UILabels created programatically
Here's a list of classes that support UIAppearance:
http://blog.mosheberman.com/list-of-classes-that-support-uiappearance-in-ios-5/

Alternatives to appearance proxy for Table Cells' UILabel setFont

I'm loving being able to use an appearance proxy in my iOS5 app to customise the NavigationController UI elements system-wide, however:
I have a lot of nested tables in my app, of which I'd love to be able to change the font across all table cells. They are mainly statically created. I've put some cell generation inside of a class, being read from an array, but the main issue lies with a large contents section I've tabled in Interface Builder. IB doesn't seem to have the option for mass-font setting.
I was wondering if anyone could help me find a way to set all the UITableCell fonts in one go?
Perhaps something like:
[[UILabel appearance] setFont:[UIFont fontWithName:#"Times" size:17.00];
You can use appearanceWhenContainedIn: to narrow down which UIViews you'd like to set the appearance for.
In your example, try:
[[UILabel appearanceWhenContainedIn:[UITableViewCell class], nil]
setFont:[UIFont fontWithName:#"Times" size:17.00]];
Actually, no. It is not valid to use UIAppearance to style a UILabel. See my self-answered question here. It kinda works, but it's not valid, for the reasons outlined over there.

Resources