Is there a way to change default font for your application - ios

As I understand all standard controls use system font by default.
Also, API [UIFont systemFontOfSize: ] uses system font too.
Is there a way to redefine it for the whole application, instead of setting a font for table, labels and so on?

The answer is no, you cant change apple's systemFont, you need to set the font on your control yourself.
For best way to set a default font for whole iOS app, please check the below SO questions:
Set a default font for whole iOS app?
How to set a custom font for entire iOS app without specifying size
How do I set a custom font for the whole application?
Is there a simple way to set a default font for the whole app?

Define and export (by including a header in files or in precompiled header) a category of UIFont as following:
#implementation UIFont (Utils)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wobjc-protocol-method-implementation"
+ (UIFont *)systemFontOfSize:(CGFloat)size
{
return [UIFont fontWithName:#"YOUR_TRUETYPE_FONT_NAME_HERE" size:size];
}
+ (UIFont *)lightSystemFontOfSize:(CGFloat)size
{
return [UIFont fontWithName:#"YOUR_TRUETYPE_FONT_NAME_HERE" size:size];
}
+ (UIFont *)boldSystemFontOfSize:(CGFloat)size
{
return [UIFont fontWithName:#"YOUR_TRUETYPE_FONT_NAME_HERE" size:size];
}
+ (UIFont *)preferredFontForTextStyle:(NSString *)style
{
if ([style isEqualToString:UIFontTextStyleBody])
return [UIFont systemFontOfSize:17];
if ([style isEqualToString:UIFontTextStyleHeadline])
return [UIFont boldSystemFontOfSize:17];
if ([style isEqualToString:UIFontTextStyleSubheadline])
return [UIFont systemFontOfSize:15];
if ([style isEqualToString:UIFontTextStyleFootnote])
return [UIFont systemFontOfSize:13];
if ([style isEqualToString:UIFontTextStyleCaption1])
return [UIFont systemFontOfSize:12];
if ([style isEqualToString:UIFontTextStyleCaption2])
return [UIFont systemFontOfSize:11];
return [UIFont systemFontOfSize:17];
}
#pragma clang diagnostic pop
#end
Light variant comes as a useful extra starting with iOS 7. Enjoy! ;)

Related

San Francisco font: bold + italic?

In the past, if I wanted to use bold and italic on the same text, I'd select a font by name, like this:
font = [UIFont fontWithName:#"HelveticaNeue-BoldItalic" size:size];
Now I'm trying to do this with the current iOS system font, San Francisco. This answer says it's not safe to refer to this font by name, and indeed, when I try it, some font-related methods do nothing and some crash the app.
Of course there is a boldSystemFontOfSize and italicSystemFontOfSize method, but no boldItalicSystemFontOfSize method.
Does that leave any other way to use bold+italic with the San Francisco font?
Here's a category based on the answer suggested by #rmaddy:
#interface UIFont (UIFontBoldItalic)
+ (UIFont *)boldItalicSystemFontOfSize:(CGFloat)fontSize;
#end
#implementation UIFont (UIFontBoldItalic)
+ (UIFont *)boldItalicSystemFontOfSize:(CGFloat)fontSize {
UIFontDescriptor *fontDescriptor = [UIFontDescriptor preferredFontDescriptorWithTextStyle:UIFontTextStyleBody];
uint32_t existingTraitsWithNewTrait = [fontDescriptor symbolicTraits] | UIFontDescriptorTraitBold | UIFontDescriptorTraitItalic;
UIFontDescriptor *updatedFontDescriptor = [fontDescriptor fontDescriptorWithSymbolicTraits:existingTraitsWithNewTrait];
UIFont *font = [UIFont fontWithDescriptor:updatedFontDescriptor size:fontSize];
return font;
}
#end
Although this doesn't specify "system font" or "San Francisco" anywhere, I assume the UIFontTextStyleBody will return the system font.

How to change iOS system font dynamically but remember original font?

I am able to change the language of my iOS app on the fly without terminating the app. However, I want to change the system fonts on the fly too because the standard Helvetica Neue fonts don't look good with some languages. I'm going to use a category from this SO answer and include it in my .pch:
#implementation UIFont (SytemFontOverride)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wobjc-protocol-method-implementation"
+ (UIFont *)systemFontOfSize:(CGFloat)fontSize {
return [UIFont fontWithName:#"FONT NAME" size:fontSize];
}
#pragma clang diagnostic pop
#end
However, when in English I want to use the default system fonts which vary slightly from iOS version and device. My question is how can I achieve something like this
static UIFont *defaultRegularFont = [UIFont systemFontOfSize:12];
so I can reuse it when the app switches back to English like this
+ (UIFont *)systemFontOfSize:(CGFloat)fontSize {
...
// If English
return [UIFont fontWithName:[defaultRegularFont fontName] size:fontSize];
}

how to make font bold,unbold of any font style

Can anybody please explain how can we make any font family font, bold or unbold + Italic or Non Italic + Underlined or Non underLined. Everywhere I got the method that make the changes but on system font. I even tried giving 2 attributes to NSAttributed string
1. Bold
2. A font family from list of supported font family
But it didnt work. Thanks in Advance.
try this
UIFontDescriptor *fontDescriptor = [[UIFontDescriptor alloc] init];
UIFontDescriptor *fontDescriptorForHelveticaNeue = [fontDescriptor fontDescriptorWithFamily:#"Helvetica Neue"];
UIFontDescriptor *symbolicFontDescriptor = [fontDescriptorForHelveticaNeue fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitBold];
UIFontDescriptor *symbolicFontDescriptor1 = [fontDescriptorForHelveticaNeue fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitMonoSpace];
NSString *text = #"iOS 7";
if(some condition){
CGSize fontSize = [text sizeWithAttributes:#{NSFontAttributeName:[UIFont fontWithDescriptor:symbolicFontDescriptor size:17.0f]}];
}
else{
CGSize fontSize = [text sizeWithAttributes:#{NSFontAttributeName:[UIFont fontWithDescriptor:symbolicFontDescriptor1 size:17.0f]}];
}
You can't do it with a few lines of code. A normal font and its bold version are completely separated (ie they're like two unrelated fonts), and if you have a look at iosfonts, the naming is not consistent. Some fonts don't have bold version, and some have several bold versions!
A solution (that requires a bit of effort, but surely works): create a list of pairs of font names, like this
{"ArialHebrew", "ArialHebrew-Bold"},
{"AvenirNext-Regular", "AvenirNext-Bold"},
...
And populate the list of fonts with the regular version (the left one). If the user desires to make it bold, then switch to the bold version (the right one).
My recommendation is to limit the number of choices for user (as iOS always does): you don't need to copy the whole list from iosfonts! just some popular ones are enough.
This may not be sufficiently generic for your purses, but maybe it'll help someone else
+ (UIFont *)whateverInvertedBoldnessFontFromFont:(UIFont *)font pointSize:(CGFloat)pointSize
{
NSString *customFontFamilyName = #"Whatever";
NSString *ibFontName = font.fontName;
NSString *customFontStyle = nil;
if ([ibFontName rangeOfString:#"Bold"].location != NSNotFound) {
customFontStyle = #"Regular";
}
else {
customFontStyle = #"Bold";
}
UIFont *customFont = [UIFont fontWithName:[NSString stringWithFormat:#"%#-%#", customFontFamilyName, customFontStyle] size:pointSize];
return customFont;
}
unfortunately this is quite fragile and works reliably when the source and destination
font families are known
If you want to set it programmatically, you must check with the font supported by xCode (iOS).
and if you want to do bold to any font then you have to use :
UIFont* boldFont = [UIFont boldSystemFontOfSize:[UIFont systemFontSize]];
[myLabel setFont:boldFont];
where myLabel is your label name.

How do I create a bold UIFont from a regular UIFont?

If I have a UIFont object, is it possible to convert it to bold? I don't know the font name, I just have a UIFont object. What I want is a function like
UIFont *boldFontFromFont(UIFont *input)
{
return [input derivedFontWithFontWeight:UIFontWeightBold];
}
How can I change the code so that it works. (The code above does not work, I just made it up to illustrate the point.)
Thanks in advance.
iOS 7 introduces a new UIFontDescriptor class, which makes it a lot easier:
UIFont *font = [UIFont fontWithName:#"Helvetica Neue" size:12];
NSLog(#"plain font: %#", font.fontName); // “HelveticaNeue”
UIFont *boldFont = [UIFont fontWithDescriptor:[[font fontDescriptor] fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitBold] size:font.pointSize];
NSLog(#"bold version: %#", boldFont.fontName); // “HelveticaNeue-Bold”
UIFont *italicFont = [UIFont fontWithDescriptor:[[font fontDescriptor] fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitItalic] size:font.pointSize];
NSLog(#"italic version: %#", italicFont.fontName); // “HelveticaNeue-Italic”
UIFont *boldItalicFont = [UIFont fontWithDescriptor:[[font fontDescriptor] fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitBold | UIFontDescriptorTraitItalic] size:font.pointSize];
NSLog(#"bold & italic version: %#", boldItalicFont.fontName); // “HelveticaNeue-BoldItalic”
For people who got here looking for a Cocoa (macOS) equivalent, UIFontDescriptor comes from NSFontDescriptor, available since 10.3.
And if you are looking for the swift implementation:
let normalFont = UIFont(name: "FONT_NAME", size: CGFloat(20))!
let boldFont = UIFont(descriptor: normalFont.fontDescriptor.withSymbolicTraits(.traitBold)!, size: normalFont.pointSize)
Hope this helps! Cheers!
To get a bold font you need to pass a specific name of the font from a font family. You can get a font family name from a given font, then list all fonts from this family. In general, a bold font will contain "bold" in its name, but the format isn't strict and there could be variations like "Helvetica-BoldOblique", for example. You can start from this code:
- (UIFont *)boldFontFromFont:(UIFont *)font
{
NSString *familyName = [font familyName];
NSArray *fontNames = [UIFont fontNamesForFamilyName:familyName];
for (NSString *fontName in fontNames)
{
if ([fontName rangeOfString:#"bold" options:NSCaseInsensitiveSearch].location != NSNotFound)
{
UIFont *boldFont = [UIFont fontWithName:fontName size:font.pointSize];
return boldFont;
}
}
return nil;
}
This is a very old thread but someone may be interested in how to do this in Swift 5 nowadays.
Easy like this:
var font: UIFont = UIFont.systemFont(ofSize: 18)
if let newDescriptor = font.fontDescriptor.withSymbolicTraits(.traitBold) {
font = UIFont(descriptor: newDescriptor, size: font.pointSize)
}
Attempting to derive the bold/italic font using font or family names no longer works correctly since iOS 7, due to the cryptic font family name of the system font. Below is a simple extension to derive the bold/italic font using the UIFontDescriptor class.
+(UIFont *) font:(UIFont *)font bold:(BOOL)bold italic:(BOOL)italic
{
NSUInteger traits = 0;
if (bold)
{
traits |= UIFontDescriptorTraitBold;
}
if (italic)
{
traits |= UIFontDescriptorTraitItalic;
}
return [UIFont fontWithDescriptor:[[font fontDescriptor] fontDescriptorWithSymbolicTraits:traits] size:font.pointSize];
}
You can either use
[UIFont boldSystemFontOfSize:12].
If you are using custom fonts you have to use the name directly
[UIFont fontWithName:#"Helvetica-Bold" size:17.0].
You can look up the possible font names with
[UIFont fontNamesForFamilyName:#"American Typewriter"].
In this Post: https://stackoverflow.com/a/15388946/436818 Ben M has an idea how to get the bold version dynamically. But extend the method to be sure to get the bold version (if it exists) because there are other bold versions like CondensedBold too.
Since this question pops up when you search for bold UIFonts in Swift, here's a fresh answer:
extension UIFont {
/// Returns a new font in the same family with the given symbolic traits,
/// or `nil` if none found in the system.
func withSymbolicTraits(_ traits: UIFontDescriptor.SymbolicTraits) -> UIFont? {
guard let descriptorWithTraits = fontDescriptor.withSymbolicTraits(traits)
else { return nil }
return UIFont(descriptor: descriptorWithTraits, size: 0)
}
}
Example:
myFont.withSymbolicTraits(.taitBold) // returns the bold version of myFont
Nobody posted a solution which:
is for Swift
is an extension for UIFont
doesn't do force unwraps
uses the same point size as source font
and only does what is asked in the question (create bold UIFont based on existing UIFont)
so I'm doing it:
import UIKit
extension UIFont {
func boldFont() -> UIFont? {
guard let boldDescriptor = fontDescriptor.withSymbolicTraits(.traitBold) else {
return nil
}
return UIFont(descriptor: boldDescriptor, size: pointSize)
}
}
Feel free to copy-and-paste!

Interface builder is defaulting to Helvetica just now

I have been using a specific font which I've been testing on the iPad directly and it was working fine.
Now all of a sudden, the label or textview using the font are default back to Helvetica.
As a hack I'm setting the font manually, but this is ridiculous
self.taskNameView.font = [UIFont fontWithName:#"BradleyHandITCTT-Bold" size:24.0];
Any idea why all of a sudden, interface builder is freaking out?
I've tried cleaning but that didn't help.
I'm on XCode 4 and working on iOS 4.3
If you just want to override all fonts in your application, maybe a category on UIFont will do the trick?
Try adding this to e.g. your AppDelegate.m:
#implementation UIFont (FontOverride)
+(UIFont*) systemFontOfSize:(CGFloat)fontSize
{
return [UIFont fontWithName:#"radleyHandITCTT-Normal" size:fontSize];
}
+(UIFont*) boldSystemFontOfSize:(CGFloat)fontSize
{
return [UIFont fontWithName:#"radleyHandITCTT-Bold" size:fontSize];
}
+(UIFont*) italicSystemFontOfSize:(CGFloat)fontSize
{
return [UIFont fontWithName:#"radleyHandITCTT-Italic" size:fontSize];
}
#end

Resources