Last active
August 27, 2015 21:59
-
-
Save txaiwieser/5b2cc3dc40d5b7951bc4 to your computer and use it in GitHub Desktop.
UILabel subclass that resizes the font size to fit width AND height
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// UIFitLabel.swift | |
// | |
// | |
// Created by Txai Wieser on 8/27/15. | |
// Copyright © 2015 Txai Wieser. All rights reserved. | |
// | |
import UIKit | |
@IBDesignable class UIFitLabel: UILabel { | |
required init?(coder aDecoder: NSCoder) { | |
super.init(coder: aDecoder) | |
initialize() | |
} | |
override init(frame: CGRect) { | |
super.init(frame: frame) | |
initialize() | |
} | |
func initialize() { | |
numberOfLines = 1 | |
adjustsFontSizeToFitWidth = true | |
minimumScaleFactor = 0 | |
textAlignment = NSTextAlignment.Center | |
baselineAdjustment = UIBaselineAdjustment.AlignCenters | |
} | |
override func prepareForInterfaceBuilder() { | |
initialize() | |
} | |
override var frame:CGRect { | |
didSet { | |
font = font.fontWithSize(frame.height) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment