How to do app-wide / faster updates by UI element type, in Swift using Xcode ...? - ios

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.

Related

how to do styling/themeing in xamarin.ios app

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

Use globally defined fonts in xib

What is the best way to enforce a standard set of fonts and sizes for an iOS-app?
Our app consists of about 150 xib-files, which of many have UILabels and UITextViews. The problem is that the font is set manually for each element in each of these xib-files, which leads to small inconsistencies here and there. I want them set globally one time, like small, medium and large, and then use those three defined fonts in the xib-files. Is it possible?
You can try user defined runtime attributes, which enable you set font name or font size, or layer border color etc for UIControl in Xcode.
You could use UIAppearance but, I believe, that this is only reliable in iOS 7+.
Otherwise, you could just subclass UILabel/UITextView, set the font in the init and then use those classes in your XIBs.
Your final option would be to use Pixate Freestyle and CSS.
Why you don't try FontReplacer like https://www.cocoacontrols.com/controls/fontreplacer
The source was removed on github but you can find in somewhere.
It does not immediately apply for xib files but working good at runtime.

Changing the font of every UILabel in a large app

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 want to apply a setting globally (to all text UIViews in my app)

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.

UIScroll and its nested elements

I created a UIScrollView.
I set up the dimensions and then I am trying to add UILabels.
However the labels are all white text (annoying because I have to change the property per label).
Is there a way to make all labels (new ones that are dragged from IB to the view) have a default text color of black?
Edited to match comments
I want to use IB as much as I can. Therefore I want to drag UILabel from the Library palette to the UIView. When I do this, the UILabel is set to white (default). I want the default color to be Black. I know I can do this programatically but I am trying to avoid that unless I really really need to.
There's no easy way to do exactly what you want. But what you can do is create a label with the properties you want, store it somewhere on the drawing board but not in the view, then duplicate it each time you want a new label instead of dragging on a new one. You can duplicate easily using option+drag.
I think the short answer is "no, there's not an easy way to do what you're describing."
The easiest way I can think of would be to create all your UILabels (with the default setting of white text), then control-click them all and set their text color all at once – all the other ways are less convenient, or would essentially require that Apple open-source Xcode or UIKit so that we can get at their internals.
Yes, there is a way. You could loop the subviews of the target view such as:
UIView * targetView;
[...]
for(id subView in targetView.subViews){
if([subView isKindOfClass:[UILabel class]]){
[subView setBackgroundColor:clearColor];
}
}
why do the labels have to come from the object library? You could get the functionality that you want by dragging only one UILable from the library to your view set all the properties to the defaults that you want and hit copy(command+c) once. Now you can paste(command+v) your UILabel with the special property values as many times as you want, IBActions and outlets will also be retained in the copys.
If you plan to tweak more involved properties than font color and size, then I would suggest a more custom approach that will require only minimul coding before you do the bulk drag and drop work in IB.
Subclass a UILable in Xcode, set all of your properties just once in a simple return method and than call this method from both "init" and "awakeFromNib" Now go back to IB and do all your drag/dropping making sure that the labels are of your subclass.
However, it is my opinion that if you are doing this a lot, especially if you will be doing something similar again in the future, you will save a substantial amount of time and energy to implement this "label factory" in code. Its likely less code than you are imagining it will be and the kicker is that you can reuse it in the next app. anyway thats my 2cents, Good Luck

Resources