iOS开发:UILabel字号根据屏幕缩放
admin
2023-02-09 11:00:11
0

场景:

假设我们有这样一个需求,iPhone 6(屏幕宽度为375pt)上的设计图上的字号为17pt,iPhone 6 Plus上的字号根据屏幕宽度缩放,即字号为(17pt x 414pt / 375pt)= 18.768pt

解决方案:

如果一个一个设置太麻烦,容易遗漏,这时候我们采用 runtime 的替换方法来实现,如果嫌替换方法太麻烦,我们可以用第三方库 Aspects 来辅助我们解决。

步骤:

  1. 添加pod

    pod 'Aspects', '~> 1.4.1'
  2. 新建UILabel Category,命名为UILabel+AspectsScaling
    以下为文件内容
    UILabel+AspectsScaling.h 文件
#import 
NS_ASSUME_NONNULL_BEGIN
@interface UILabel (AspectsScaling)
@end
NS_ASSUME_NONNULL_END

UILabel+AspectsScaling.m 文件

#import "UILabel+AspectsScaling.h"
#import "Aspects.h"
@implementation UILabel (AspectsScaling)
+ (void)load {
  NSError * error = nil;
  [self aspect_hookSelector:@selector(initWithCoder:) withOptions:AspectPositionAfter usingBlock:^(id info, NSCoder * coder) {
    [info.instance scaleFont];
  } error:&error];
  [self aspect_hookSelector:@selector(initWithFrame:) withOptions:AspectPositionAfter usingBlock:^(id info, CGRect frame) {
    [info.instance scaleFont];
  } error:&error];
  //以下是log方法,可以不要
#if DEBUG
  [self aspect_hookSelector:@selector(scaleFont) withOptions:AspectPositionBefore usingBlock:^(id info) {
    UILabel * label = info.instance;
    NSLog(@"UILabel: Before Scaling font size: %f", label.font.pointSize);
  } error:&error];
  [self aspect_hookSelector:@selector(scaleFont) withOptions:AspectPositionAfter usingBlock:^(id info) {
    UILabel * label = info.instance;
    NSLog(@"UILabel: After Scaling font size: %f", label.font.pointSize);
  } error:&error];
#endif
}
- (void)scaleFont {
  CGFloat ratio = CGRectGetWidth(UIScreen.mainScreen.bounds) / (CGFloat)375;
  self.font = [UIFont fontWithDescriptor:self.font.fontDescriptor size:self.font.pointSize * ratio];
}
@end

解释:

  1. 显然,这是缩放字体的方法
    - (void)scaleFont;
  2. 这个方法是在原来的initWithCoder: 方法后面执行一个 block ,这是 Aspects 库的方法,利用的是 runtime,可以自行了解源码
    [self aspect_hookSelector:@selector(initWithCoder:) withOptions:AspectPositionAfter usingBlock:^(id info, NSCoder * coder)...
  3. 再看 log 方法,这个 log 方法利用 Aspects ,在替换字体前后 NSLog 字体的字号,这个区别在参数 AspectPositionBefore 和 AspectPositionAfter
    [self aspect_hookSelector:@selector(scaleFont) withOptions:AspectPositionBefore usingBlock:^(id info) ...
    [self aspect_hookSelector:@selector(scaleFont) withOptions:AspectPositionAfter usingBlock:^(id info) ...
  4. 我们看看 Aspects 的Aspects.h文件:
    里面提供两个方法,
    • 一个是类方法(修改类的所有实例的方法),
    • 一个是实例方法(修改单个实例的方法),
    • 返回值是一个id可以保存以后取消修改,
    • usingBlock:(id)block 里面的类型id一般情况下可以写成^(id info, ...) ...是要修改的方法的所有参数,如@selector(initWithFrame:) ,block 类型^(id info, CGRect frame)
...
typedef NS_OPTIONS(NSUInteger, AspectOptions) {
    AspectPositionAfter   = 0,            /// Called after the original implementation (default)
    AspectPositionInstead = 1,            /// Will replace the original implementation.
    AspectPositionBefore  = 2,            /// Called before the original implementation.

    AspectOptionAutomaticRemoval = 1 << 3 /// Will remove the hook after the first execution.
};
...
+ (id)aspect_hookSelector:(SEL)selector
                      withOptions:(AspectOptions)options
                       usingBlock:(id)block
                            error:(NSError **)error;

/// Adds a block of code before/instead/after the current `selector` for a specific instance.
- (id)aspect_hookSelector:(SEL)selector
                      withOptions:(AspectOptions)options
                       usingBlock:(id)block
                            error:(NSError **)error;
...

总结

Aspects 是 iOS Aspect-oriented programming (AOP) 的一种实现,
满足以下几点就可以使用(但不是必须满足才能使用)

  • 原来要有实例方法实现
  • 频繁调用,一个一个修改太麻烦
  • 在原来的实例方法的前面和后面可以插入代码完成需求
  • 最最常用的是log,以后可以一步注释
[UIViewController aspect_hookSelector:@selector(viewWillAppear:) withOptions:AspectPositionAfter usingBlock:^(id aspectInfo, BOOL animated) {
    NSLog(@"View Controller %@ will appear animated: %tu", aspectInfo.instance, animated);
} error:NULL];

Aspects 不是万能的,GitHub项目主页有Compatibility and Limitations ,一种常见的问题是当拦截一个方法的时候,它会把相关类当作已拦截,就会报错(A method can only be hooked once per class hierarchy ),所以当方法名相同时要考虑其他方法,这个 Aspects 库无法满足需求

相关内容

热门资讯

共促数智交融 共启丝路新篇—... 本报记者 亓玉昆7月22日,2026年世界互联网大会数字丝路发展论坛在陕西西安举行。论坛以“智汇丝路...
赛里木湖景区通报“工作人员殴打... 2026年7月23日17时许,赛里木湖景区入口处发生多名景区工作人员殴打旅游车司机事件,我们深感痛心...
继英伟达黄仁勋后,AMD苏姿丰... IT之家 7 月 24 日消息,随着中国开源 AI 模型的不断变强,近期美国业界出现了关于 AI 是...
辰奕智能获得实用新型专利授权:... 证券之星消息,根据天眼查APP数据显示辰奕智能(301578)新获得一项实用新型专利授权,专利名为“...
湖南湘江新区文化创意产业出海观... 中新网长沙7月23日电(胡语桐)在日本大型量贩门店的货架上,中国研发的视频创意软件实体产品陈列其中;...
中国数学家首次获得的菲尔兹奖“... 当地时间7月23日上午,2026年国际数学家大会在美国费城开幕,现场揭晓2026年菲尔兹奖得主。中国...
猪周期反转,这次可能来真的 市场过去几年关于猪周期的很多认知,现在需要重新审视了。过去几年,越来越多人相信:随着规模化养殖企业不...
王虹、邓煜为啥能获菲尔兹奖? 当地时间7月23日早晨,2026年国际数学家大会在美国费城开幕。国际数学联盟在开幕式上正式公布第二十...
高温津贴,不能用绿豆汤冲抵 近日,深圳市人力资源和社会保障局发布关于高温津贴的典型劳动仲裁案例,并普法提醒:即便劳动合同约定日薪...
菲律宾,你到底想干什么 中国和菲律宾关系,又出幺蛾子了。反正,最近几天,麻烦不断,场面火爆。仁爱礁,菲两艘小艇冲撞我巡逻船只...