问题描述
据我所知,iOS 7的默认字体是 helvetica neue Ultralight ,与其大胆的前身相比,它更薄.为了提供一致的设计并使我即将到来的应用在所有常见的iOS版本中看起来都一样,我想将Helvetica Neue Ultralight应用于该应用程序的默认字体(主要)字体.
很高兴,此"新字体"可用自ios版本5.0 以来,因此它已经得到了iOS 7之前的版本支持.可悲的是,我想出的唯一方法是在每个Uiview的字体上手动调用[UIFont fontWithName:@"HelveticaNeue-UltraLight" size:size],这是乏味且容易出错的不一致性.
所以我的问题是,您如何做到这一点或如何处理此设计更改?
推荐答案
这是Objective-C运行时解决方案:
@interface UIFont (CustomSystemFont) + (UIFont *)ln_systemFontOfSize:(CGFloat)fontSize; + (UIFont *)ln_boldSystemFontOfSize:(CGFloat)fontSize; + (UIFont *)ln_italicSystemFontOfSize:(CGFloat)fontSize; @end @implementation UIFont (CustomSystemFont) + (void)load { Method orig = class_getClassMethod([UIFont class], @selector(systemFontOfSize:)); Method swiz = class_getClassMethod([UIFont class], @selector(ln_systemFontOfSize:)); method_exchangeImplementations(orig, swiz); orig = class_getClassMethod([UIFont class], @selector(boldSystemFontOfSize:)); swiz = class_getClassMethod([UIFont class], @selector(ln_boldSystemFontOfSize:)); method_exchangeImplementations(orig, swiz); orig = class_getClassMethod([UIFont class], @selector(italicSystemFontOfSize:)); swiz = class_getClassMethod([UIFont class], @selector(ln_italicSystemFontOfSize:)); method_exchangeImplementations(orig, swiz); } + (UIFont *)ln_systemFontOfSize:(CGFloat)fontSize { if([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0f) { //Call original implementation. return [self ln_systemFontOfSize:fontSize]; } return [UIFont fontWithName:@"HelveticaNeue" size:fontSize]; } + (UIFont *)ln_boldSystemFontOfSize:(CGFloat)fontSize { if([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0f) { //Call original implementation. return [self ln_systemFontOfSize:fontSize]; } return [UIFont fontWithName:@"HelveticaNeue-Medium" size:fontSize]; } + (UIFont *)ln_italicSystemFontOfSize:(CGFloat)fontSize { if([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0f) { //Call original implementation. return [self ln_systemFontOfSize:fontSize]; } return [UIFont fontWithName:@"HelveticaNeue-Italic" size:fontSize]; } @end
我在此示例中要做的是用自己的三个系统字体方法替换三个系统字体方法并测试以查看系统版本是7还是UP.如果是的话,我使用原始方法,否则返回我选择的字体(在这种情况下,helvetica neue具有适用于常规和斜体请求的超轻重量,而大胆请求的中等重量).
这适用于代码中生成的所有内容,包括系统创建的视图.从XIB和情节板文件加载视图时,它不起作用,因为字体是在笔尖文件本身中进行硬编码的.使用字体选择器选择所需的字体.
其他推荐答案
为什么不使用外观API?
[[UILabel appearance] setFont:[UIFont fontWithName:@"YourFontName" size:14.0]];
其他推荐答案
没有通过特定的方法名称来破坏NDA - 为什么不声明Uifont上的类别以模仿动态类型的新方法?
问题描述
To my knowledge, the default font of iOS 7 is Helvetica Neue UltraLight, which is a lot thinner compared to its bold predecessor. To provide a consistent design and make my forthcoming apps look the same across all common iOS versions, I'd like to apply Helvetica Neue UltraLight as the default (primary) font of the app.
Gladly, this "new font" is available since iOS version 5.0, so it's already supported by versions prior to iOS 7. Sadly, the only way I figured out to use it, is to manually call [UIFont fontWithName:@"HelveticaNeue-UltraLight" size:size] on each UIView's font, which is tedious and error-prone to inconsistency.
So my question is, what is your way to do this or how do you handle this design change?
推荐答案
Here is the Objective-C Runtime solution:
@interface UIFont (CustomSystemFont) + (UIFont *)ln_systemFontOfSize:(CGFloat)fontSize; + (UIFont *)ln_boldSystemFontOfSize:(CGFloat)fontSize; + (UIFont *)ln_italicSystemFontOfSize:(CGFloat)fontSize; @end @implementation UIFont (CustomSystemFont) + (void)load { Method orig = class_getClassMethod([UIFont class], @selector(systemFontOfSize:)); Method swiz = class_getClassMethod([UIFont class], @selector(ln_systemFontOfSize:)); method_exchangeImplementations(orig, swiz); orig = class_getClassMethod([UIFont class], @selector(boldSystemFontOfSize:)); swiz = class_getClassMethod([UIFont class], @selector(ln_boldSystemFontOfSize:)); method_exchangeImplementations(orig, swiz); orig = class_getClassMethod([UIFont class], @selector(italicSystemFontOfSize:)); swiz = class_getClassMethod([UIFont class], @selector(ln_italicSystemFontOfSize:)); method_exchangeImplementations(orig, swiz); } + (UIFont *)ln_systemFontOfSize:(CGFloat)fontSize { if([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0f) { //Call original implementation. return [self ln_systemFontOfSize:fontSize]; } return [UIFont fontWithName:@"HelveticaNeue" size:fontSize]; } + (UIFont *)ln_boldSystemFontOfSize:(CGFloat)fontSize { if([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0f) { //Call original implementation. return [self ln_systemFontOfSize:fontSize]; } return [UIFont fontWithName:@"HelveticaNeue-Medium" size:fontSize]; } + (UIFont *)ln_italicSystemFontOfSize:(CGFloat)fontSize { if([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0f) { //Call original implementation. return [self ln_systemFontOfSize:fontSize]; } return [UIFont fontWithName:@"HelveticaNeue-Italic" size:fontSize]; } @end
What I do in this example is replace the three system font methods with my own and test to see if the system version is 7 or up. If it is, I use the original methods, otherwise return a font of my choosing (in this case Helvetica Neue with UltraLight weight for regular and italic requests, and Medium weight for bold requests).
This works for everything generated in code, including system created views. It does not work when loading views from Xib and Storyboard files, because the fonts are hardcoded in the NIB file itself. Use the font picker to choose the font you need.
其他推荐答案
Why not just use the appearance API?
[[UILabel appearance] setFont:[UIFont fontWithName:@"YourFontName" size:14.0]];
其他推荐答案
Without breaking NDA by being specific about the method names - why not declare a category on UIFont to mimic the new methods for dynamic type?