暂无描述

UITextFieldExt.swift 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. //
  2. // UITextFieldExt.swift
  3. // PaiaiUIKit
  4. //
  5. // Created by FFIB on 2017/9/14.
  6. // Copyright © 2017年 FFIB. All rights reserved.
  7. //
  8. import UIKit
  9. extension UITextField {
  10. public var isEmpty: Bool { return text?.isEmpty ?? false }
  11. public func setPlaceHolderTextColor(_ color: UIColor) {
  12. guard let holder = placeholder, !holder.isEmpty else {
  13. return
  14. }
  15. self.attributedPlaceholder = NSAttributedString(string: holder,
  16. attributes: [NSAttributedString.Key.foregroundColor: color])
  17. }
  18. public func addLeftPadding(_ padding: CGFloat) {
  19. let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: padding, height: frame.height))
  20. leftView = paddingView
  21. leftViewMode = .always
  22. }
  23. public func addRightPadding(_ padding: CGFloat) {
  24. let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: padding, height: frame.height))
  25. rightView = paddingView
  26. rightViewMode = .always
  27. }
  28. public func addLeftIcon(image: UIImage, point: CGPoint) {
  29. leftView = addIcon(image: image, point: point)
  30. leftViewMode = .always
  31. }
  32. public func addrightIcon(image: UIImage, point: CGPoint) {
  33. rightView = addIcon(image: image, point: point)
  34. rightViewMode = .always
  35. }
  36. private func addIcon(image: UIImage, point: CGPoint) -> UIView {
  37. let view = UIView()
  38. let imageView = UIImageView(frame: CGRect(x: point.x, y: point.y, width: 0, height: 0))
  39. imageView.image = image
  40. imageView.sizeToFit()
  41. view.frame = CGRect(x: 0,
  42. y: 0,
  43. width: imageView.width + point.x,
  44. height: imageView.height + point.y)
  45. view.addSubview(imageView)
  46. return view
  47. }
  48. public func clear() {
  49. text = ""
  50. attributedText = NSAttributedString(string: "")
  51. }
  52. }