Created
August 20, 2018 18:29
-
-
Save cscouto/0be954b3b9d3de33b0df80289f298513 to your computer and use it in GitHub Desktop.
SWIFT - UILabel content size for specific width
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
// | |
// UILabel+height.swift | |
// AllBody | |
// | |
// Created by Tiago Do Couto on 8/20/18. | |
// Copyright © 2018 coutocode. All rights reserved. | |
// | |
import UIKit | |
extension UILabel { | |
func textHeight(withWidth width: CGFloat) -> CGFloat { | |
return text?.height(withWidth: width, font: font) ?? 0 | |
} | |
} | |
extension String { | |
func height(withWidth width: CGFloat, font: UIFont) -> CGFloat { | |
let maxSize = CGSize(width: width, height: CGFloat.greatestFiniteMagnitude) | |
let contentSize = self.boundingRect(with: maxSize, options: [.usesLineFragmentOrigin], attributes: [.font : font], context: nil) | |
return contentSize.height | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment