How to get the font-size of a UIFont instance?
Or, if someone can implement this method for UIFont?
- (UIFont *)boldFont;
This is an old answer and it's no longer the best solution, please see the accepted answer instead.
-(UIFont *)boldFont{
//First get the name of the font (unnecessary, but used for clarity)
NSString *fontName = self.fontName;
//Some fonts having -Regular on their names. so we have to remove that before append -Bold / -BoldMT
fontName = [[fontName componentsSeparatedByString:@"-"] firstObject];
//Then append "-Bold" to it.
NSString *boldFontName = [fontName stringByAppendingString:@"-Bold"];
//Then see if it returns a valid font
UIFont *boldFont = [UIFont fontWithName:boldFontName size:self.pointSize];
//If it's valid, return it
if(boldFont) return boldFont;
//Seems like in some cases, you have to append "-BoldMT"
boldFontName = [fontName stringByAppendingString:@"-BoldMT"];
boldFont = [UIFont fontWithName:boldFontName size:self.pointSize];
//Here you can check if it was successful, if it wasn't, then you can throw an exception or something.
return boldFont;
}
-BoldMT
won't work for a font such as ArialMT
; its bold equivalent is Arial-BoldMT
, not ArialMT-BoldMT
.
Jul 4, 2011 at 0:03
To access the font size from your UIFont instance use
@property(nonatomic, readonly) CGFloat pointSize
There are more property to tell the font size (for cap, small etc...)
capHeight,xHeight,pointSize
Use below to access the bold font
UIFont* myboldFont = [UIFont boldSystemFontOfSize:11];
boldFont:
method is completely useless; it's just a wrapper for boldSystemFontOfSize:
. It seems obvious that @iwill was asking how to convert an existing UIFont to its bold counterpart.
Jul 1, 2011 at 6:04
UIFontDescriptor
is pretty powerful class and can be used to get bold version of the font you want. It should be totally safe and future proof as it only requires usage of public API and doesn't depend on any string modifications.
Here is the code in Swift 3:
extension UIFont {
func bold() -> UIFont? {
let fontDescriptorSymbolicTraits: UIFontDescriptorSymbolicTraits = [fontDescriptor.symbolicTraits, .traitBold]
let bondFontDescriptor = fontDescriptor.withSymbolicTraits(fontDescriptorSymbolicTraits)
return bondFontDescriptor.flatMap { UIFont(descriptor: $0, size: pointSize) }
}
}
Here is the code in objective-c:
@implementation UIFont (RABoldFont)
- (UIFont *)ra_boldFont
{
UIFontDescriptor *fontDescriptor = [self fontDescriptor];
UIFontDescriptorSymbolicTraits traits = fontDescriptor.symbolicTraits;
traits = traits | UIFontDescriptorTraitBold;
UIFontDescriptor *boldFontDescriptor = [fontDescriptor fontDescriptorWithSymbolicTraits:traits];
return [UIFont fontWithDescriptor:boldFontDescriptor size:self.pointSize];
}
@end
[fontDescriptor.symbolicTraits, .traitBold]
equivalent to fontDescriptor.symbolicTraits.union(.traitBold)
?
Jul 13, 2018 at 10:49
Here is the easiest way to find font size
UIFont *anyFont;
//initialization and other stuff here
Now at some point you want to find its size
CGFloat fontSize= [anyFont pointSize];
Use the NSString+UIKit methods to measure the size a string with a certain font.
The iPhone treats the normal and bold fonts of the same name as completely separate fonts, and as such there is no simple way to convert between them. For instance, ArialMT and Arial-BoldMT are considered completely different fonts by the OS.
EDIT: I may have misunderstood your question. Perhaps you are looking for the pointSize property of UIFont?
Swift 3 version of rafał-augustyniak answer
extension UIFont {
func boldFont() -> UIFont? {
let fontDescriptor = self.fontDescriptor
let fontDescriptorSymbolicTraits: UIFontDescriptorSymbolicTraits = [ fontDescriptor.symbolicTraits, .traitBold ]
guard let boldFontDesc = fontDescriptor.withSymbolicTraits(fontDescriptorSymbolicTraits) else {
return nil
}
return UIFont(descriptor: boldFontDesc, size: pointSize)
}
}
pointSize
ofUIFont
.pointSize
will be equal to the value that you provide to thesize:
parameter of the UIFont constructor. If you read the documentation for UIFont you will see that the fontSize/fontOfSize methods take or return values in points.