Skip to content

Instantly share code, notes, and snippets.

@mehmetfarhan
Last active April 26, 2019 21:37
Show Gist options
  • Save mehmetfarhan/6ca3c2411d2f16dbb3c9dbebcec488af to your computer and use it in GitHub Desktop.
Save mehmetfarhan/6ca3c2411d2f16dbb3c9dbebcec488af to your computer and use it in GitHub Desktop.
PaddedLabel
//
// PaddedLabel.swift
// PaddedLabel
//
// Created by Mohammad Farhan on 4/27/19.
// Copyright © 2019 PaddedLabel. All rights reserved.
//
import UIKit
@IBDesignable
final class PaddedLabel: UILabel {
@IBInspectable var inset: CGSize = CGSize.zero
var padding: UIEdgeInsets {
guard let text = self.text else { return UIEdgeInsets.zero }
let hasText = text.isEmpty ? false : true
if !hasText { return UIEdgeInsets.zero }
return UIEdgeInsets(top: inset.height,
left: inset.width,
bottom: inset.height,
right: inset.width)
}
override func drawText(in rect: CGRect) {
super.drawText(in: rect.inset(by: padding))
}
override var intrinsicContentSize: CGSize {
let superContentSize = super.intrinsicContentSize
let width = superContentSize.width + padding.left + padding.right
let heigth = superContentSize.height + padding.top + padding.bottom
return CGSize(width: width, height: heigth)
}
override func sizeThatFits(_ size: CGSize) -> CGSize {
let superSizeThatFits = super.sizeThatFits(size)
let width = superSizeThatFits.width + padding.left + padding.right
let heigth = superSizeThatFits.height + padding.top + padding.bottom
return CGSize(width: width, height: heigth)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment