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.
- Set the
UILabel
orUITextView
font usingInterface Builder
or thefont
property directly. - Define for which labels and text views the font should be auto scaled. Check the examples below for more details.
- Define the reference screen size to be used for scaling. Your original font size will match exactly the chosen reference screen size and will be scaled up or down for other screen sizes.
- Enjoy the magic!
iPhone 4 inch | iPhone 4.7 inch | iPhone 5.5 inch |
---|---|---|
Define the reference screen size for a specific label. Different instances can have different reference screen sizes defined:
import AMXFontAutoScale
class SomeViewController: UIViewController {
@IBOutlet var someLabel
override func viewDidLoad() {
super.viewDidLoad()
someLabel.amx_autoScaleFont(forReferenceScreenSize: .size4Inch)
}
}
In practice most of the instances will share the same reference screen size, so it is inconvenient to set it per instance. You can define the global reference screen size and just enable the auto scaling for particular instances:
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
}
}
Or using the Interface Builder:
UILabel
and UITextView
from your app, even the unobvious labels or text views in the system controls and components.
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
}
}
Note: The instance scaling overrides the global one if set.
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
}
}
Get a closure called every time the font should be updated. Might be convenient when defining the fonts for the attributed strings:
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
}
}
}
If you are using CocoaPods, you can as well use it to integrate the library by adding the following lines to your Podfile
.
use_frameworks!
target 'YourAppTarget' do
pod "AMXFontAutoScale"
end
If you are using Carthage, you can always use it to build the library within your workspace by adding the line below to your Cartfile
.
github "alexmx/AMXFontAutoScale"
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 include the entire AMXFontAutoScale library as sub-project by copying it to your project directory or include as git submodule.
This project is licensed under the terms of the MIT license. See the LICENSE file.