From c9c424a3c7fc863ef42cd781146811174e5b5c08 Mon Sep 17 00:00:00 2001 From: Ruttanachai Auitragool Date: Thu, 10 Jan 2019 10:45:45 +0700 Subject: [PATCH] Add top, right, bottom, left margin of icon. and adjust default margin value. --- Sources/SkyFloatingLabelTextField.swift | 14 ++++- .../SkyFloatingLabelTextFieldWithIcon.swift | 53 +++++++++++++------ 2 files changed, 50 insertions(+), 17 deletions(-) diff --git a/Sources/SkyFloatingLabelTextField.swift b/Sources/SkyFloatingLabelTextField.swift index 3bb382c..9689071 100644 --- a/Sources/SkyFloatingLabelTextField.swift +++ b/Sources/SkyFloatingLabelTextField.swift @@ -114,6 +114,16 @@ open class SkyFloatingLabelTextField: UITextField { // swiftlint:disable:this ty } } + /** + A float value that determines the left margin of the title. + */ + @IBInspectable + dynamic open var titleMarginLeft: CGFloat = 0 { + didSet { + updateTitleLabel() + } + } + /// A UIColor value that determines the color of the bottom line when in the normal state @IBInspectable dynamic open var lineColor: UIColor = .lightGray { didSet { @@ -616,9 +626,9 @@ open class SkyFloatingLabelTextField: UITextField { // swiftlint:disable:this ty */ open func titleLabelRectForBounds(_ bounds: CGRect, editing: Bool) -> CGRect { if editing { - return CGRect(x: 0, y: 0, width: bounds.size.width, height: titleHeight()) + return CGRect(x: titleMarginLeft, y: 0, width: bounds.size.width, height: titleHeight()) } - return CGRect(x: 0, y: titleHeight(), width: bounds.size.width, height: titleHeight()) + return CGRect(x: titleMarginLeft, y: titleHeight(), width: bounds.size.width, height: titleHeight()) } /** diff --git a/Sources/SkyFloatingLabelTextFieldWithIcon.swift b/Sources/SkyFloatingLabelTextFieldWithIcon.swift index 11881a0..c2e2c0a 100644 --- a/Sources/SkyFloatingLabelTextFieldWithIcon.swift +++ b/Sources/SkyFloatingLabelTextFieldWithIcon.swift @@ -111,6 +111,29 @@ open class SkyFloatingLabelTextFieldWithIcon: SkyFloatingLabelTextField { } } + + /** + A float value that determines the right margin of the icon. + Use this value to position the icon more precisely horizontally. + */ + @IBInspectable + dynamic open var iconMarginRight: CGFloat = 8 { + didSet { + updateFrame() + } + } + + /** + A float value that determines the top margin of the icon. + Use this value to position the icon more precisely vertically. + */ + @IBInspectable + dynamic open var iconMarginTop: CGFloat = 0 { + didSet { + updateFrame() + } + } + /** A float value that determines the bottom margin of the icon. Use this value to position the icon more precisely vertically. @@ -242,11 +265,11 @@ open class SkyFloatingLabelTextFieldWithIcon: SkyFloatingLabelTextField { override open func textRect(forBounds bounds: CGRect) -> CGRect { var rect = super.textRect(forBounds: bounds) if isLTRLanguage { - rect.origin.x += CGFloat(iconWidth + iconMarginLeft) + rect.origin.x += CGFloat(iconWidth + iconMarginLeft + iconMarginRight) } else { - rect.origin.x -= CGFloat(iconWidth + iconMarginLeft) + rect.origin.x -= CGFloat(iconWidth + iconMarginLeft + iconMarginRight) } - rect.size.width -= CGFloat(iconWidth + iconMarginLeft) + rect.size.width -= CGFloat(iconWidth + iconMarginLeft + iconMarginRight) return rect } @@ -258,11 +281,11 @@ open class SkyFloatingLabelTextFieldWithIcon: SkyFloatingLabelTextField { override open func editingRect(forBounds bounds: CGRect) -> CGRect { var rect = super.editingRect(forBounds: bounds) if isLTRLanguage { - rect.origin.x += CGFloat(iconWidth + iconMarginLeft) + rect.origin.x += CGFloat(iconWidth + iconMarginLeft + iconMarginRight) } else { // don't change the editing field X position for RTL languages } - rect.size.width -= CGFloat(iconWidth + iconMarginLeft) + rect.size.width -= CGFloat(iconWidth + iconMarginLeft + iconMarginRight) return rect } @@ -275,11 +298,11 @@ open class SkyFloatingLabelTextFieldWithIcon: SkyFloatingLabelTextField { override open func placeholderRect(forBounds bounds: CGRect) -> CGRect { var rect = super.placeholderRect(forBounds: bounds) if isLTRLanguage { - rect.origin.x += CGFloat(iconWidth + iconMarginLeft) + rect.origin.x += CGFloat(iconWidth + iconMarginLeft + iconMarginRight) } else { // don't change the editing field X position for RTL languages } - rect.size.width -= CGFloat(iconWidth + iconMarginLeft) + rect.size.width -= CGFloat(iconWidth + iconMarginLeft + iconMarginRight) return rect } @@ -293,27 +316,27 @@ open class SkyFloatingLabelTextFieldWithIcon: SkyFloatingLabelTextField { let textWidth: CGFloat = bounds.size.width if isLTRLanguage { iconLabel.frame = CGRect( - x: 0, - y: bounds.size.height - textHeight() - iconMarginBottom, + x: 0 + iconMarginLeft, + y: bounds.size.height - textHeight() - iconMarginBottom - iconMarginTop, width: iconWidth, height: textHeight() ) iconImageView.frame = CGRect( - x: 0, - y: bounds.size.height - textHeight() - iconMarginBottom, + x: 0 + iconMarginLeft, + y: bounds.size.height - textHeight() - iconMarginBottom - iconMarginTop, width: iconWidth, height: textHeight() ) } else { iconLabel.frame = CGRect( - x: textWidth - iconWidth, - y: bounds.size.height - textHeight() - iconMarginBottom, + x: textWidth - iconWidth - iconMarginRight, + y: bounds.size.height - textHeight() - iconMarginBottom - iconMarginTop, width: iconWidth, height: textHeight() ) iconImageView.frame = CGRect( - x: textWidth - iconWidth, - y: bounds.size.height - textHeight() - iconMarginBottom, + x: textWidth - iconWidth - iconMarginRight, + y: bounds.size.height - textHeight() - iconMarginBottom - iconMarginTop, width: iconWidth, height: textHeight() )