【问题标题】:NSAttributedString with two different text alignments具有两种不同文本对齐方式的 NSAttributedString
【发布时间】:2017-12-15 20:03:51
【问题描述】:

我正在尝试使用属性文本在 UILabel 中显示一些文本,具有 2 种不同的文本对齐方式。我正在从 Firebase 获取值,它们是键和值类型。由于某种原因,我无法让标题左对齐而字幕右对齐。任何帮助表示赞赏。

 private func descriptionAttributedText() -> NSAttributedString {
    let title = "some title on the left side of the screen"
    let subtitle = "subtitle on the right side"

    let rightParagraph = NSMutableParagraphStyle()
    rightParagraph.alignment = .right

    let titleAttributes = [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 14), NSAttributedStringKey.foregroundColor: UIColor.black]
    let titleString = NSMutableAttributedString(string: title, attributes: titleAttributes)

    let subtitleAttributes = [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 13), NSAttributedStringKey.foregroundColor: UIColor.darkGray, NSAttributedStringKey.paragraphStyle: rightParagraph]
    let subtitleString = NSAttributedString(string: subtitle, attributes: subtitleAttributes)

    let paragraph = NSMutableParagraphStyle()
    paragraph.setParagraphStyle(.default)
    paragraph.lineSpacing = 2

    let range = NSMakeRange(0, titleString.string.count)
    titleString.addAttribute(NSAttributedStringKey.paragraphStyle, value: paragraph, range: range)

    titleString.append(subtitleString)


    return titleString
}

我得到的结果是“Titlesubtitle”类型

我看过这里,Attributed text with two text alignments,但这对我没有多大帮助。

我试图获得与 App Store 上相同的效果,但不是

卖家........... Facebook Inc.,我得到了

SellerFacebook Inc.

【问题讨论】:

  • 你为什么说它们没有对齐?你可以发布你得到的东西吗?
  • 我已经编辑了问题。
  • 我明白了,那么您无法以这种方式解决问题(NSAttributedString)。在图片中,可能有一个UITableView(也可能是一个自定义的UIVIew)和一个UITableViewCell(谈论第一个)有两个不同的UILabel
  • 这看起来像标准的 Right Detail 样式的单元格
  • 最好将它们分开 UILabel

标签: ios swift nsmutableattributedstring nsattributedstringkey


【解决方案1】:

我已经使用正确的细节样式单元格作为@vadian adivsed 解决了这个问题。

let cell = UITableViewCell(style: .value1, reuseIdentifier: "CellId")

谢谢!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-06
    • 2021-12-14
    • 2013-08-24
    • 2017-04-27
    • 2011-10-11
    • 1970-01-01
    • 2020-12-04
    相关资源
    最近更新 更多