EZSwiftExtensions/Sources/UILabelExtensions.swift

75 lines
2.2 KiB
Swift

//
// UILabelExtensions.swift
// EZSwiftExtensions
//
// Created by Goktug Yilmaz on 15/07/15.
// Copyright (c) 2015 Goktug Yilmaz. All rights reserved.
//
#if os(iOS) || os(tvOS)
import UIKit
extension UILabel {
/// EZSE: Initialize Label with a font, color and alignment.
public convenience init(font: UIFont, color: UIColor, alignment: NSTextAlignment) {
self.init()
self.font = font
self.textColor = color
self.textAlignment = alignment
}
/// EZSwiftExtensions
public convenience init(x: CGFloat, y: CGFloat, w: CGFloat, h: CGFloat, fontSize: CGFloat = 17) {
self.init(frame: CGRect(x: x, y: y, width: w, height: h))
font = UIFont.HelveticaNeue(type: FontType.None, size: fontSize)
backgroundColor = UIColor.clear
clipsToBounds = true
textAlignment = NSTextAlignment.left
isUserInteractionEnabled = true
numberOfLines = 1
}
/// EZSwiftExtensions
public func getEstimatedSize(_ width: CGFloat = CGFloat.greatestFiniteMagnitude, height: CGFloat = CGFloat.greatestFiniteMagnitude) -> CGSize {
return sizeThatFits(CGSize(width: width, height: height))
}
/// EZSwiftExtensions
public func getEstimatedHeight() -> CGFloat {
return sizeThatFits(CGSize(width: w, height: CGFloat.greatestFiniteMagnitude)).height
}
/// EZSwiftExtensions
public func getEstimatedWidth() -> CGFloat {
return sizeThatFits(CGSize(width: CGFloat.greatestFiniteMagnitude, height: h)).width
}
/// EZSwiftExtensions
public func fitHeight() {
self.h = getEstimatedHeight()
}
/// EZSwiftExtensions
public func fitWidth() {
self.w = getEstimatedWidth()
}
/// EZSwiftExtensions
public func fitSize() {
self.fitWidth()
self.fitHeight()
sizeToFit()
}
/// EZSwiftExtensions (if duration set to 0 animate wont be)
public func set(text _text: String?, duration: TimeInterval) {
UIView.transition(with: self, duration: duration, options: .transitionCrossDissolve, animations: { () -> Void in
self.text = _text
}, completion: nil)
}
}
#endif