Advertisement
Guest User

Untitled

a guest
Jun 29th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 1.19 KB | None | 0 0
  1. import UIKit
  2.  
  3. class TextView: UITextView {
  4.  
  5.     var border: UIView
  6.     var originalBorderFrame: CGRect
  7.     var originalInsetBottom: CGFloat
  8.  
  9.     deinit {
  10.         removeObserver(self, forKeyPath: "contentOffset")
  11.     }
  12.  
  13.     override var frame: CGRect {
  14.         didSet {
  15.             border.frame = CGRectMake(0, frame.height+contentOffset.y-border.frame.height, frame.width, border.frame.height)
  16.             originalBorderFrame  = CGRectMake(0, frame.height-border.frame.height, frame.width, border.frame.height);
  17.         }
  18.     }
  19.  
  20.     override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
  21.         if keyPath == "contentOffset" {
  22.             border.frame = CGRectOffset(originalBorderFrame, 0, contentOffset.y)
  23.         }
  24.     }
  25.  
  26.     func addBottomBorderWithColor(color: UIColor, width: CGFloat) {
  27.         border.backgroundColor = color
  28.         border.frame = CGRectMake(0, frame.height+contentOffset.y-width, self.frame.width, width)
  29.         originalBorderFrame = CGRectMake(0, frame.height-width, self.frame.width, width)
  30.         textContainerInset.bottom = originalInsetBottom+width
  31.     }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement