Let's say I want to use a different font, different font sizes, and a different color scheme for my app, and let's say I want to use Interface Builder.
I want to be able to style all these in one place, instead of say going to each label on Interface Builder and changing its font, color, etc.
What is the most common way to achieve this?
I know you can set these things up in code, but then I can't see the changes in Interface Builder?
Having to change these all one by one is a maintenance nightmare, and I can't seem to find any easy way to create custom styles directly in Interface Builder.
The only way I can think of is subclassing each of these views, such as label, button, etc., creating XIBs for each, and making them #IBDesignable.
Is this the way to go? It feels like it's just an unnecessary amount of work, for something simple.
If you want to change simple properties of UIKit controls, you can use UIAppearance.
With UIAppearance, You can change appearance like, TextColor, Backgorund color, Tintcolor etc for almost all UIKit Controls.
for ex:
Change UIbutton's title Color appearance:
UIButton.Appearance.SetTitleColor (UIColor.Brown, UIControlState.Normal);
Change UILable's Background Color appearance
UILabel.Appearance.BackgroundColor = UIColor.Blue;
Well, If you want to customise it to next level- Only way is by long process like Subclassing controls as you explained in your question
Related
Just curious - doing some maintenance on a swift app, bit time consuming, would love to know if there is a better recommended way, or faster way (?), to do mass updates for a UI element type, e.g. update all buttons, to have a certain property, e.g. say a color or width constraint...?
Color, yes, by using the button's appearance proxy, like
UIButton.appearance().backgroundColor = UIColor.whateverColor()
Width constraint, no. There are a couple other ways to do it, though.
If your regex skills are sharp, you can do a search & replace in all of the storyboards' and xibs' XML code to add a width constraint to each, but that'd be error-prone because some of them may already have width constraints.
You can subclass UIButton and give it a width constraint and set any other properties you wish, but you'd still have to search-and-replace all UIButtons in the appearance files with your custom class type.
For some mass updates, like changing fonts or colours of a control, you could use the UIAppearance proxy. You can also extend classes to add more options for controlling the design of views and controls. Just add methods marked with UI_APPEARANCE and implement as needed.
I'm using a custom button type since I need the touch areas to be non-rectangular so am using a class called OBShapedButton which I shamelessly found online. However, I also want to color these buttons to match the player's color.
When talking about UIButton and tintcolor, Apple's docs say
This property has no default effect for buttons with type
UIButtonTypeCustom. For custom buttons, you must implement any
behavior related to tintColor yourself.
So how would I go about implementing this behaviour myself?
you could create an array of colors using UIColor within your class then access them individually have you tried it that way?
ok as an example something like this
UIBarButtonItem.appearance().tintColor = UIColor.yellowColor()
ideally can you post your class code aswell
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
The app I'm working on has a particular color scheme, so I was wondering if it is possible to set a "default" background color, text color, and font size so that every time I create a new view, or label in Interface Builder I don't have to change all of these parameters.
As of iOS 5, there is the UIAppearance protocol that all of the standard UI elements implement. Using the "Appearance proxy" you can set the appearance for ALL objects of that type in a given application. More information can be found in the incredibly helpful and informative WWDC '12 session on "Advanced Appearance Customization on iOS"
How about just creating a DanFViewController (subclassed from UIViewController) that already has the specific background color, text color, font size, etc. you need and then derive all your app's custom views from that?
That's what I do in my own apps where clients are expecting a certain look & feel for all the views.
are there any alternatives to using the accessor setTintColor for UIBarButtonItems in order to add ios4 compatibility?
is it possible to modify the setter to include a conditional statement for systemVersion (without subclassing UIBarButtonItem)?
There are three approaches to do this. First, is to draw a button with CoreGraphic. Second, use a custom image. The third approach is probably the easiest and allows you to use a tintColor property for dynamic changes if wanted.
The idea is to create a UISegmentedControl and stylize it to look like a button and use that tintColor property to change the color of the "button".
You can find specific details at this webpage http://charles.lescampeurs.org/2011/02/10/tint-color-uibutton-and-uibarbuttonitem .