美文网首页
ios 全局改变字体样式,等比适配文字字体大小,y g j 等英

ios 全局改变字体样式,等比适配文字字体大小,y g j 等英

作者: child_cool | 来源:发表于2017-10-13 15:50 被阅读148次

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

最开始在项目中,使用的系统默认的字体样式,字体大小也是固定的,之后应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,使用正规的苹果认可的就可以

相关文章

网友评论

      本文标题:ios 全局改变字体样式,等比适配文字字体大小,y g j 等英

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