1堤器、改變代碼中通過接口[UIFont systemFontOfSize:]击费、[UIFont systemFontOfSize:weight:]設(shè)置的字體,其他接口設(shè)置的字體可自行添加。建議項(xiàng)目中用統(tǒng)一的接口設(shè)置字體罕伯,方便適配。
2叽讳、改變xib文件中設(shè)置fitDeviceFont屬性為true的字體追他。控件包括UILabel,UIButton,UITextField,UITextView岛蚤。
其他控件可自行添加邑狸。
3、根據(jù)屏幕適配字體大小涤妒,適配公式可自行定義(當(dāng)前代碼是通過屏幕寬度比例单雾,以414屏寬為基準(zhǔn),來修改字體大小)她紫,修改[UIFont fitFontSize:]函數(shù)即可硅堆。
文件:UIFont+FitDevice.h
@interface UIFont (FitDevice)
+ (CGFloat)fitFontSize:(CGFloat)curFontSize;
@end
#import "UIFont+FitDevice.h"
#import <objc/runtime.h>
const CGFloat default_screen_width = 414.0;
@implementation UIFont (FitDevice)
+ (void)load{
Method nm1 = class_getClassMethod([self class], @selector(fitDeviceFont:));
Method m1 = class_getClassMethod([self class], @selector(systemFontOfSize:));
method_exchangeImplementations(nm1, m1);
Method nm2 = class_getClassMethod([self class], @selector(fitDeviceFont:weight:));
Method m2 = class_getClassMethod([self class], @selector(systemFontOfSize:weight:));
method_exchangeImplementations(nm2, m2);
}
+ (UIFont*)fitDeviceFont:(CGFloat)fontSize{
CGFloat newSize = [UIFont fitFontSize:fontSize];
UIFont* font = [UIFont fitDeviceFont:newSize];
return font;
}
+ (UIFont*)fitDeviceFont:(CGFloat)fontSize weight:(UIFontWeight)weight{
CGFloat newSize = [UIFont fitFontSize:fontSize];
UIFont* font = [UIFont fitDeviceFont:newSize weight:weight];
return font;
}
+ (CGFloat)fitFontSize:(CGFloat)curFontSize{
CGFloat screenW = [UIScreen mainScreen].bounds.size.width;
CGFloat font_size = curFontSize*screenW/default_screen_width;
// if (screenW > default_screen_width) {
// font_size += 1;
// }else {
// font_size -= 1;
// }
return font_size;
}
@end
文件:UILabelExt.swift
extension UILabel{
@IBInspectable var fitDeviceFont:Bool{
get{
return self.fitDeviceFont
}
set{
if (newValue) {
let fontAttributes = self.font.fontDescriptor.fontAttributes
if let descriptor = changeFontAttributes(fontAttributes: fontAttributes) {
self.font = UIFont.init(descriptor: descriptor.0, size: descriptor.1)
}
}else{
}
}
}
}
extension UIButton{
@IBInspectable var fitDeviceFont:Bool{
get{
return self.fitDeviceFont
}
set{
if (newValue) {
if let fontAttributes = self.titleLabel?.font.fontDescriptor.fontAttributes {
if let descriptor = changeFontAttributes(fontAttributes: fontAttributes) {
self.titleLabel?.font = UIFont.init(descriptor: descriptor.0, size: descriptor.1)
}
}
}else{
}
}
}
}
extension UITextField{
@IBInspectable var fitDeviceFont:Bool{
get{
return self.fitDeviceFont
}
set{
if (newValue) {
if let fontAttributes = self.font?.fontDescriptor.fontAttributes {
if let descriptor = changeFontAttributes(fontAttributes: fontAttributes) {
self.font = UIFont.init(descriptor: descriptor.0, size: descriptor.1)
}
}
}else{
}
}
}
}
extension UITextView{
@IBInspectable var fitDeviceFont:Bool{
get{
return self.fitDeviceFont
}
set{
if (newValue) {
if let fontAttributes = self.font?.fontDescriptor.fontAttributes {
if let descriptor = changeFontAttributes(fontAttributes: fontAttributes) {
self.font = UIFont.init(descriptor: descriptor.0, size: descriptor.1)
}
}
}else{
}
}
}
}
func changeFontAttributes(fontAttributes:[UIFontDescriptor.AttributeName : Any])->(UIFontDescriptor,CGFloat)?{
var attributes = fontAttributes
let font_size_num = fontAttributes[UIFontDescriptor.AttributeName.size] as? NSNumber
let font_size = CGFloat(font_size_num?.floatValue ?? 0)
let fit_Font_size = UIFont.fitSize(font_size)
if font_size != fit_Font_size && fit_Font_size > 0 {
attributes[UIFontDescriptor.AttributeName.size] = NSNumber.init(value:fit_Font_size)
let descriptor = UIFontDescriptor.init(fontAttributes: fontAttributes)
return (descriptor,fit_Font_size)
}
return nil
}