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

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.

Related

UITextField font-size when content and losing focus

I have a UITextField for which I would like to assign a custom font and font-size like this
self.txtEmail.font = UIFont("HelveticaNeue-Light", size: 24)
When I start the app everything looks good. My placeholder text gets the new font and if I click on the input field and start typing, the text has the new font. However, when I click somewhere else and the input field lose focus the font is reset to the default font. What I find even more strange is that I only have set this default font for UILabels and not UITextField by adding this line in the application method inside AppDelegate. If I try to set the default font for UITextField nothing happens.
UILabel.appearance().font = UIFont(name: "HelveticeNeue", size: 18)
So my question is why I get the behavior where the UITextField has the correct font until it gets content and lose focus.
I have 1 general remark on UI elements' appearance management and one more on UITextField.
A) Appearance:
The documentation says:
To customize the appearance of all instances of a class, use appearance to get the appearance proxy for the class.
In fact, that means that your changes will be applied to every instance of UILabel that enter your application window (as an iOS app has only 1 "public" window). If you want to customize the appearance of labels contained within a particular container class (e. g. inside UIView), you should consider using
+ appearanceForTraitCollection:whenContainedIn: (unfortunately, it's been deprecated since iOS 9.0). A much safer way is to use inheritance for your purposes as follows: MySpecialLabel -[inherits from and applies custom style attributes within its initializers and/or the awakeFromNib: method]-> UILabel.
B) Why is it related to UITextField?
That's because UITextField utilizes UILabels under the hood (like _placeholderLabel and _promptLabel).
I hope it will help you solve your problem.

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.

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

Set the default font style, color, and size for an xcode project

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.

Resources