【问题标题】:How to add dot view to the text in swift?如何快速将点视图添加到文本中?
【发布时间】:2021-11-14 20:47:19
【问题描述】:

我曾使用 UIStackView 进行单元格设计。现在我必须在文本“Inprocess”旁边添加一个黄点视图。我们该怎么做?

let backView = UIView(color: AppColor.appWhite, cornerRadius: 18)

lazy var advanceStack = VerticalStackView(arrangedSubViews: [SalaryadvanceLabel, $99advanceValue], spacing: 3, distribution: .fillEqually)
lazy var StatusStack = VerticalStackView(arrangedSubViews: [Inprocesslabel, timelabel], spacing: 3, distribution: .fillEqually)
    
lazy var infoStack = HorizontalStackView(arrangedSubViews: [advanceStack, StatusStack], spacing: 10, distribution: .fillEqually)

let indicator: UIView = {
    let view = UIView(color: AppColor.appGreen, cornerRadius: 3)
    view.constraintWidth(constant: 6)
    view.constraintHeight(constant: 6)
    return view
}()
    
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    backgroundColor = AppColor.appBackground
    selectionStyle = .none
    setupView()
    indicator.isHidden = true
}

private func setupView() {
    addSubview(backView)
    backView.anchor(top: topAnchor, leading: leadingAnchor, bottom: bottomAnchor, trailing: trailingAnchor, padding: .init(top: 8, left: 12, bottom: 0, right: 12))

    backView.addSubview(infoStack)
    infoStack.anchor(top: nil, leading: backView.leadingAnchor, bottom: nil, trailing: backView.trailingAnchor, padding: .init(top: 0, left: 20, bottom: 0, right: 20))
    infoStack.centerYInSuperview()

    backView.addSubview(indicator)
    indicator.anchor(top: infoStack.bottomAnchor, leading: nil, bottom: nil, trailing: chargeValue.trailingAnchor, padding: .init(top: 6, left: 0, bottom: 0, right: 0))
}

【问题讨论】:

  • 许多可能性:为带有点的标签使用属性字符串;创建一个带有彩色点和标签的自定义视图,并将其添加到您的垂直堆栈视图中;创建一个带有点和标签的水平堆栈视图,并将其添加到垂直堆栈视图; ....

标签: ios swift iphone


【解决方案1】:

我使用以下代码并将其添加到 UIStackView。问题解决了。

let StatusDot = UILabel(text: "\u{2022}" , color: AppColor.appBlack, font: Helvetica.bold, size: 40, alignment: .right)

【讨论】:

    【解决方案2】:

    这就是你如何使用带有点状字符的attributedText 来做到这一点(正如 cmets 中已经建议的那样)

    let attributedText = NSMutableAttributedString(string: "• In process")
    let attributes: [NSAttributedString.Key : Any] = [
        .foregroundColor: UIColor.systemYellow
    ]
    attributedText.addAttributes(attributes, range: NSRange(location: 0, length: 1))
    
    
    inProcessLabel.attributedText = attributedText
    

    【讨论】:

      猜你喜欢
      • 2020-10-06
      • 2017-12-25
      • 1970-01-01
      • 2021-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多