Skip to content

Instantly share code, notes, and snippets.

@Daij-Djan
Forked from anonymous/UIFont+Traits.mm
Last active May 14, 2016 07:00
Show Gist options
  • Save Daij-Djan/5046612 to your computer and use it in GitHub Desktop.
Save Daij-Djan/5046612 to your computer and use it in GitHub Desktop.
Easy UIFont Traits Querying (isBold/ isItalic) Found this hidden gem and made it a gist: http://joshua.nozzi.name/2012/08/easy-uifont-bold-and-italic-querying-with/ - and copyng of fonts to add specific traits
#import <UIKit/UIKit.h>
#import <CoreText/CTFont.h>
@interface UIFont (Traits)
@property(nonatomic, readonly) CTFontRef CTFontRef;
@property(nonatomic, readonly) CTFontSymbolicTraits traits;
@property(nonatomic, readonly, getter=isBold) BOOL bold;
@property(nonatomic, readonly, getter=isItalic) BOOL italic;
-(UIFont*)fontWithSymbolicTraitsValues:(CTFontSymbolicTraits)values
symbolicTraitsMask:(CTFontSymbolicTraits)mask;
-(UIFont*)fontWithSize:(CGFloat)pointSize
symbolicTraitsValues:(CTFontSymbolicTraits)Values
symbolicTraitsMask:(CTFontSymbolicTraits)mask;
@end
#import "UIFont+Traits.h"
@implementation UIFont (Traits)
- (CTFontRef)CTFontRef {
#if !__has_feature(objc_arc)
return (CTFontRef)self;
#else
return (__bridge CTFontRef)self;
#endif
}
#pragma mark -
- (CTFontSymbolicTraits)traits
{
CTFontSymbolicTraits symbolicTraits = CTFontGetSymbolicTraits(self.CTFontRef);
return symbolicTraits;
}
- (BOOL)isBold
{
CTFontSymbolicTraits symbolicTraits = [self traits];
return (symbolicTraits & kCTFontBoldTrait);
}
- (BOOL)isItalic
{
CTFontSymbolicTraits symbolicTraits = [self traits];
return (symbolicTraits & kCTFontItalicTrait);
}
#pragma mark -
-(UIFont*)fontWithSymbolicTraitsValues:(CTFontSymbolicTraits)Values symbolicTraitsMask:(CTFontSymbolicTraits)mask {
return [self fontWithSize:self.pointSize symbolicTraitsValues:Values symbolicTraitsMask:mask];
}
-(UIFont*)fontWithSize:(CGFloat)pointSize symbolicTraitsValues:(CTFontSymbolicTraits)Values symbolicTraitsMask:(CTFontSymbolicTraits)mask {
UIFont *newFont = (UIFont*)CTFontCreateCopyWithSymbolicTraits(self.CTFontRef, pointSize, NULL, Values, mask);
#if !__has_feature(objc_arc)
return [newFont autorelease];
#else
return newFont;
#endif
}
@end
@Daij-Djan
Copy link
Author

added a method to copy a font with specific traits

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment