UITextViewExtensions.swift 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. //
  2. // UITextViewExtensions.swift
  3. // EZSwiftExtensions
  4. //
  5. // Created by Goktug Yilmaz on 15/07/15.
  6. // Copyright (c) 2015 Goktug Yilmaz. All rights reserved.
  7. //
  8. #if os(iOS) || os(tvOS)
  9. import UIKit
  10. extension UITextView {
  11. public convenience init(x: CGFloat, y: CGFloat, w: CGFloat, h: CGFloat, fontSize: CGFloat) {
  12. self.init(frame: CGRect(x: x, y: y, width: w, height: h))
  13. font = UIFont.HelveticaNeue(type: FontType.None, size: fontSize)
  14. backgroundColor = UIColor.clear
  15. clipsToBounds = true
  16. textAlignment = NSTextAlignment.left
  17. isUserInteractionEnabled = true
  18. #if os(iOS)
  19. isEditable = false
  20. #endif
  21. isScrollEnabled = false
  22. }
  23. #if os(iOS)
  24. /// EZSE: Automatically adds a toolbar with a done button to the top of the keyboard. Tapping the button will dismiss the keyboard.
  25. public func addDoneButton(_ barStyle: UIBarStyle = .default, title: String? = nil) {
  26. let keyboardToolbar = UIToolbar()
  27. keyboardToolbar.items = [
  28. UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil),
  29. UIBarButtonItem(title: title ?? "Done", style: .done, target: self, action: #selector(resignFirstResponder))
  30. ]
  31. keyboardToolbar.barStyle = barStyle
  32. keyboardToolbar.sizeToFit()
  33. inputAccessoryView = keyboardToolbar
  34. }
  35. #endif
  36. }
  37. #endif