DynamicTypeFont helper. Easy creation, custom font, auto update etc.
- Custom dynamic type font available.
- easy creation and powerful initializer (See Usage).
- Useful extension for
UILabel,UITextField,UITextView,UIButton. update automatically dynamic type font when UIContentSizeCategory changed in Setting.app.
import DTFont
// use UIFontTextStyle
_ = DTFont.make(with: "Avenir-Book", textStyle: .body)
// use DTSize (DTSize is a enum wrapped UIContentSizeCategory
_ = DTFont.make(with: "Avenir-Book") { size -> CGFloat in
switch size {
case .xs ... .l:
return 18.0
case .xl ... .xxxl:
return 22.0
default:
return 24.0
}
}
// use DTSize and comparision operator
_ = DTFont.make(with: "Avenir-Book") { size in size < .xl ? 18.0 : 22.0 }
_ = DTFont.make(with: "Avenir-Book") { $0 < .xl ? 18.0 : 22.0 }If a user change UIContentSieCategory in Setting.app ([Accessibility] -> [Large Text]), dynamic type font must be updated manually.
DTFontUpdater is a class that subscribe UIContentSizeCategoryDidChange notification.
import DTFont
class ViewControlelr: UIViewController {
@IBOutlet private weak var label: UILabel!
private lazy var updater = DTFontUpdater()
override func viewDidLoad() {
super.viewDidLoad()
updater.updateHandler = { [weak self] _ in
self?.label.font = DTFont.make(with: "Avenir-Book", textStyle: .body)
}
// update immediately
updater.update()
}
}If you use dynamic type font in UILabel, UITextField, UITextView, UIButton,
use enableAutomaticFontUpdate:(updateImmediately:) better than using DTFontUpdater.
class ViewControlelr: UIViewController {
@IBOutlet private weak var label: UILabel! {
didSet {
label.enableAutomaticFontUpdate(with: DTFont.make(with: "Avenir-Book") { $0 < .l ? 18.0 : 22.0 })
// if you don't want to update immediately
label.enableAutomaticFontUpdate(with: DTFont.make(with: "Avenir-Book") { $0 < .l ? 18.0 : 22.0 }, updateImmediately: false)
}
}
}- iOS 8.0 or later
- Xcode 8.0
- Swift 3.0
- Add the following to your Cartfile:
github 'sgr-ksmt/DTFont'- Then run command:
$ carthage update - Add the framework as described.
Details: Carthage Readme
DTFont is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'DTFont'and run pod install
- If you found a bug, please open an issue. 🙇
- Also, if you have a feature request, please open an issue. 👍
- If you want to contribute, submit a pull request.:muscle:
DTFont is under MIT license. See the LICENSE file for more info.
