-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
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
added h file