我正在尝试使用属性文本在 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.,我得到了
卖家Facebook Inc.