Advertisement
rachmadi

EdgeInsetLabel

Nov 28th, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 1.67 KB | None | 0 0
  1. //
  2. //  EdgeInsetLabel.swift
  3. //  TabbedApp
  4. //
  5. //  Created by Muhammad Rachmadi on 23/10/17.
  6. //  Copyright © 2017 Muhammad Rachmadi. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10.  
  11. class EdgeInsetLabel: UILabel {
  12.  
  13.     var textInsets = UIEdgeInsets.zero {
  14.         didSet { invalidateIntrinsicContentSize() }
  15.     }
  16.    
  17.     override func textRect(forBounds bounds: CGRect, limitedToNumberOfLines numberOfLines: Int) -> CGRect {
  18.         let insetRect = UIEdgeInsetsInsetRect(bounds, textInsets)
  19.         let textRect = super.textRect(forBounds: insetRect, limitedToNumberOfLines: numberOfLines)
  20.         let invertedInsets = UIEdgeInsets(top: -textInsets.top,
  21.                                           left: -textInsets.left,
  22.                                           bottom: -textInsets.bottom,
  23.                                           right: -textInsets.right)
  24.         return UIEdgeInsetsInsetRect(textRect, invertedInsets)
  25.     }
  26.    
  27.     override func drawText(in rect: CGRect) {
  28.         super.drawText(in: UIEdgeInsetsInsetRect(rect, textInsets))
  29.     }
  30.  
  31. }
  32.  
  33. extension EdgeInsetLabel {
  34.     @IBInspectable
  35.     var leftTextInset: CGFloat {
  36.         set { textInsets.left = newValue }
  37.         get { return textInsets.left }
  38.     }
  39.    
  40.     @IBInspectable
  41.     var rightTextInset: CGFloat {
  42.         set { textInsets.right = newValue }
  43.         get { return textInsets.right }
  44.     }
  45.    
  46.     @IBInspectable
  47.     var topTextInset: CGFloat {
  48.         set { textInsets.top = newValue }
  49.         get { return textInsets.top }
  50.     }
  51.    
  52.     @IBInspectable
  53.     var bottomTextInset: CGFloat {
  54.         set { textInsets.bottom = newValue }
  55.         get { return textInsets.bottom }
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement