【问题标题】:NSMutableAttributedString text go down when reaches right anchorNSMutableAttributedString 文本到达右锚点时下降
【发布时间】:2021-03-24 13:11:50
【问题描述】:

我试图让 NSMutableAttributedString 在文本达到宽度时返回多行。

我以为我可以使用 numberOfLines 但这只能通过 UILabel 访问?

理想情况下,“社区指南”和“条款与条件”文本都应位于“通过选中此框”下方。我该怎么做?

我用来创建attributedSting 的代码是

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)
 }

我正在尝试使用上面的文字,类似于下面的文字,但我不知道如何实现。

在更改了我选择约束属性字符串的方式之后,

这是我看到的。

【问题讨论】:

    标签: ios swift nsmutableattributedstring nsmutablestring


    【解决方案1】:

    我认为这取决于您的设计和约束而不是代码,因为您需要通过将其 numberOflines 设置为 0 + 来制作带有多行的标签,并给出标签约束:尾随、前导、底部和顶部

    【讨论】:

    • 我尝试增加高度和减少宽度,但我仍然遇到同样的问题。我可以不使用 NSMutableAttributedString 来完成这个吗?
    • 使用尾随、前导、底部和顶部约束不固定宽度和高度
    • 我明白你在说什么。但我想将属性文本的顶部约束到按钮的底部,然后将文本的左侧约束到复选框
    • 我尝试使用您的建议,但它不起作用
    • 你为什么不把它做成一个普通的字符串,然后用插座做成它会起作用的
    猜你喜欢
    • 2015-10-07
    • 1970-01-01
    • 2011-02-28
    • 2021-05-24
    • 2012-08-25
    • 2014-07-13
    • 1970-01-01
    • 2022-01-07
    • 1970-01-01
    相关资源
    最近更新 更多