【问题标题】:Xcode: How to change all fonts at once in an app?Xcode:如何在应用程序中一次更改所有字体?
【发布时间】:2013-06-11 15:02:18
【问题描述】:

我想知道是否可以同时更改大约 100 个不同视图控制器的字体?这比一个一个地修改它们要简单得多。有任何想法吗?谢谢!

【问题讨论】:

  • 使用sed批量修改源代码。

标签: iphone objective-c xcode4


【解决方案1】:

用户界面文件 (*.xib) 是纯文本,您可以将它们加载到编辑器中。

在 Xcode4 的左侧窗格中,您可以右键单击 > 打开为 > 源代码。

这将为您提供您可以在那里找到/替换的任何 XML 源。

警告:做错事可能会导致整个文件无用,所以除非你有源代码控制,否则在尝试更改之前制作 XIB 的副本。

【讨论】:

  • 你解决了我的问题。有超过 20 多个视图......并且替换将它们全部修复。 #神模式
【解决方案2】:

你不能一次改变所有的字体....

但我找到了另一种可以帮助你的变种......

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

按照以下步骤..

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

@interface BaseViewController : UIViewController

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

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self changeFontsOfViewController];
}

-(void)changeFontsOfViewController
{
    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];
    }

}

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

        if ([vv isKindOfClass:[UIButton class]])
        {
            UIButton *btn = (UIButton *)vv;
            CGFloat fontSize = btn.titleLabel.font.pointSize;
            btn.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:fontSize];
        }
        else if ([vv isKindOfClass:[UILabel class]])
        {
            UILabel *lbl = (UILabel *)vv;
            CGFloat fontSize = lbl.font.pointSize;
            [lbl setFont:[UIFont fontWithName:@"Helvetica-Bold" size:fontSize]];
        }
        else if ([vv isKindOfClass:[UITextView class]])
        {
            UITextView *txt = (UITextView *)vv;
            CGFloat fontSize = txt.font.pointSize;
            [txt setFont:[UIFont fontWithName:@"Helvetica-Bold" size:fontSize]];
        }
        else if ([vv isKindOfClass:[UITextField class]])
        {
            UITextField *txt = (UITextField *)vv;
            CGFloat fontSize = txt.font.pointSize;
            [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];
        }
    }

}

现在您的每个 viewController(RootViewController) 都将从 BaseViewController 类扩展,如 RootViewController.h..

#import "BaseViewController.h"

@interface RootViewController : BaseViewController
{

}

并确保您在UIViewController(RootViewController.m) 的.m 文件中写了以下内容

- (void)viewDidLoad
{
    [super viewDidLoad];
}

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

【讨论】:

  • 请参阅我的答案以获得更简单的递归方法。
  • 谢谢 我已将它集成到我的代码中,但它如何与自动布局一起使用?请看我的问题stackoverflow.com/questions/25638837/…
【解决方案3】:

UIView 上的这个小递归方法怎么样?

@implementation UIView (JPCSetFont)

- (void)jpc_setAllFonts:(UIFont *)font
{
    if ([self respondsToSelector:@selector(setFont:)]) {
        UIFont *oldFont = [self valueForKey:@"font"];
        UIFont *newFont = [font fontWithSize:oldFont.pointSize];
        [self setValue:newFont forKey:@"font"];
    }

    for (UIView *subview in self.subviews) {
        [subview jpc_setAllFonts:font];
    }
}

@end

【讨论】:

    【解决方案4】:

    John Cromartie 的代码是个好东西。简单而简洁。

    请记住,有些字体是粗体斜体。因此,您可能必须传递 2 个(或 3 个)参数:常规字体(现在这样)、粗体字体和斜体字体。此外,您必须检测当前字体是否为粗体等。所以下面有一些增强的代码。

    但是,它在 iOS7 上可能会出错,因为用户可以设置自己的字体,因此重量/样式检测将无法正常工作。

    @implementation UIView (JPCSetFont)
    
    - (void)jpc_setAllFonts:(UIFont*)regular bold:(UIFont*)bold
    {
        if ([self respondsToSelector:@selector(setFont:)]) {
            UIFont *oldFont = [self valueForKey:@"font"];
    
            UIFont *newFont;
            // for iOS6
            NSRange isBold = [[oldFont fontName] rangeOfString:@"Bold" options:NSCaseInsensitiveSearch];
            // for iOS7 (is device owner didn't change it!)
            NSRange isMedium = [[oldFont fontName] rangeOfString:@"MediumP4" options:NSCaseInsensitiveSearch];
            if (isBold.location==NSNotFound && isMedium.location==NSNotFound) {
                newFont = [regular fontWithSize:oldFont.pointSize];
            } else {
                newFont = [bold fontWithSize:oldFont.pointSize];
            }
    
            // TODO: there are italic fonts also though
    
            [self setValue:newFont forKey:@"font"];
        }
    
        for (UIView *subview in self.subviews) {
            [subview jpc_setAllFonts:regular bold:bold];
        }
    }
    
    @end
    

    https://github.com/iutinvg/ZZLib/blob/master/ZZLib/UIView%2BZZFontSetter.h https://github.com/iutinvg/ZZLib/blob/master/ZZLib/UIView%2BZZFontSetter.m

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-09
      • 1970-01-01
      • 1970-01-01
      • 2015-09-22
      • 2018-02-06
      • 2015-12-19
      • 2017-07-23
      • 2023-03-18
      相关资源
      最近更新 更多