我正在开发需要更改所有控件的动态字体大小的应用程序。
如您在屏幕截图中所见,我必须根据字体大小的百分比更改字体。
例如
对于100%
lbl.font=[UIFont systemFontOfSize:20.0f];
lbl1.font=[UIFont systemFontOfSize:30.f];
对于80%
lbl.font=[UIFont systemFontOfSize:16.0f];
lbl1.font=[UIFont systemFontOfSize:24.f];
对于50%
lbl.font=[UIFont systemFontOfSize:10.0f];
lbl1.font=[UIFont systemFontOfSize:15.f];
最好的方法是什么?
回答我自己的问题。我不确定这是最好的方法。
枚举对于不同的字体大小
typedef enum {
UIFontCategoryExtraSmall,
UIFontCategorySmall,
UIFontCategoryMedium,
UIFontCategoryLarge,
UIFontCategoryExtraLarge
} UIFontCategory;
#define FONT_SIZE_KEY @"fontsize"
UIFont的类别类
@interface UIFont (CustomFonSize)
+(UIFont *)preferredFontSizeWithMaxFontSize:(CGFloat )fontSize;
@end
@implementation UIFont (AvenirContentSize)
+ (UIFont *)preferredFontSizeWithMaxFontSize:(CGFloat )fontSize; {
// choose the font size
NSInteger currentFontSize=[[NSUserDefaults standardUserDefaults]integerForKey:FONT_SIZE_KEY];
if (currentFontSize==UIFontCategoryExtraSmall) {
fontSize = fontSize-8;
} else if (currentFontSize==UIFontCategorySmall) {
fontSize = fontSize-6;
} else if (currentFontSize==UIFontCategoryMedium) {
fontSize = fontSize-4;
} else if (currentFontSize==UIFontCategoryLarge) {
fontSize = fontSize-2;
} else if (currentFontSize==UIFontCategoryExtraLarge) {
fontSize = fontSize;
}
return [UIFont systemFontOfSize:fontSize];
}
自定义字体类的使用
lbl.font=[UIFont preferredFontSizeWithMaxFontSize:16.0f];
lbl1.font=[UIFont preferredFontSizeWithMaxFontSize:24.f];
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句