AMXFontAutoScale 1.2.0

AMXFontAutoScale 1.2.0

测试已测试
语言语言 Obj-CObjective C
许可证 MIT
发布最后发布2023年3月

Alex Maimescu 管理。



  • 作者
  • Alex Maimescu

AMXFontAutoScale

Twitter: @amaimescu License

按比例缩放不同屏幕尺寸下 UILabelUITextView 的字体。只需定义要用于缩放的屏幕尺寸作为参考,库将自动更新所有 UILabelUITextView 的实例。

用法

  1. 使用 Interface Builder 或直接使用 font 属性来设置 UILabelUITextView 的字体。
  2. 定义应自动缩放的哪些 标签文本视图。以下示例中将提供更多详细信息。
  3. 定义用于缩放的参考屏幕尺寸。您原始的字体大小将完全匹配所选择的参考屏幕尺寸,并且将根据其他屏幕尺寸进行缩放。
  4. 享受魔法吧!
iPhone 4 英寸 iPhone 4.7 英寸 iPhone 5.5 英寸
Contact List Contact Details Edit Contact

用法示例

为单个实例定义自动缩放

定义单个标签的参考屏幕尺寸。不同实例可以定义不同的参考屏幕尺寸

import AMXFontAutoScale

class SomeViewController: UIViewController {
    
    @IBOutlet var someLabel
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        someLabel.amx_autoScaleFont(forReferenceScreenSize: .size4Inch)
    }
}

为多个实例定义自动缩放

在实际操作中,大多数实例将共享相同的参考屏幕尺寸,所以逐个实例设置它不方便。您可以为全局参考屏幕尺寸定义,并只为特定实例启用自动缩放

import AMXFontAutoScale

class SomeViewController: UIViewController {
    
    @IBOutlet var someLabel1
    @IBOutlet var someLabel2
    @IBOutlet var someLabel3
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
	UILabel.amx_referenceScreenSize = .size4Inch
	
        someLabel1.amx_autoScaleEnabled = true
	someLabel2.amx_autoScaleEnabled = true
	someLabel3.amx_autoScaleEnabled = true
    }
}

或使用界面构建器

Interface Builder

🌍定义全局自动缩放

⚠️- 使用此功能时请小心,因为它实际上会按比例缩放您应用中所有UILabelUITextView实例,包括系统控件和组件中不明显标签或文本视图。

import AMXFontAutoScale

class AppDelegate: UIResponder, UIApplicationDelegate {

  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        // Scale all the label fonts using the 4 inch screen size as a reference
        UILabel.amx_autoScaleFont(forReferenceScreenSize: .size4Inch)
        
        return true
  }
}

注:如果设置了实例缩放,将覆盖全局缩放。

在启用全局缩放时禁用某些实例的缩放

import AMXFontAutoScale

class SomeViewController: UIViewController {
    
    @IBOutlet var someLabel
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Global font scaling is enabled
	UILabel.amx_autoScaleFont(forReferenceScreenSize: .size4Inch)
	
        // Font scaling for someLabel is disabled
	// Alternatively you can disable it using the Interface Builder 
        someLabel.amx_autoScaleEnabled = false
    }
}

处理字体大小手动更新

每次字体需要更新时都会调用一个闭包。在为属性字符串定义字体时可能很有用

import AMXFontAutoScale

class SomeViewController: UIViewController {
        
    override func viewDidLoad() {
        super.viewDidLoad()
        
        someLabel.amx_fontSizeUpdateHandler = { originalSize, preferredSize, multiplier in
	    // E.g. Compute the new fonts for the attributed text
        }
    }
}

安装

CocoaPods

如果您使用的是 CocoaPods,也可以使用它来集成库,只需将以下行添加到您的 Podfile 文件中。

use_frameworks!

target 'YourAppTarget' do
    pod "AMXFontAutoScale"
end

Carthage

如果您使用的是 Carthage,您总是可以在您的workspace中通过在 Cartfile 文件中添加以下行来构建库。

github "alexmx/AMXFontAutoScale"

手动安装

要将 AMXFontAutoScale 库集成到您的项目中,您需要从提供的源代码构建一个动态框架并将其添加到您的项目中,或者通过将其复制到项目目录或将它作为git子模块包含作为子项目来包含整个 AMXFontAutoScale 库。

许可证

本项目采用 MIT 许可证条款。请参阅 LICENSE 文件。