Skip to content

Instantly share code, notes, and snippets.

@acrookston
Last active December 14, 2017 00:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save acrookston/70154fc790ffc0a4c99811995295f26d to your computer and use it in GitHub Desktop.
Save acrookston/70154fc790ffc0a4c99811995295f26d to your computer and use it in GitHub Desktop.
//
// LICENSE: MIT
// Created by Andrew C on 4/25/17.
// Copyright © 2017 Andrew Crookston. All rights reserved.
//
class TopAlignedLabel: UILabel {
override func drawText(in rect: CGRect) {
if let string = text as NSString? {
let size = string.boundingRect(with: CGSize(width: self.frame.width,height: CGFloat.greatestFiniteMagnitude),
options: NSStringDrawingOptions.usesLineFragmentOrigin,
attributes: [NSFontAttributeName: font],
context: nil).size
super.drawText(in: CGRect(x: 0, y: 0, width: self.frame.width, height: ceil(size.height)))
} else {
super.drawText(in: rect)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment