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 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
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 var nextLabelPosition: CoachMarkNextLabelPosition = .trailing
private let spacing: CGFloat = 18

public var background: CoachMarkBodyBackgroundStyle {
get { return bodyBackground }
Expand All @@ -44,7 +46,9 @@ public class CoachMarkBodyDefaultView: UIControl,
initializeViewHierarchy()
}

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

super.init(frame: frame)
initializeViewHierarchy()

Expand All @@ -55,12 +59,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(hintText: String, nextText: String?, nextLabelPosition: CoachMarkNextLabelPosition) {
self.init(frame: CGRect.zero, hintText: hintText, nextText: nextText, nextLabelPosition: nextLabelPosition)
}

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

required public init?(coder aDecoder: NSCoder) {
Expand All @@ -82,12 +93,68 @@ 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 .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
case .topTrailing:
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 .topLeading:
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 .bottomTrailing:
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 .bottomLeading:
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)
])
}
}

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 = .trailing
) -> (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: nextLabelPosition)
}

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 = .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,14 @@
// Copyright (c) 2015-present Frédéric Maquin <[email protected]> and contributors.
// Licensed under the terms of the MIT License.

/// Available positions for the nextLabel.
/// A nextLabel can either be positioned right beside the hintLabel (.trailing / .leading) with a separator in between, or
/// be positioned at the corner of the body (.topTrailing / .topLeading / .bottomTrailing / .bottomLeading) without a separator.
public enum CoachMarkNextLabelPosition {
case trailing
case leading
case topTrailing
case topLeading
case bottomTrailing
case bottomLeading
}