zach wilcox
zach wilcox

Reputation: 75

NSMutableAttributedString text go down when reaches right anchor

I am trying to have a NSMutableAttributedString return multiple lines when the text reaches the width.

example

I thought I could use numberOfLines but that's only accessible with UILabel?

The text "Community Guidelines" and "Terms & Conditions" ideally should both go under "By checking this box." How can I do this?

the code I am using to create the attributedSting is

func setupView() {

    let color = UIColor.white
    let font = UIFont.systemFont(ofSize: 20)
    let label = UIButton(type: .system)
    label.backgroundColor = GREEN_Theme
    let attributedTitle = NSMutableAttributedString(string:
    "By checking this box, you agree to our", attributes: [NSAttributedString.Key.foregroundColor:
    color, NSAttributedString.Key.font : font])
    
    attributedTitle.append(NSAttributedString(string: "Community Guidelines", attributes: [NSAttributedString.Key.foregroundColor: UIColor.white, NSAttributedString.Key.font: font]))
    label.addTarget(self, action: #selector(communityGuideLinesAction), for: .touchUpInside)
    label.setAttributedTitle(attributedTitle, for: .normal)
    
    view.addSubview(label)
    let gesture = UITapGestureRecognizer(target: self, action: #selector(didTapCheckBox))
    checkbox.addGestureRecognizer(gesture)

    
    label.anchors(top: registerButton.bottomAnchor, topPad: 0, bottom: nil, bottomPad: 0, left: checkbox.rightAnchor, leftPad: 10, right: nil, rightPad: 0, height: 60, width: 300)
 }

I am trying to have the text above, resemble the text below but I don't know how to achieve this.

image 2

After making changes to how I chose to constraint the Attributed, string,

This is what I see.

image 2

Upvotes: 0

Views: 185

Answers (1)

Menaim
Menaim

Reputation: 1014

I think it depends on your design & constrains not code, as you need to make a label with multilines by setting its numberOflines to 0 + giving the label constrains: Trailing, leading, bottom & top

Upvotes: 0

Related Questions