Created
January 18, 2014 20:48
-
-
Save priore/8496194 to your computer and use it in GitHub Desktop.
UILabel text size fit macro
This file contains hidden or 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
| // UILabel text size fit macro | |
| // | |
| // use : | |
| // | |
| // UILabel *label = .... | |
| // TEXT_FIT(label, @"your text here"); | |
| // | |
| #define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending) | |
| #define TEXT_FIT_IOS6(t,txt) (CGRect){t.frame.origin, t.frame.size.width, [txt sizeWithFont:t.font constrainedToSize:t.frame.size lineBreakMode:t.lineBreakMode].height} | |
| #define TEXT_FIT_IOS7(t,txt) (CGRect){t.frame.origin, t.frame.size.width, [txt boundingRectWithSize:t.frame.size options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:t.font} context:nil].size.height} | |
| #define TEXT_FIT(t,txt) {t.text = txt;t.numberOfLines = 0;t.frame = SYSTEM_VERSION_LESS_THAN(@"7.0")?TEXT_FIT_IOS6(t,txt):TEXT_FIT_IOS7(t,txt);} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment