Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // EdgeInsetLabel.swift
- // TabbedApp
- //
- // Created by Muhammad Rachmadi on 23/10/17.
- // Copyright © 2017 Muhammad Rachmadi. All rights reserved.
- //
- import UIKit
- class EdgeInsetLabel: UILabel {
- var textInsets = UIEdgeInsets.zero {
- didSet { invalidateIntrinsicContentSize() }
- }
- override func textRect(forBounds bounds: CGRect, limitedToNumberOfLines numberOfLines: Int) -> CGRect {
- let insetRect = UIEdgeInsetsInsetRect(bounds, textInsets)
- let textRect = super.textRect(forBounds: insetRect, limitedToNumberOfLines: numberOfLines)
- let invertedInsets = UIEdgeInsets(top: -textInsets.top,
- left: -textInsets.left,
- bottom: -textInsets.bottom,
- right: -textInsets.right)
- return UIEdgeInsetsInsetRect(textRect, invertedInsets)
- }
- override func drawText(in rect: CGRect) {
- super.drawText(in: UIEdgeInsetsInsetRect(rect, textInsets))
- }
- }
- extension EdgeInsetLabel {
- @IBInspectable
- var leftTextInset: CGFloat {
- set { textInsets.left = newValue }
- get { return textInsets.left }
- }
- @IBInspectable
- var rightTextInset: CGFloat {
- set { textInsets.right = newValue }
- get { return textInsets.right }
- }
- @IBInspectable
- var topTextInset: CGFloat {
- set { textInsets.top = newValue }
- get { return textInsets.top }
- }
- @IBInspectable
- var bottomTextInset: CGFloat {
- set { textInsets.bottom = newValue }
- get { return textInsets.bottom }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement