Skip to content

Add next label position #273

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

Merged
merged 2 commits into from
May 25, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public class CoachMarkBodyDefaultView: UIControl,
public lazy var nextLabel: UILabel = makeNextLabel()
public lazy var hintLabel: UITextView = makeHintTextView()
public lazy var separator: UIView = makeSeparator()
private let nextLabelPosition: CoachMarkNextLabelPosition?
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd set the default position (.trailing) directly here.

Suggested change
private let nextLabelPosition: CoachMarkNextLabelPosition?
private let nextLabelPosition: CoachMarkNextLabelPosition = .trailing

private let spacing: CGFloat = 18

public var background: CoachMarkBodyBackgroundStyle {
get { return bodyBackground }
Expand All @@ -40,11 +42,15 @@ public class CoachMarkBodyDefaultView: UIControl,

// MARK: - Initialization
override public init(frame: CGRect) {
self.nextLabelPosition = .none

super.init(frame: frame)
initializeViewHierarchy()
}

public init(frame: CGRect, hintText: String, nextText: String?) {
public init(frame: CGRect, hintText: String, nextText: String?, nextLabelPosition: CoachMarkNextLabelPosition?) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably want to force users to provide a position that isn't nil.

Suggested change
public init(frame: CGRect, hintText: String, nextText: String?, nextLabelPosition: CoachMarkNextLabelPosition?) {
public init(frame: CGRect, hintText: String, nextText: String?, nextLabelPosition: CoachMarkNextLabelPosition) {

self.nextLabelPosition = nextLabelPosition

super.init(frame: frame)
initializeViewHierarchy()

Expand All @@ -55,12 +61,19 @@ public class CoachMarkBodyDefaultView: UIControl,
hintLabel.text = hintText
}

convenience public init(hintText: String, nextText: String?) {
self.init(frame: CGRect.zero, hintText: hintText, nextText: nextText)
public init(frame: CGRect, nextLabelPosition: CoachMarkNextLabelPosition?) {
self.nextLabelPosition = nextLabelPosition

super.init(frame: frame)
initializeViewHierarchy()
}

convenience public init() {
self.init(frame: CGRect.zero)
convenience public init(hintText: String, nextText: String?, nextLabelPosition: CoachMarkNextLabelPosition?) {
self.init(frame: CGRect.zero, hintText: hintText, nextText: nextText, nextLabelPosition: nextLabelPosition)
}

convenience public init(nextLabelPosition: CoachMarkNextLabelPosition?) {
self.init(frame: CGRect.zero, nextLabelPosition: nextLabelPosition)
}

required public init?(coder aDecoder: NSCoder) {
Expand All @@ -82,12 +95,63 @@ private extension CoachMarkBodyDefaultView {
bodyBackground.fillSuperview()
labelStackView.fillSuperview(insets: UIEdgeInsets(top: 10, left: 15, bottom: 10, right: 15))

labelStackView.addArrangedSubview(hintLabel)
labelStackView.addArrangedSubview(separator)
labelStackView.addArrangedSubview(nextLabel)

separator.heightAnchor.constraint(equalTo: labelStackView.heightAnchor,
constant: -10).isActive = true
switch nextLabelPosition {
case .topRight:
labelStackView.addSubview(hintLabel)
labelStackView.addSubview(nextLabel)

NSLayoutConstraint.activate([
nextLabel.topAnchor.constraint(equalTo: topAnchor, constant: spacing),
nextLabel.bottomAnchor.constraint(equalTo: hintLabel.topAnchor, constant: -spacing),
nextLabel.rightAnchor.constraint(equalTo: rightAnchor, constant: -spacing),
hintLabel.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -spacing),
hintLabel.leftAnchor.constraint(equalTo: leftAnchor, constant: spacing),
hintLabel.rightAnchor.constraint(lessThanOrEqualTo: rightAnchor, constant: -spacing)
])
case .topLeft:
labelStackView.addSubview(hintLabel)
labelStackView.addSubview(nextLabel)

NSLayoutConstraint.activate([
nextLabel.topAnchor.constraint(equalTo: topAnchor, constant: spacing),
nextLabel.bottomAnchor.constraint(equalTo: hintLabel.topAnchor, constant: -spacing),
nextLabel.leftAnchor.constraint(equalTo: leftAnchor, constant: spacing),
hintLabel.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -spacing),
hintLabel.leftAnchor.constraint(equalTo: leftAnchor, constant: spacing),
hintLabel.rightAnchor.constraint(lessThanOrEqualTo: rightAnchor, constant: -spacing)
])
case .bottomRight:
labelStackView.addSubview(hintLabel)
labelStackView.addSubview(nextLabel)

NSLayoutConstraint.activate([
hintLabel.topAnchor.constraint(equalTo: topAnchor, constant: spacing),
hintLabel.bottomAnchor.constraint(equalTo: nextLabel.topAnchor, constant: -spacing),
hintLabel.leftAnchor.constraint(equalTo: leftAnchor, constant: spacing),
hintLabel.rightAnchor.constraint(lessThanOrEqualTo: rightAnchor, constant: -spacing),
nextLabel.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -spacing),
nextLabel.rightAnchor.constraint(equalTo: rightAnchor, constant: -spacing)
])
case .bottomLeft:
labelStackView.addSubview(hintLabel)
labelStackView.addSubview(nextLabel)

NSLayoutConstraint.activate([
hintLabel.topAnchor.constraint(equalTo: topAnchor, constant: spacing),
hintLabel.bottomAnchor.constraint(equalTo: nextLabel.topAnchor, constant: -spacing),
hintLabel.leftAnchor.constraint(equalTo: leftAnchor, constant: spacing),
hintLabel.rightAnchor.constraint(lessThanOrEqualTo: rightAnchor, constant: -spacing),
nextLabel.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -spacing),
nextLabel.leftAnchor.constraint(equalTo: leftAnchor, constant: spacing)
])
case .none:
labelStackView.addArrangedSubview(hintLabel)
labelStackView.addArrangedSubview(separator)
labelStackView.addArrangedSubview(nextLabel)

separator.heightAnchor.constraint(equalTo: labelStackView.heightAnchor,
constant: -10).isActive = true
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add the .leading case as well!

Suggested change
case .none:
labelStackView.addArrangedSubview(hintLabel)
labelStackView.addArrangedSubview(separator)
labelStackView.addArrangedSubview(nextLabel)
separator.heightAnchor.constraint(equalTo: labelStackView.heightAnchor,
constant: -10).isActive = true
case .trailing:
labelStackView.addArrangedSubview(hintLabel)
labelStackView.addArrangedSubview(separator)
labelStackView.addArrangedSubview(nextLabel)
separator.heightAnchor.constraint(equalTo: labelStackView.heightAnchor,
constant: -10).isActive = true
case .leading:
labelStackView.addArrangedSubview(nextLabel)
labelStackView.addArrangedSubview(separator)
labelStackView.addArrangedSubview(hintLabel)
separator.heightAnchor.constraint(equalTo: labelStackView.heightAnchor,
constant: -10).isActive = true

}
}

func initializeAccessibilityIdentifier() {
Expand Down
12 changes: 7 additions & 5 deletions Sources/Instructions/Helpers/Public/CoachMarkHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,16 @@ public class CoachMarkHelper {
public func makeDefaultCoachViews(
withArrow arrow: Bool = true,
withNextText nextText: Bool = true,
arrowOrientation: CoachMarkArrowOrientation? = .top
arrowOrientation: CoachMarkArrowOrientation? = .top,
nextLabelPosition: CoachMarkNextLabelPosition? = .none
) -> (bodyView: CoachMarkBodyDefaultView, arrowView: CoachMarkArrowDefaultView?) {

var coachMarkBodyView: CoachMarkBodyDefaultView

if nextText {
coachMarkBodyView = CoachMarkBodyDefaultView()
coachMarkBodyView = CoachMarkBodyDefaultView(nextLabelPosition: nextLabelPosition)
} else {
coachMarkBodyView = CoachMarkBodyDefaultView(hintText: "", nextText: nil)
coachMarkBodyView = CoachMarkBodyDefaultView(hintText: "", nextText: nil, nextLabelPosition: .none)
}

var coachMarkArrowView: CoachMarkArrowDefaultView?
Expand All @@ -61,9 +62,10 @@ public class CoachMarkHelper {
withArrow arrow: Bool = true,
arrowOrientation: CoachMarkArrowOrientation? = .top,
hintText: String,
nextText: String? = nil
nextText: String? = nil,
nextLabelPosition: CoachMarkNextLabelPosition? = .none
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't need to be optional in my opinion, because there's always going to be a position.

Suggested change
nextLabelPosition: CoachMarkNextLabelPosition? = .none
nextLabelPosition: CoachMarkNextLabelPosition = .trailing

) -> (bodyView: CoachMarkBodyDefaultView, arrowView: CoachMarkArrowDefaultView?) {
let coachMarkBodyView = CoachMarkBodyDefaultView(hintText: hintText, nextText: nextText)
let coachMarkBodyView = CoachMarkBodyDefaultView(hintText: hintText, nextText: nextText, nextLabelPosition: nextLabelPosition)

var coachMarkArrowView: CoachMarkArrowDefaultView?

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
public enum CoachMarkNextLabelPosition {
case topRight
case topLeft
case bottomRight
case bottomLeft
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest using the leading/trailing terminology, so that it's direction-agnostic. Let's add leading & trailing in it as well!

Suggested change
case topRight
case topLeft
case bottomRight
case bottomLeft
case leading
case trailing
case topTrailing
case topLeading
case bottomTrailing
case bottomLeading

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @ephread! Thanks for the feedback!
The leading/trailing terminology seems good to me!
I have made some changes at 89584d2, feel free to check it out. :)

}