Custom fonts using xcode through attribute inspector - ios

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.

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.

UILabel Font Size Does Not Change in XIB while Color Does

This question seems simple, but this problem turned me crazy after days since none of the answers could solve my problem so far. I will be glad if someone can help me to figure it out.
So, I have a .xib file where I created a subview (called StaticDetailVerticalView) and I have also another .xib file for the whole page's view (called TripDetailView), and lastly, this TripDetailView is being used inside of a controller (called TripDetailViewController). My font problem occurs in StaticDetailVerticalView.
First, I was trying to set the fonts with an outlet from .xib with the methods [self.label setFont:[UIFont systemFontOfSize:20.0] and self.label.font = [self.label.font fontWithSize:20.0]. Then secondly, I tried setting from the TripDetailView by creating a IBInspectable fontSize inside StaticDetailVerticalView with direct setting from TripDetailView.xib file.
Also, when I try to change the color or the text by calling the methods exact same places with font changes, everything changes accordingly but the font size.
I hope there will be an answer to be accepted :) Joking aside, I really stuck in here.
Please ask me for further details, or any files that I can share for you if you need one. I am not adding the codes or screenshots since I believe this is not a problem by code based but something that I am missing about the outlets.
You can find the all three .xib files mentioned above here.
Thanks for all the helps and answers!
You can try to adjust the font size in your StaticDetailVerticalView.xib
Go to StaticDetailVerticalView.xib
Clic on the label you want to change the font's size
Clic on the button "Show the attributes inspector"
Then, you can change the font manually.

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.

iOS Format Background

I am creating a simple quote app as a learning experience and need to format it to look better.
I am trying to make the background have a "white square" where the text is and make the text black. Just like the Twitter app has when you click on a tweet.
What is the best way to do this and what should I start with?
I know it is very simple, I'm only 15 and am trying to learn iOS. As you can see I have a server and have the app up and running, just want to format before I call it complete. Thanks! :)
-Henry
This will depend on how you created the label that displays your text, but you can do this very easily:
If you created your UI in a Storyboard / XIB file in XCode: assuming you dragged out a UILabel or UITextView, you can use the Attributes Inspector to set the text color and the background color (in the View section) of the element holding the text.
If you created the label (or text view) in code, you can set the textColor property and the backgroundColor properties programmatically.

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

Resources