                 attributes[NSFontAttributeName] = font
             }
             
-            placeHolder.drawInRect(rect, withAttributes: attributes)
+            placeHolder.draw(in: rect, withAttributes: attributes)
         }
     }
     
     // Trim white space and new line characters when end editing.
-    func textDidEndEditing(notification: NSNotification) {
-        if notification.object === self {
-            if trimWhiteSpaceWhenEndEditing {
-                text = text?.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
+    func textDidEndEditing(notification: Notification) {
+        if let notificationObject = notification.object as? GrowingTextView {
+            if notificationObject === self {
+                if trimWhiteSpaceWhenEndEditing {
+                    text = text?.trimmingCharacters(in: .whitespacesAndNewlines)
+                    setNeedsDisplay()
+                }
+            }
+        }
+    }
+    
+    // Limit the length of text
+    func textDidChange(notification: Notification) {
+        if let notificationObject = notification.object as? GrowingTextView {
+            if notificationObject === self {
+                if maxLength > 0 && text.characters.count > maxLength {
+                    
+                    let endIndex = text.index(text.startIndex, offsetBy: maxLength)
+                    text = text.substring(to: endIndex)
+                    undoManager?.removeAllActions()
+                }
                 setNeedsDisplay()
             }
         }
     }
-
-    // Limit the length of text
-    func textDidChange(notification: NSNotification) {
-        if notification.object === self {
-            if maxLength > 0 && text.characters.count > maxLength {
-                let endIndex = text.startIndex.advancedBy(maxLength)
-                text = text.substringToIndex(endIndex)
-                undoManager?.removeAllActions()
-            }
-            setNeedsDisplay()
-        }
-    }
 }

    
    // Listen to UITextView notification to handle trimming, placeholder and maximum length
    private func commonInit() {
    
        self.contentMode = .Redraw
    
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(textDidChange), name: UITextViewTextDidChangeNotification, object: self)
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(textDidEndEditing), name: UITextViewTextDidEndEditingNotification, object: self)
    }
    
    // Remove notification observer when deinit
    deinit {
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    
    // Calculate height of textview
    override public func layoutSubviews() {
        super.layoutSubviews()
        let size = sizeThatFits(CGSizeMake(bounds.size.width, CGFloat.max))
        var height = size.height
        if maxHeight > 0 {
            height = min(size.height, maxHeight)
        }
    
        if (heightConstraint == nil) {
            heightConstraint = NSLayoutConstraint(item: self, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: height)
            addConstraint(heightConstraint!)
        }
        

            self.heightConstraint!.constant = height;
            scrollRangeToVisible(NSMakeRange(0, 0))
            if let delegate = delegate as? GrowingTextViewDelegate {
                delegate.textViewDidChangeHeight?(height: height)
            }
        }
    }
    
    // Show placeholder
    override public func draw(_ rect: CGRect) {
        super.draw(rect)
        if text.isEmpty {
            guard let placeHolder = placeHolder else { return }
            guard let placeHolderColor = placeHolderColor else { return }
            let paragraphStyle = NSMutableParagraphStyle()
            paragraphStyle.alignment = textAlignment
            
            let rect = CGRect(x: textContainerInset.left + placeHolderLeftMargin,
                              y: textContainerInset.top,
                              width:   frame.size.width - textContainerInset.left - textContainerInset.right,
                              height: frame.size.height)
            
            var attributes = [
                NSForegroundColorAttributeName: placeHolderColor,
