Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add top, right, bottom, left margin of icon #267

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions Sources/SkyFloatingLabelTextField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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())
}

/**
Expand Down
53 changes: 38 additions & 15 deletions Sources/SkyFloatingLabelTextFieldWithIcon.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}

Expand All @@ -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
}

Expand All @@ -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
}

Expand All @@ -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()
)
Expand Down