我正在尝试使用 attributeText 在 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.
最佳答案
我已经使用正确的细节样式单元格(@vadian adivsed)解决了这个问题。
let cell = UITableViewCell(style: .value1, reuseIdentifier: "CellId")
谢谢!
关于ios - 具有两种不同文本对齐方式的 NSAttributedString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47839156/