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.
Related
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];
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...
Here's an interesting problem for you:
We are in the process of re-skinning our entire app, which consists of over 100,000 lines of code and almost 100 XIB files. The new design requires (almost) every label in the app to use a new custom font, whereas the old app uses the default font of Helvetica Neue.
What is the most efficient way of changing as many of the UILabels as possible? We don't want to have to manually change thousands of labels both in code and in XIBs (which is especially difficult because we are using a non-system font).
We've discussed swizzling UILabel's creation methods to default to using the new custom font, which would still allow for the few labels that would remain in Helvetica Neue to be customized after creation.
Thoughts?
Take a look at NUI https://github.com/tombenner/nui.
You can customize a lot controls with it using a CSS-like stylesheet and implement it via subclasses or categories.
You should probably subclass UILabel and override either an initmethod of your choice, or awakeFromNib, and set the custom font there. Then you can go through the xib, either manually, or open it up in a text-editor and add a line like
<string key="X.CustomClassName">YourCustomLabelClass</string>
To set the specified label to your custom label class.
Swizzling is a good option . If you want to do it other way and have problems with custom fonts, I usually make a trivial subclass of UILabel that changes it's font in awakeFromNib for each custom font so I can lay them out in the interface builder.
But... This comes to my mind:
[[UILabel appearance] setFont:xxx];
I am not sure how you would deal with keeping the size, though. Perhaps swizzle is the best choice
I have a bunch of text views, and I now want to turn autoresizing on in all of them. I don't want to highlight each one individually, and I only want the changes to apply to all text views in the app. So I don't want to modify the framework. I will be sure to pick the best answer.
EDIT: I guess I should have mentioned earlier that I am using storyboards.
In iOS 5.0+, you should be able to use the appearance proxy for UITextView like so:
[[UITextView appearance] setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
This would make all UITextView's respond with a flexible width and height. Here's a great tutorial on how to use the proxy for other UI controls:
http://www.raywenderlich.com/4344/user-interface-customization-in-ios-5
EDITED:
This code should work and I've tested this in both the simulator and on an iPhone directly, even though the autoresizingMask property in UIView.h is not flagged with UI_APPEARANCE_SELECTOR. This post seems to indicate that its possible for a property to not have this flag, yet still obey messages from the appearance proxy. All that said, #rmaddy is right. It might be a risk to rely on this long-term.
One option is to subclass UITextView and create a CustomTextView. In its init methods or layoutSubviews method, set the autoresizing mask and then use that CustomTextView in your project. One advantage with usage of init method for setting this is that, you can later override this autoresize mask.
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/