我正在尝试使用attributionText在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.,我收到了
卖家脸书公司
我已经使用@vadian所提供的正确细节样式的细胞解决了这个问题。
let cell = UITableViewCell(style: .value1, reuseIdentifier: "CellId")
谢谢 !