Use globally defined fonts in xib - ios

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.

Related

Disable Automatic adjusting font for UILabel,UIText,etc. in iOS/Swift?

Go to "Settings"-> "General"-> "Accessibility"-> "Larger Text" to change the font size.
Open my app, now all the UILabels and UIButtons do change accordingly. I want to disable it and want to keep as it is regardless of the large font or small font on the setting. I have made everything programmatically. How can I disable this?
myLabel.adjustsFontForContentSizeCategory = false
Although this depends on the font that you're using. See more info here: https://developer.apple.com/documentation/uikit/uicontentsizecategoryadjusting/1771731-adjustsfontforcontentsizecategor?language=objc.
Specifically, this part:
For this property to take effect, the element’s font must be vended
one of the following ways:
It must be vended using the preferredFontForTextStyle: or
preferredFontForTextStyle:compatibleWithTraitCollection: method with a
valid text style.
It must be vended using one of the scaling methods from UIFontMetrics.

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

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.

Custom fonts using xcode through attribute inspector

I am using Xcode 6.1 interface builder.
I want to have a universal UILabel font throughout the app in which I can easily change.
For example I want all my UILabel to use font 'A.otf' and later I would like to update all my UILabel to be something else like 'B.otf'
My first attempt is to subclass UILabel(lets call it MyCustomUILabel) and set its font attribute to whatever font I want. With this solution, I just have to make sure I set the UILabel to MyCustomUILabel in the identity inspector. And if I do need to change the font for all MyCustomUILabel, I will need to change it within the MyCustomUILabel class.
My second attempt is to use Xcode's built in custom font helper(attribute inspector -> font icon -> custom -> select my font). I was thinking if Xcode already provided a way to custom a font of my UILabel, then why create a custom class?
Using the Xcode built in custom way, I have a UILabel using 'GoodPro-Book.otf' as the custom font right now. I wanted to update this UILabel's font to 'Sanitarium.otf' and I was hoping that all I have to do is just rename my 'Sanitarium.otf' to 'GoodPro-Book.otf' and replace the one inside Xcode with this new one.
Of course it didn't work out.
So, I would like to ask, is this possible? in Xcode? Am I missing anything?
thanks, any suggestions and opinions are appreciated.
I think the font family and style are contained within the font file, so renaming the file doesn't have an effect. Unfortunately, I've never found a way to change Xcode's default font for a project (that would just make things too easy).
I think you're right in that the best way to change them all at once is to subclass UILabel and make sure you always use your subclass. Otherwise you have to change them all by hand.

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

How do you have two fonts in one UITextView (Xcode)?

I'm trying to make two font styes in one UITextView, how do I do this?
From the class reference:
This class does not support multiple styles for text. The font, color,
and text alignment attributes you specify always apply to the entire
contents of the text view. To display more complex styling in your
application, you need to use a UIWebView object and render your
content using HTML.
You cannot have two on the same page because it is not supported. Just use a webview and an HTML file
You can know use this by using the attributedText property of UITextView. This is available under iOS 6
UITextView supports just a single font, but there's a different topic on something similar:
Can I use multiple font style in UITextView?
If the text you want to draw is simple, I'd suggest subclassing UITextView or UIView, overwriting the drawRect function and work with some extra variables of your own. This only works if you have a very predictable system to the fonts though.
Another option is using multiple labels, which would probably need an ever more predictable setup.

Resources