-3

应用程序可以选择由用户动态更改字体大小,选项如 14、17、20、23。

当用户动态更改字体大小时,我们需要根据新的视图高度更改所有视图位置。这是正确的方法吗?

4

2 回答 2

2

您不能一次更改所有字体....

但我发现了另一种对我很有效的变种,也可以帮助你......

我已经做了一些递归函数,你可以帮助你..

按照以下步骤..

首先创建一个从 UIViewController 扩展的类(BaseViewController),就像在 BaseViewController.h 文件中一样

@interface BaseViewController : UIViewController

并在 BaseViewController.m 文件中编写以下代码。

-(void)changeFontsOfViewController:(CGFloat)size
{
    UIViewController * vv = [self viewControllerOfView:self.view];
    NSArray *objects = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([vv class]) owner:vv options:nil];

    for (id object in objects)
    {
        [self changeFontOfView:object withSize:size];
    }

}

-(void)changeFontOfView:(UIView *)aView withSize:(CGFloat)size
{
    for (UIView *vv in [aView subviews])
    {

        if ([vv isKindOfClass:[UIButton class]])
        {
            UIButton *btn = (UIButton *)vv;
            btn.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:fontSize];
        }
        else if ([vv isKindOfClass:[UILabel class]])
        {
            UILabel *lbl = (UILabel *)vv;
            [lbl setFont:[UIFont fontWithName:@"Helvetica-Bold" size:fontSize]];
        }
        else if ([vv isKindOfClass:[UITextView class]])
        {
            UITextView *txt = (UITextView *)vv;
            [txt setFont:[UIFont fontWithName:@"Helvetica-Bold" size:fontSize]];
        }
        else if ([vv isKindOfClass:[UITextField class]])
        {
            UITextField *txt = (UITextField *)vv;
            [txt setFont:[UIFont fontWithName:@"Helvetica-Bold" size:fontSize]];
        }
        else if ([vv isKindOfClass:[UIView class]]||[vv isKindOfClass:[UIScrollView class]])
        {
            if (aView.subviews.count == 0)return;
            [self changeFontOfView:vv  withSize:size];
        }
    }

}

现在,您的每个 viewController(RootViewController) 都将从 BaseViewController 类扩展,就像在 RootViewController.h 中一样。

#import "BaseViewController.h"

@interface RootViewController : BaseViewController
{

}

当您想更改字体大小时,请调用changeFontOfView:withSize:...

[self changeFontsOfViewController:14];

请仔细按照上面的步骤,你会摇滚.......

于 2013-06-27T14:13:51.367 回答
0

I haven't done this, but this is how I would do it:

Make sure you have a class which is responsible for handling all your data which you can access from everywhere within your project (Usually my dataControllers are referenced from my AppDelegate).

Then you create a property that contains the different switchable font sizes (Tipp: Create a C typedef enum if you want to call your fontsizes by name rather than numbers).

When you're doing this you may want to create more than 1 property because you probably have headers and subtitles and not everything should have the same fontsize right?

Now you want every related UIControl to be changed when this value is changed right? If you want to do so, I would highly suggest using the key-value-observer as it will do all the work for you after you changed the value.

于 2013-06-27T14:29:17.210 回答