Custom font with size classes in iOS - 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...

Related

Using custom font for different trait collections in storyboard doesn't reflect in app

When I'm using custom font for a UILabel or UIButton for multiple trait collection through storyboard like in the following picture I am facing an issue in UI updation while running either in simulator or in device.
The issue is instead of the custom font system font gets assigned while running the app.
FYI
When I use the same custom font for default single trait collection the font is reflecting successfully. But the same fails if I use it for different traits.
And if we apply the font for different trait collection through code as shown in the following snippet the UI is updating successfully.
if ([self respondsToSelector:#selector(traitCollection)]) {
if ((self.traitCollection.horizontalSizeClass == UIUserInterfaceSizeClassCompact) && (self.traitCollection.verticalSizeClass == UIUserInterfaceSizeClassRegular)) {
self.customButtonFont = [UIFont customFontWithSize:18.0f];
}
}
Does anyone know the reason for this issue and can I achieve this through storyboard itself ?

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.

Obj-C, how to iterate all views for views with font properties?

I'm trying to initially survey (log) all the fonts and font sizes I use in my app, fonts / sizes, that could be set programmatically or in nib files. Afterwards, I hope to use the code to set fonts too.
(I am aware of font type dynamics, however I'm have auto layout issues.)
I've got so far (see below), but ideally I'd like to just find anything with a font, I mean it could be a navigation bar not just buttons, labels or textfields.
Maybe I could somehow check to so see if the font method was a available ?
NSArray* buttonsArray = currentView.subviews;
for((UIButton*) button in buttonsArray)
{
button.titleLabel.font = [UIFont fontWithName:#"myCustomFont" size:16];
}
While asking if an object responds to font or setFont: is technically working, we now have the UIAppearance system, which are designed to give the app a consistent design. You can do things like "all my navigation bars should be green", or "all buttons should be blue, except those which are contained in .... which should be yellow". Many attributes are configureable, and setting this up is as easy as listing the desired appearance patterns for example in applicationDidLoad:.
http://nscookbook.com/2013/01/ios-programming-recipe-8-using-uiappearance-for-a-custom-look/ is one of the short introductions available on the net.
The good thing about using this instead of hacking something together while traversing your view tree is that it will work for all views, even those that you didn't create so far. If you set it manually, you have to do so after each view creation, which is tedious, wasteful and open to bugs. Also, you're less likely to break things that you didn't mean to, like third party classes.
You can check if an object responds to -font by sending it the -respondsToSelector: message, like this:
if ( [ obj respondsToSelector:#selector( font ) ] )
{
UIFont * font = [ (id)obj font ] ;
}
You can assign if the setter of the font is responding:
Note: code didn't tested
for(id obj in buttonsArray)
{
if ( [obj respondsToSelector:#selector(setFont:)] )
{
// do something
}
}
[obj respondsToSelector:#selector(font)];
if the method is retuen YES,then meant obj has font method.

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

Selecting custom fonts through IB in Xcode

I read many tutorials explaining how to add custom fonts to iOS apps.But in every tutorials its done through code.I mean, if i need change the font of 10 labels,i have to write code for each labels.I would like to know is there any method to add custom fonts to my project and then select that font from the storyboard ,i mean from the attributes list of label object..
Please help me in solving this....
That cannot be achieved through the interface builder only. However you can create a custom class subclassing UILabel. You define the .m file as follows:
#implementation CustomLabel
-(void) awakeFromNib{
[super awakeFromNib];
self.font = [UIFont fontWithName:#"<your custom font's file name>" size: self.font.pointSize];
//set other settings of the custom label here (colour, etc.)
}
#end
Then in the .xib file whenever you use a UILabel, set the class to be CustomLabel.
This can be done in Storyboard or Interface Builder with the use of very simple categories and user defined runtime attributes.
Something like this:
Please check this response for more details on how to achieve it.
Try This One:
UIFont *font = [UIFont fontWithName:#"YourFontName(without Extension)" size:30.0f];
self.yourLabel.font=[font fontWithSize:28];
add your font name in your projectname.plist like this
Fonts provided by application ---->Array------>2(items)
item0 --------->string----->urfontname with extension(ttf/otf) also
item1 --------->string----->secondfont name with extension(ttf/otf) also
This could now be done through Interface Builder in Xcode 6.
Now when you add custom font to your project and after making sure it is properly setup in your target settings, you should see your font in the regular font selector. Simply select Font > Custom, Family > your custom font should be listed here.
I currently have a bug after selecting the font in beta5 but this is how to do it. I installed Xcode 6 only for this feature (far more convenient than defining it programmatically everywhere).

Resources