Skip to content

Commit

Permalink
PlaceholderLabel Add option for alignment in X #101
Browse files Browse the repository at this point in the history
  • Loading branch information
muukii authored Mar 10, 2022
1 parent d13c8c1 commit 84bfae7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
1 change: 1 addition & 0 deletions NextGrowingTextView-Demo/FixedWidthViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ final class FixedWidthViewController: CodeBasedViewController {

let textView = NextGrowingTextView()&>.do {
$0.configuration = .init(
placeholderHorizontalLayout: .center,
minLines: 1,
maxLines: 10,
isAutomaticScrollToBottomEnabled: true,
Expand Down
30 changes: 27 additions & 3 deletions NextGrowingTextView/NextGrowingTextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ open class NextGrowingTextView: UIView {
/// Hides typed text one or more.
case onTypedText
}

public enum PlaceholderHorizontalLayout {
case leading
case center
case trailing
}

public var minLines: Int
public var maxLines: Int
Expand All @@ -41,16 +47,19 @@ open class NextGrowingTextView: UIView {
public var isFlashScrollIndicatorsEnabled: Bool = false

public var placeholderHidingMode: PlaceholderHidingMode
public var placeholderHorizontalLayout: PlaceholderHorizontalLayout

public init(
placeholderHidingMode: PlaceholderHidingMode = .onTypedText,
placeholderHorizontalLayout: PlaceholderHorizontalLayout = .leading,
minLines: Int = 1,
maxLines: Int = 3,
isAutomaticScrollToBottomEnabled: Bool = true,
isFlashScrollIndicatorsEnabled: Bool = false
) {

self.placeholderHidingMode = placeholderHidingMode
self.placeholderHorizontalLayout = placeholderHorizontalLayout
self.minLines = minLines
self.maxLines = maxLines
self.isAutomaticScrollToBottomEnabled = isAutomaticScrollToBottomEnabled
Expand Down Expand Up @@ -163,12 +172,27 @@ open class NextGrowingTextView: UIView {
NSLayoutConstraint.activate([

placeholderLabel.topAnchor.constraint(equalTo: topAnchor, constant: inset.top),
placeholderLabel.leftAnchor.constraint(equalTo: leftAnchor, constant: inset.left + 4),
placeholderLabel.rightAnchor.constraint(lessThanOrEqualTo: rightAnchor, constant: -(inset.right + 4)),
placeholderLabel.bottomAnchor.constraint(lessThanOrEqualTo: bottomAnchor, constant: -inset.bottom),

])

switch configuration.placeholderHorizontalLayout {
case .leading:
NSLayoutConstraint.activate([
placeholderLabel.leadingAnchor.constraint(equalTo: leadingAnchor, constant: inset.left + 4),
placeholderLabel.trailingAnchor.constraint(lessThanOrEqualTo: trailingAnchor, constant: -(inset.right + 4)),
])
case .center:
NSLayoutConstraint.activate([
placeholderLabel.centerXAnchor.constraint(equalTo: centerXAnchor, constant: 0),
])
case .trailing:
NSLayoutConstraint.activate([
placeholderLabel.leadingAnchor.constraint(greaterThanOrEqualTo: leadingAnchor, constant: inset.left + 4),
placeholderLabel.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -(inset.right + 4)),
])
}

// refresh with current state and new configuration
update(by: state)

Expand Down

0 comments on commit 84bfae7

Please sign in to comment.