Selecting custom fonts through IB in Xcode - ios

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).

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

Base Internationalization doesn't work in custom class

I create a subclass of UILabel, in which I set font for its text(and this is the only thing I do in this subclass):
[self setFont:[UIFont fontWithName:#"mplus-1c-regular" size:14.0]];
I use Base Internationalization to do i18n for my project, this is the related translation in my storybaord.strings:
"kjN-FS-SlD.text" = "進步來自精準分析。";
If I set this subclass as the custom class of this label in interface builder, this translation doesn't work. It shows English text, which is my default language of base internationalization, just like my system language is English. However, if I don't set the custom class(leave it blank), it works as normal.
How can I make it work for my custom class?

Can't set custom Font in UILabel (iPad app)

I load view from bundle and try to set custom font for label in it, but nothing changes.
Outlets for this view and labels are in file's owner.
Here's my code:
self.rikView = (RIKMainToolbar*)[[[NSBundle mainBundle] loadNibNamed:#"RIKMainToolbarPort" owner:self options:nil] objectAtIndex:0];
label.font = [UIFont fontWithName:#"PFDinTextCondPro-Medium.ttf" size:46];
What am I doing wrong?
UPD: I use this font in iPhone app, but there it's using for label in ViewController and sets in viewDidLoad function. And it works. So, I consider that problem is in loadWithNibName or in iPad.
Solved, thanks to #voromax. The problem was that labels' outlets belonged to file's owner. I've replace it with class and everything works)
I'm not sure I follow your code exactly, but assuming you're making the label.font assignment in initWithFrame of RIKMainToolbarPort, try moving it to the awakeFromNib method.
Open the FontBook application, and select "view font info". Copy the post-script-name of your desired font, and use that in your Obj-C code
You are not using the correct font name.
You seem to be using the font file's name, NOT the font name.
For example, in my current app I have the file Opal_1.ttf. However, I use "Opal" as the font name.
Check the example here: https://stackoverflow.com/a/1384206/855738

Resources