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

Properties to customize vertical spacing and the title visible status #255

Open
wants to merge 3 commits 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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@ textField1.iconImage = UIImage(imageLiteralResourceName: "PlaneIcon")
self.view.addSubview(textField1)
```

### Title and line spacing
```swift
userTextField.titleVerticalSpacing = 10
userTextField.lineVerticalSpacing = 5
```

### Error state and delegates

The textfield supports displaying an error state - this can be useful for example when validating fields on the fly. When the `errorMessage` property is set on the control, then the control is highlighted with the color set in the `errorColor` property.
Expand Down
31 changes: 24 additions & 7 deletions Sources/SkyFloatingLabelTextField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,20 @@ open class SkyFloatingLabelTextField: UITextField { // swiftlint:disable:this ty
setNeedsDisplay()
}
}

// MARK: Components spacing

@IBInspectable dynamic open var lineVerticalSpacing: CGFloat = 0 {
didSet {
setNeedsDisplay()
}
}

@IBInspectable dynamic open var titleVerticalSpacing: CGFloat = 0 {
didSet {
setNeedsDisplay()
}
}

// MARK: View components

Expand Down Expand Up @@ -232,6 +246,9 @@ open class SkyFloatingLabelTextField: UITextField { // swiftlint:disable:this ty
updateControl(true)
}
}

@IBInspectable
open var alwaysShowTitle: Bool = false

/// The backing property for the highlighted property
fileprivate var _highlighted: Bool = false
Expand Down Expand Up @@ -489,7 +506,7 @@ open class SkyFloatingLabelTextField: UITextField { // swiftlint:disable:this ty
if hasErrorMessage {
titleText = titleFormatter(errorMessage!)
} else {
if editingOrSelected {
if editingOrSelected || alwaysShowTitle {
titleText = selectedTitleOrTitlePlaceholder()
if titleText == nil {
titleText = titleOrPlaceholder()
Expand Down Expand Up @@ -527,7 +544,7 @@ open class SkyFloatingLabelTextField: UITextField { // swiftlint:disable:this ty
- returns: True if the title is displayed on the control, false otherwise.
*/
open func isTitleVisible() -> Bool {
return hasText || hasErrorMessage || _titleVisible
return hasText || hasErrorMessage || _titleVisible || alwaysShowTitle
}

fileprivate func updateTitleVisibility(_ animated: Bool = false, completion: ((_ completed: Bool) -> Void)? = nil) {
Expand Down Expand Up @@ -568,7 +585,7 @@ open class SkyFloatingLabelTextField: UITextField { // swiftlint:disable:this ty
x: superRect.origin.x,
y: titleHeight,
width: superRect.size.width,
height: superRect.size.height - titleHeight - selectedLineHeight
height: superRect.size.height - titleHeight - selectedLineHeight - lineVerticalSpacing
)
return rect
}
Expand All @@ -586,7 +603,7 @@ open class SkyFloatingLabelTextField: UITextField { // swiftlint:disable:this ty
x: superRect.origin.x,
y: titleHeight,
width: superRect.size.width,
height: superRect.size.height - titleHeight - selectedLineHeight
height: superRect.size.height - titleHeight - selectedLineHeight - lineVerticalSpacing
)
return rect
}
Expand All @@ -601,7 +618,7 @@ open class SkyFloatingLabelTextField: UITextField { // swiftlint:disable:this ty
x: 0,
y: titleHeight(),
width: bounds.size.width,
height: bounds.size.height - titleHeight() - selectedLineHeight
height: bounds.size.height - titleHeight() - selectedLineHeight - lineVerticalSpacing
)
return rect
}
Expand Down Expand Up @@ -640,7 +657,7 @@ open class SkyFloatingLabelTextField: UITextField { // swiftlint:disable:this ty
open func titleHeight() -> CGFloat {
if let titleLabel = titleLabel,
let font = titleLabel.font {
return font.lineHeight
return font.lineHeight + titleVerticalSpacing
}
return 15.0
}
Expand Down Expand Up @@ -687,7 +704,7 @@ open class SkyFloatingLabelTextField: UITextField { // swiftlint:disable:this ty
- returns: the content size to be used for auto layout
*/
override open var intrinsicContentSize: CGSize {
return CGSize(width: bounds.size.width, height: titleHeight() + textHeight())
return CGSize(width: bounds.size.width, height: titleHeight() + textHeight() + lineVerticalSpacing)
}

// MARK: - Helpers
Expand Down