如何增加整个应用程序的字体大小?

4

我正在开发一款应用程序,需要动态调整所有控件的字体大小。

enter image description here

如您在截图中所见,我必须根据字体大小的百分比来更改字体。

例如:

对于100%

lbl.font=[UIFont systemFontOfSize:20.0f];
lbl1.font=[UIFont systemFontOfSize:30.f];

For 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];

什么是最好的方法?

你应该将100%字体的值存储到任何存储器中,然后重新计算它。这是最好的方法。 - Akshit Zaveri
我知道这一点,但对于所有的控件来说,应该有一种简单的方法。 - Sunny Shah
可能是为整个iOS应用设置默认字体?的重复问题。 - rmaddy
2个回答

2
你可以使用UIAppearance教程链接 并且看看这个
Here's a list of classes that support this feature, in one way or the other.

UIActivityIndicatorView
UIBarButtonItem
UIBarItem
UINavigationBar
UIPopoverController
UIProgressView
UISearchBar
UISegmentedControl
UISlider
UISwitch
UITabBar
UITabBarItem
UIToolbar
UIView
UIViewController

示例

UIActivityIndicatorView:

[[UIActivityIndicatorView appearance] setColor:[UIColor orangeColor]];

UINavigationBar:

[[UINavigationBar appearance] setTintColor:[UIColor brownColor]];
[[UINavigationBar appearanceWhenContainedIn:[MyCustomView class], nil] setTintColor:[UIColor blackColor]];

UIBarButtonItem:

[[UIBarButtonItem appearance] setTintColor:[UIColor redColor]];
[[UIBarButtonItem appearanceWhenContainedIn:[MyCustomView class], nil] setTintColor:[UIColor magentaColor]];

UIProgressView:

[[UIProgressView appearance] setProgressTintColor:[UIColor yellowColor]];
[[UIProgressView appearance] setTrackTintColor:[UIColor greenColor]];

UISegmentedControl:

UIImage *segmentSelected = [[UIImage imageNamed:@"Segment_Selected.png"]
                        resizableImageWithCapInsets:UIEdgeInsetsMake(0, 12, 0, 12)];
UIImage *segmentUnselected = [[UIImage imageNamed:@"Segment_Unselected.png"]
                          resizableImageWithCapInsets:UIEdgeInsetsMake(0, 12, 0, 12)];

[[UISegmentedControl appearance] setBackgroundImage:segmentUnselected
                                               forState:UIControlStateNormal
                                             barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearance] setBackgroundImage:segmentSelected
                                               forState:UIControlStateSelected
                                             barMetrics:UIBarMetricsDefault];

[[UISegmentedControl appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                        [UIColor magentaColor],UITextAttributeTextColor,
                                        [UIColor clearColor], UITextAttributeTextShadowColor,
                                        [NSValue valueWithUIOffset:UIOffsetMake(0, 0)], UITextAttributeTextShadowOffset,
                                        [UIFont fontWithName:@"Courier-Oblique" size:16.0], UITextAttributeFont, nil] forState:UIControlStateNormal];

[[UISegmentedControl appearance] setDividerImage:[UIImage imageNamed:@"SegmentedControl_Divider.png"]
                                 forLeftSegmentState:UIControlStateNormal
                                   rightSegmentState:UIControlStateNormal
                                          barMetrics:UIBarMetricsDefault];

滑动条:

[[UISlider appearance] setMinimumTrackImage:[UIImage imageNamed:@"Slider_Background.png"]
                                       forState:UIControlStateNormal];
[[UISlider appearance] setMaximumTrackImage:[UIImage imageNamed:@"Slider_Background.png"]
                                       forState:UIControlStateNormal];
[[UISlider appearance] setThumbImage:[UIImage imageNamed:@"Slider_Thumb.png"]
                                forState:UIControlStateNormal];

UISwitch:

[[UISwitch appearance] setOnTintColor:[UIColor redColor]];

UITabBar:

[[UITabBar appearance] setTintColor:[UIColor brownColor]];
[[UITabBar appearance] setSelectedImageTintColor:[UIColor greenColor]];

UIToolBar:

[[UIToolbar appearance] setTintColor:[UIColor blueColor]];

UISearchBar:

[[UISearchBar appearance] setImage:[UIImage imageNamed:@"Search_Icon.png"]
                      forSearchBarIcon:UISearchBarIconSearch
                                 state:UIControlStateNormal];
[[UISearchBar appearance] setImage:[UIImage imageNamed:@"Search_Cross.png"]
                      forSearchBarIcon:UISearchBarIconClear
                                 state:UIControlStateNormal];
UIImage *searchBg = [UIImage imageNamed:@"Search_Background.png"];
searchBg = [searchBg stretchableImageWithLeftCapWidth:10 topCapHeight:10];
[[UISearchBar appearance] setBackgroundImage:searchBg];

编辑:

这取决于你。假设您应该将百分比的大小存储到NSUSerDefaults或其他存储中。然后,通过使用上面的代码,您可以根据存储的百分比计算大小。明白了吗?


如果我使用外观,则所有标签的字体大小将相同。 - Sunny Shah
然后使用多个标签(子类化)并使用UIAppearance - Akshit Zaveri

0
回答我自己的问题。我不确定这是否是最好的方法。
不同字体大小的枚举。
  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];

减法并不是理想的方式。如果你想真正按百分比调整字体大小,那么你应该使用0.8*fontSize表示80%,0.6*fontSize表示60%等等。[注:0.8 = 80%]。我希望你明白我的解释。 - Akshit Zaveri
我知道,但是使用UIFontCategoryExtraSmall和0.2会变得太小了。所以我使用了减法。 - Sunny Shah
这取决于你的应用程序用户。无论如何,很高兴你找到了解决方案。不要忘记在2天后标记你的答案。 - Akshit Zaveri

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接