How to change default System font for xib? - ios

I've swizzled systemFontOfSize:, boldSystemFontOfSize:, and systemFontOfSize:weight: but when I set the font to "System" in a xib it still sets it to the default value. How can I change the default "System" font for xibs?
I know I swizzled these methods properly because when they are called programmatically it works as expected.
I'm not interested in a solution related to setting label appearance because I need to be able to change the font size.

In my suggest, you can also swizzled initWithCoder: for UILalbel ,UIButton ,UITextField. And modify the font according to your need in the swizzling method.

Related

iOS - Dynamic Type and Interface Builder

Trying to implement support for Dynamic Type and have an issue. I set the style I want to use on a label or something in Interface Builder. I register for the UIContentSizeCategoryDidChangeNotification, and in the handler, I set the label's font to ... what? How do I know what style to use? Shouldn't there be an accessor that lets me find that out? If not, I have to put it in 2 places, which means they'll get out of sync and I'll be annoyed. Any thoughts?
I don’t think this will satisfy you, but set the font to [UIFont preferredFontForTextStyle:UIFontTextStyleTitle2 or whatever style you set in Interface Builder.
Ignore the setting in Interface Builder. It’s not even worth setting. Interface Builder is a (mostly) static representation of the initial state of your views, but this is Dynamic Type.
You could subclass UILabel to make it dynamic, and/or join us on the dark side of setting up views in code.
Since iOS 10, there's no need to follow this rationale because the adjustsFontForContentSizeCategory property allows an automatic scaling of the font sizes according to the content type size selected in the settings.
All the text styles are well defined in the Apple reference site and their size variations as well.

Custom font with size classes in iOS

I'm trying to add 2 different font sizes for iphone and ipad layouts using size classes. It works cool with a default System font but doesn't work with custom font(I'm using PragmataPro in my project). If I add the second size for wR hR then font looks correctly in interface builder(I even checked xml) but in simulator and on device it becomes System instead of PragmataPro. But if I remove wR hR(or whatever layout I'm using for another size) then font shows correctly. Any idea how to solve this issue?
Thanks!
Subclass UILabel and override "layoutSubviews" method like:
- (void)layoutSubviews
{
[super layoutSubviews];
// Implement font logic depending on screen size
self.font = [UIFont fontWithName:#"CustomFont" size:self.font.pointSize];
}
Follow the link (it is a step-by-step from Apple):
https://developer.apple.com/library/ios/recipes/xcode_help-IB_adaptive_sizes/chapters/ChangingtheFontforaSizeClass.html
The 'custom' is there to define one single setting per type class.
If you don't define a 'custom' library with all the variations you want than I don't really think there to be a solution, mainly because by 'custom' you mean ONE SINGLE COMPONENT OF A GIVEN LIST OF CHOICES...

Change System font for testing purposes

I'm currently working on an iOS project which has tons of labels/buttons/controls spread over dozens of scenes.
Most of those controls were created using Interface Builder.
So, it's now my job to make sure that every control (especially the labels) is formatted with the correct font family, which is not the case at the moment because many devs simply forget to change the font (our font must be set programmatically since it's not included in IB) after adding the control in IB.
Is there a way to change the system font temporarily so it's easier to see where font-settings have been forgotten?
I've searched for:
Changing the system font programmatically
Changing the font in Xcode somewhere
Changing the font in the iOS simulator (maybe as a debugging option)
But I was unsuccessful so far. I can't be the only one with this kind of problem - it's just naturally tedious to set every single control font programmatically.
The only thing I could imagine is like overriding the base UILabel's drawing method with a custom font (wingdings anyone?), but that seems a bit excessive?
You can try creating a category, which overrides the systemFontOfSize: method of UIFont or use method swizzling (you can find out more about method swizzling here: http://cocoadev.com/MethodSwizzling). Both are extremely ugly and shouldn't be used in production, but should be fine for testing purposes.
Here is an example category of UIFont:
#interface UIFont (SysFont)
#end
#implementation UIFont (SysFont)
+ (UIFont *)systemFontOfSize:(CGFloat)fontSize {
return [UIFont fontWithName:#"YourFont" size:fontSize];
}
#end

Set UIKeyboardAppearance across the whole app

Is there a useful property in a cocoa-touch project that would allow setting the one-and-only consistent style of keyboard appearance throughout the app? Say I want UIKeyboardAppearanceAlert on all the textFields and textViews I have in my app without directly modifying anything. Is is possible?
No, it isn't possible. The keyboardAppearance is part of the UITextInputTraits protocol and is not marked as a UIAppearance method. If it was you could do something like:
[[UITextField appearance] setKeyboardAppearance:UIKeyboardAppearanceAlert];
You can identify methods that can be used with the appearance proxy by looking at the docs or, from within your code, at the UIKit headers (command-click on a method and it will take you to the header.
For example, in UINavigationBar.h, you can see this:
#property(nonatomic,retain) UIColor *barTintColor NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR; // default is nil
The marker UI_APPEARANCE_SELECTOR means that this property can be used on the appearance proxy. It isn't present on keyboardAppearance, and it doesn't look like any keys in the info.plist allow you to define an application-wide appearance.
Your best bet is to subclass textfield and textview and use those subclasses everywhere.

Using constants in Nib files

I have an App with few Nibs, the Nibs are built according to a color scheme. Now I have a requirement to change the color scheme. So I need to go to each Nib, and each component in it and change its color.
I was wondering whether I can tell the Nib to read the color from a "#define" so it would be easy to make these kind of changes in the future.
Or any other way to change the Nib content not by going to each Nib and changing it but by doing it is a central place.
There is currently no way to use constants or defined symbols inside the context of a Nib file. You should consider filing an enhancement request with Apple at http://bugreporter.apple.com for this functionality.
Wouldn't be suitable to set IBOutlet for each element that required color customization and then set color in code?
Even after creating the nib with some other colour, you can still change its the background colour's value in lets say the viewDidLoad function or in any other function to the #define-ed value.

Resources