When building for iOS 7, my custom fonts are displayed in a UILabel correctly. However building with XCode 6 and for iOS 8 another "standard" font is displayed.
Code:
UILabel *label = [[UILabel alloc] initWithFrame:rect];
UIFont *font = [[CAPTheme sharedTheme] appTitleFont];
label.text = title;
label.textAlignment = NSTextAlignmentCenter;
label.textColor = [UIColor whiteColor];
label.backgroundColor = [UIColor clearColor];
label.alpha = kLabelAlphaConstant;
[label setFont:font];
...
- (UIFont *)appTitleFont
{
NSArray *familyNames = [UIFont familyNames];
for (NSString *aFamilyName in familyNames) {
NSArray *fontNames = [UIFont fontNamesForFamilyName:aFamilyName];
NSLog(@"familyname: %@", aFamilyName);
for (NSString *aFontName in fontNames) {
NSLog(@" %@", aFontName);
}
}
return [UIFont fontWithFamily:@"MyFont" size:15.0f];
}
...
+ (UIFont*)fontWithFamily:(NSString*)fontFamily size:(float)size
{
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) {
return [UIFont fontWithName:fontFamily size:size];
}
else {
UIFont *font = [UIFont fontWithName:fontFamily size:size];
UIFontDescriptor *des = [[font fontDescriptor] fontDescriptorByAddingAttributes:@{
UIFontDescriptorTextStyleAttribute: UIFontTextStyleHeadline,
UIFontDescriptorSizeAttribute: @(size)
}];
UIFont *finalFont = [UIFont fontWithDescriptor:des size:0.0];
return finalFont;
}
}
Let's call my font "MyFont". It is then displayed in the debug prints as:
familyname: MyFont
MyFont
MyFont-Bold
I have tried both to add it as ttf and otf file. It is added under Fonts provided by application
in plist file. It is also added to the target. I have also tried only using one of the fonts.
Interesting is also that I have code that adjust the size of the label based on the labels size. When entering a font family name that does not exists (i.e. "MyFont123") is will make the text not show at all, meaning it does recognizes that the custom font exists, but it wont show it.
Any ideas?
Here is a demo project I created to show the problem: https://www.dropbox.com/s/mkn7xb3fb2lmh20/customLabel.zip?dl=0 run it using iOS 8 and the font will not be shown.
Edit: My guess is that I cannot use fontDescriptors with custom fonts but then I'm back to my original problem, that I get a crash on iOS 8 with this error:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason:
'scaledValueForValue: called on a font that doesn't have a text style set'
Similar to this one scaledValueForValue: called on a font that doesn't have a text style set