ios 全局改变字体样式,等比适配文字字体大小,y g j 等英文字母高度不够,被遮挡一部分

荒废了很久,终于想起来简书的存在,做了一下项目的语言本地化,却发现一个新的问题,在这记录一下。

最开始在项目中,使用的系统默认的字体样式,字体大小也是固定的,之后应lb的要求,全局添加新的字体样式,在不同的屏幕下等比适配字体的字号大小。

代码如下

#import "UIFont+YJExtension.h"
#import <objc/runtime.h>

#define SizeScale  1 //[UIScreen mainScreen].bounds.size.height/667

@implementation UIFont (YJExtension)
#pragma mark  更改全局的文字字体
+ (void)load {
  [super load];
  static dispatch_once_t onceToken;
  dispatch_once(&onceToken, ^{
      Method oldMethod = class_getClassMethod([self class], @selector(systemFontOfSize:));
      Method newMethod = class_getClassMethod([self class], @selector(yj_changeFontOfSize:));
      method_exchangeImplementations(oldMethod, newMethod);
  });
}

+ (UIFont *)yj_changeFontOfSize:(CGFloat)fontSize {
  UIFont *font = [UIFont fontWithName:@"SourceHanSansCN-Light" size:fontSize];
  if (!font)return [self yj_changeFontOfSize:fontSize];
  return font;
}

@end

@implementation UILabel (YJFont)

+ (void)load{
  //利用running time运行池的方法在程序启动的时候把两个方法替换 适用Xib建立的label
  Method imp = class_getInstanceMethod([self class], @selector(initWithCoder:));
  Method myImp = class_getInstanceMethod([self class], @selector(myInitWithCoder:));
  method_exchangeImplementations(imp, myImp);  //交换方法
}

- (id)myInitWithCoder:(NSCoder*)aDecode{
  [self myInitWithCoder:aDecode];
  if (self) {
      //部分不像改变字体的 把tag值设置成LabelFontSize值的跳过
      if(self.tag != 2017) {
          CGFloat fontSize = self.font.pointSize;
          self.font = [UIFont systemFontOfSize:fontSize*SizeScale];
      }
  }
  return self;
}
@end

@implementation UIButton (YJFont)

+ (void)load{
  
  //利用running time运行池的方法在程序启动的时候把两个方法替换 适用Xib建立的label
  Method imp = class_getInstanceMethod([self class], @selector(initWithCoder:));
  Method myImp = class_getInstanceMethod([self class], @selector(myInitWithCoder:));
  method_exchangeImplementations(imp, myImp);  //交换方法
}

- (id)myInitWithCoder:(NSCoder*)aDecode{
  [self myInitWithCoder:aDecode];
  if (self) {
      //部分不像改变字体的 把tag值设置成2016跳过
      //        if (iPhone6P) {
      if(self.tag != 2017) {
          CGFloat fontSize = self.titleLabel.font.pointSize;
          self.titleLabel.font = [UIFont systemFontOfSize:fontSize*SizeScale];
      }
      //        }
  }
  return self;
}

@end

@implementation UITextField (YJFont)

+ (void)load{
  //利用running time运行池的方法在程序启动的时候把两个方法替换 适用Xib建立的label
  Method imp = class_getInstanceMethod([self class], @selector(initWithCoder:));
  Method myImp = class_getInstanceMethod([self class], @selector(myInitWithCoder:));
  method_exchangeImplementations(imp, myImp);  //交换方法
}

- (id)myInitWithCoder:(NSCoder*)aDecode{
  [self myInitWithCoder:aDecode];
  if (self) {
      //部分不像改变字体的 把tag值设置成LabelFontSize值的跳过
      //        if (iPhone6P) {
      if(self.tag != 2017) {
          CGFloat fontSize = self.font.pointSize;
          self.font = [UIFont systemFontOfSize:fontSize*SizeScale];
      }
      //        }
  }
  return self;
}
@end

因为项目一直默认使用的简体中文,很长一段时间都没注意到这个问题,语言的本地化才发现在动态改变字体的样式之后,如果出现 y g j 等带有尾巴的英文字母就会出现高度不够,被遮挡一部分的问题,如果在项目中做自适应高度就会出现这个问题,如果你是计算的控件高度,还需要另外+2个尺寸,我只测了几种屏幕,可能不够完全。
如下图所示,
暂时还没有解决方案!如果有思路或者已经解决了的大佬,求指点。

159FCCCFBCB5674C23BFAE824BEE88D1.png

解决方法如下:
官方字体地址:https://github.com/adobe-fonts/source-han-sans/tree/release/OTF/SimplifiedChinese,使用正规的苹果认可的就可以

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 13,805评论 1 92
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,385评论 25 708
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,245评论 4 61
  • 你说我不懂浪漫情怀 其实我只是说不出来 你知道,我是个性格内向的女孩 风起的时候,你别走开 好吧为了你,我努力去改...
    暖树听风阅读 265评论 6 8
  • 懒,不仅仅是行动上的不作为,更是思想上的懒惰。照着一个思路不断重复行为的后果往往可怕的多。重新又捡起本职工作,要换...
    浅浅绘绘阅读 275评论 0 0