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

Increase field height based on placeholder #153

Closed
wants to merge 1 commit into from
Closed
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
27 changes: 22 additions & 5 deletions Source/WSTagsField.swift
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,6 @@ extension WSTagsField {

if textField.isEnabled {
var textFieldRect = CGRect.zero
textFieldRect.size.height = Constants.STANDARD_ROW_HEIGHT

if availableWidthForTextField < Constants.MINIMUM_TEXTFIELD_WIDTH {
// If in the future we add more UI elements below the tags,
Expand All @@ -721,6 +720,10 @@ extension WSTagsField {
textFieldRect.origin.x = curX
textFieldRect.size.width = availableWidthForTextField

// using the available width to find the height required for placeholder.
let tempSize = CGSize(width: availableWidthForTextField, height: UIScreen.main.bounds.height)
let placeholderHeight: CGFloat = showsPlaceholder ? getPlaceholderHeight(in: tempSize) : .zero
textFieldRect.size.height = max(Constants.STANDARD_ROW_HEIGHT, placeholderHeight)
closure(nil, nil, textFieldRect)
}
}
Expand Down Expand Up @@ -772,16 +775,30 @@ extension WSTagsField {
}

fileprivate func updatePlaceholderTextVisibility() {
textField.attributedPlaceholder = (placeholderAlwaysVisible || tags.count == 0) ? attributedPlaceholder() : nil
textField.attributedPlaceholder = showsPlaceholder ? attributedPlaceholder() : nil
}

private func getPlaceholderHeight(in size: CGSize) -> CGFloat {
attributedPlaceholder().boundingRect(with: size, options: .usesLineFragmentOrigin, context: nil).height
+ contentInset.top
+ contentInset.bottom
}

private var showsPlaceholder: Bool {
placeholderAlwaysVisible || tags.count == 0
}

private func attributedPlaceholder() -> NSAttributedString {
var attributes: [NSAttributedString.Key: Any]?
var attributes: [NSAttributedString.Key: Any] = [:]
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineBreakMode = .byWordWrapping
attributes[.paragraphStyle] = paragraphStyle

if let placeholderColor = placeholderColor {
attributes = [NSAttributedString.Key.foregroundColor: placeholderColor]
attributes[.foregroundColor] = placeholderColor
}
if let placeholderFont = placeholderFont {
attributes = [NSAttributedString.Key.font: placeholderFont]
attributes[.font] = placeholderFont
}
return NSAttributedString(string: placeholder, attributes: attributes)
}
Expand Down