ios - 具有两种不同文本对齐方式的 NSAttributedString

标签 ios swift nsmutableattributedstring nsattributedstringkey

我正在尝试使用 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.

img_5752

最佳答案

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

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

谢谢!

关于ios - 具有两种不同文本对齐方式的 NSAttributedString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47839156/

相关文章:

iphone - 在 iOS 5.1 中创建具有不同样式文本的 UILabel,而不使用 Storyboard

ios - iPhone 应用程序从事件状态过渡到非事件状态

ios - 使用带有浮点值的 dispatch_time

ios - SecAccessControl 没有成员 takeRetainedValue

ios - 在框架中找不到类

ios - 每当 UITextView 更改时,如何从空的 attributedString 开始?

ios - 如何使用 NSMutableAttributedString 在我的 UITextView 中加粗一些单词?

iphone - 存储静态位置和信息 iOS

swift - 变量 'xxx' 从未发生变化,考虑更改为 'let'

ios - 文本下划线颜色与 iOS 中的文本颜色不同