Skip to content

Instantly share code, notes, and snippets.

@txaiwieser
Last active August 27, 2015 21:59
Show Gist options
  • Save txaiwieser/5b2cc3dc40d5b7951bc4 to your computer and use it in GitHub Desktop.
Save txaiwieser/5b2cc3dc40d5b7951bc4 to your computer and use it in GitHub Desktop.
UILabel subclass that resizes the font size to fit width AND height
//
// 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