美文网首页iOS学习
iOS 全局改变字号(二)

iOS 全局改变字号(二)

作者: fulen | 来源:发表于2016-10-14 11:23 被阅读675次
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.jianshu.com/p/824d3feeee02

相关文章

网友评论

    本文标题:iOS 全局改变字号(二)

    本文链接:https://www.haomeiwen.com/subject/ukfgyttx.html