iOS 全局改變字號(二)

meng

1、上篇文章說到,給UIFont添加分類,實現全局改變字號,下面就來介紹給UILabel和UIButton添加分類,在init時候改變字號

 The first step:創建UILabel和UIButton的分類,如圖
UILabel的分類

UILabel+changeTextFont.h

#import <UIKit/UIKit.h>

@interface UILabel (changeTextFont)

@end

UILabel+changeTextFont.m

#import "UILabel+changeTextFont.h"

@implementation UILabel (changeTextFont)
// 監聽字號的改變
- (instancetype)initWithFrame:(CGRect)frame{
    if (self= [super initWithFrame:frame]) {       
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeTextSize:) name:@"changeTextSize" object:nil];
    }
    return self;
}

- (void)changeTextSize:(NSNotification *)noti {
    //11是文字的最小字號,可以根據需求修改此字號
    self.font = [UIFont systemFontOfSize:(11 * [[[NSUserDefaults standardUserDefaults] valueForKey:@"fontsize"] floatValue])];
}
@end

1.2 Button分類

button的分類

UIButton+ChangeTextFont.h

#import "UIButton+ChangeTextFont.h"
@implementation UIButton (ChangeTextFont)

// 監聽字號改變
- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeTextSize:) name:@"changeTextSize" object:nil];
    }
    return self;
}

// 收到通知后改變字號
- (void)changeTextSize:(NSNotification *)noti{
    //11是文字的最小字號,可以根據需求修改此字號
    self.titleLabel.font = [UIFont systemFontOfSize:[[NSUserDefaults standardUserDefaults] floatForKey:@"fontsize"]*11];
}

@end

2 、ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@end

ViewController.m

#import "ViewController.h"
#import "NextViewController.h"

// 定義文字大小的宏
#define FONT_SIZE [[[NSUserDefaults standardUserDefaults] objectForKey:@"fontsize"] floatValue] > 1 ?  [[[NSUserDefaults standardUserDefaults] valueForKey:@"fontsize"] floatValue] * 11 : 11

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"全局改變字號";
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, 300, 100)];
    label.backgroundColor = [UIColor redColor];
    label.text = @"這是測試文字";
    label.font = [UIFont systemFontOfSize:FONT_SIZE];
    [self.view addSubview:label];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
    [btn setBackgroundColor:[UIColor yellowColor]];
    btn.frame = CGRectMake(100, 200, 300, 100);
    [btn setTitle:@"這是一個按鈕" forState:UIControlStateNormal];
    [btn setTintColor:[UIColor blackColor]];
    btn.titleLabel.font = [UIFont systemFontOfSize:FONT_SIZE];
    [self.view addSubview:btn];
    
}

// 點擊屏幕任意位置,跳到設置界面
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {    
    [self.navigationController pushViewController:[[NextViewController alloc] init] animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
 ViewController界面的效果圖如下
ViewController界面效果

3、設置界面

NextViewController.h

#import <UIKit/UIKit.h>

@interface NextViewController : UIViewController

@end

NextViewController.m

#import "NextViewController.h"

@interface NextViewController ()

@end

@implementation NextViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    // 創建滑動開關
    UISlider *slide = [[UISlider alloc] initWithFrame:CGRectMake(40, 200, 200, 20)];
    slide.value = [[[NSUserDefaults standardUserDefaults] valueForKey:@"fontsize"] floatValue] - 1;
    slide.maximumValue = 1.0;
    slide.minimumValue = 0.0;
    [slide addTarget:self action:@selector(change:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:slide];
    
}

- (void)change:(UISlider *)slide {
    // 將slider的值存到本地
    [[NSUserDefaults standardUserDefaults] setValue:@(slide.value + 1) forKey:@"fontsize"];
    // 發送通知
    [[NSNotificationCenter defaultCenter] postNotificationName:@"changeTextSize" object:nil userInfo:@{@"size":@(slide.value + 1)}];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
 設置界面的效果如下
設置界面效果

此時,調節滾動條就可以實現全局改變字號了,但是會有一個問題,因為導航欄的title也是繼承與UILabel,所以此時對UILabel添加分類,勢必會造成調節字號的時候,導航條的字號也隨之改變,有些app可能不需要改變導航條的字號,那么下面就來解決這一問題

4、如果想在調節字號的時候導航條title的字號沒有改變的話,那么此時就不能對UILabel添加分類,我們可以創建一個類,繼承與UILabel,用這個類來創建label,具體代碼如下

    首先先來創建一個類 CustomLabel

CustomLabel.h

#import <UIKit/UIKit.h>

@interface CustomLabel : UILabel

@end

CustomLabel.m

#import "CustomLabel.h"

@implementation CustomLabel

- (instancetype)initWithFrame:(CGRect)frame{
    if (self= [super initWithFrame:frame]) {       
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeTextSize:) name:@"changeTextSize" object:nil];
    }
    return self;
}

- (void)changeTextSize:(NSNotification *)noti {
    //11是我默認設置的文字大小,你根據需求該這個數就行
    self.font = [UIFont systemFontOfSize:(11 * [[[NSUserDefaults standardUserDefaults] valueForKey:@"fontsize"] floatValue])];
}
@end

創建label的時候,用這個類創建,就可以避免在調節字號的時候同時改變導航條的字號了,關于另外一種做法,在上篇文章中已經介紹過

iOS 全局改變字號(一)http://www.lxweimin.com/p/824d3feeee02

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容