AMXFontAutoScale 1.2.0

AMXFontAutoScale 1.2.0

TestsTested
LangLanguage Obj-CObjective C
License MIT
ReleasedLast Release Jan 2018

Maintained by Alex Maimescu.



  • By
  • Alex Maimescu

Scale the font for UILabel and UITextView proportionally across all the screen sizes. Just define the screen size to be used as reference for scaling and the library will update all the instances of the UILabel and UITextView automatically.

Usage

  1. Set the UILabel or UITextView font using the font property or Interface Builder.
  2. Decide if you want to apply the automatic scaling globally or for particular instances. You can mix both approaches.
  3. Set the reference screen size you want to be used for scaling. Your original font size will be used for reference screen size and scaled up or down for other screen sizes.
  4. Enjoy the magic!
iPhone 4 inch iPhone 4.7 inch iPhone 5.5 inch
Contact List Contact Details Edit Contact

🌍 Global scaling

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
  }
}

Instance scaling

import AMXFontAutoScale

class SomeViewController: UIViewController {
    
    var someLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 50))
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        someLabel.amx_autoScaleFont(forReferenceScreenSize: .size4Inch)
    }
}

Note: The instance scaling overrides the global one if set.

Disable scaling for some instances

import AMXFontAutoScale

class SomeViewController: UIViewController {
    
    var someLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 50))
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Global font scaling is enabled
	UILabel.amx_autoScaleFont(forReferenceScreenSize: .size4Inch)
	
        // Font scaling for someLabel is disabled
        someLabel.amx_autoScaleEnabled = false
    }
}

Handle manually font size updates

Get a closure called every time the font should be updated.

import AMXFontAutoScale

class SomeViewController: UIViewController {
        
    override func viewDidLoad() {
        super.viewDidLoad()
        
        someLabel.amx_fontSizeUpdateHandler = { [weak someLabel] originalSize, preferredSize, multiplier in
	    let newFont = ... // Compute the new font
	    someLabel.font = newFont // Set the new computed font
        }
    }
}

Installation

Manual installation

In order to include the AMXFontAutoScale library into your project, you need to build a dynamic framework from provided source code and include it into your project, or inlcude the entire AMXFontAutoScale library as sub-project by copying it to your project directory or include as git submodule.

License

This project is licensed under the terms of the MIT license. See the LICENSE file.