【问题标题】:Why my label is truncating even though the number of lines is 0 and no height constraint?为什么即使行数为 0 且没有高度限制,我的标签也会被截断?
【发布时间】:2017-09-22 07:14:01
【问题描述】:

我有一个标签,我向它添加了一个属性字符串。字符串是,

let nameText = "My name is Shreesha and Im an iOS developer. My name is Shreesha and Im an iOS developer."`My name is Shreesha and Im an iOS developer. My name is Shreesha and Im an iOS developer.` 

在此文本中,我尝试在文本开头添加*,因此我使用了属性字符串,代码如下所示,

func attributedTextForFeeApplies() -> NSAttributedString {
    let attributedText = NSMutableAttributedString(string: "* " + nameText)
    attributedText.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColor.blue, range: NSMakeRange(0, attributedText.length))
    attributedText.addAttribute(NSAttributedStringKey.font, value: UIFont.systemFont(ofSize: 10), range: NSMakeRange(0, attributedText.length))

    let superScriptString = "* "

    attributedText.addAttribute(NSAttributedStringKey.baselineOffset, value: 2, range: NSMakeRange(0, superScriptString.characters.count))
    attributedText.addAttribute(NSAttributedStringKey.font, value: UIFont.systemFont(ofSize: 9), range: NSMakeRange(0, superScriptString.characters.count))
    attributedText.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColor.blue, range: NSMakeRange(0, superScriptString.characters.count))

    let superscriptAttributedString = attributedText
    let paragraph = NSMutableParagraphStyle()
    paragraph.lineBreakMode = .byTruncatingTail
    superscriptAttributedString.addAttribute(NSAttributedStringKey.paragraphStyle, value: paragraph, range: NSMakeRange(0, superscriptAttributedString.length))

    return superscriptAttributedString
}

我这样给Label设置了约束,

即使我将行数设置为 0 并且没有高度限制,标签也会像这样截断,

但是当我不使用这行代码 attributedText.addAttribute(NSAttributedStringKey.baselineOffset, value: 2, range: NSMakeRange(0, superScriptString.characters.count)) 时,它可以正常工作,

如果在中间添加*(不删除attributedText.addAttribute(NSAttributedStringKey.baselineOffset, value: 2, range: NSMakeRange(0, superScriptString.characters.count))),它可以正常工作,但如果我在文本开头使用它就不起作用,

屏幕截图:

如果我增加字体大小,它也可以工作。

我认为NSAttributedString 存在问题,如果不是,我想知道问题所在。谁能帮帮我。

【问题讨论】:

  • 因此 let superscriptAttributedString = attributesText let paragraph = NSMutableParagraphStyle() paragraph.lineBreakMode = .byTruncatingTail superscriptAttributedString.addAttribute(NSAttributedStringKey.paragraphStyle, value: paragraph, range: NSMakeRange(0, superscriptAttributedString.length))您的标签可以放入行数
  • 我没听懂你说的(你的标签可以写成行数吗?)。能否请您详细说明。
  • NSMutableParagraphStyle 这意味着您的单行转换为多行,因此如果您想在单行中输入文本,请不要使用 NSMutableParagraphStyle 。
  • 删除这一行 let superscriptAttributedString = attributesText let paragraph = NSMutableParagraphStyle() paragraph.lineBreakMode = .byTruncatingTail superscriptAttributedString.addAttribute(NSAttributedStringKey.paragraphStyle, value: paragraph, range: NSMakeRange(0, superscriptAttributedString.length) ) 并检查您的输出
  • 我实施了您的建议,但没有成功。

标签: ios uilabel nsattributedstring


【解决方案1】:

您的问题是,一旦您分配了属性字符串,您就必须重新计算高度。但是有一个快速解决方法,您无需自己计算。在标签末尾给标签一个强制换行符,在这种情况下,它必须自动重新计算高度。

// see the \n at the end of your string that will cause the label to recalculate it's height.
let nameText = "My name is Shreesha and Im an iOS developer. My name is Shreesha and Im an iOS developer."`My name is Shreesha and Im an iOS developer. My name is Shreesha and Im an iOS developer. \n"

【讨论】:

  • 我的问题是即使使用当前的实现它也可以工作。但似乎NSAttributedStringUILabel 存在问题。而且我不想在当前实现应该工作的地方快速修复。但是现在我没有在应用程序中添加任何属性字符串,只是为了尽快发布构建。是的,你的建议有效。但我想知道问题出在哪里,并且我希望它能够在没有任何调整的情况下与当前实现一起工作。
  • 我已经向您解释过 Xcode 有一个错误,如果您尝试更改任何字符的偏移量,它无法计算属性字符串的确切大小。在这种情况下,您必须根据您的字符串计算标签的高度。
猜你喜欢
  • 2020-01-03
  • 1970-01-01
  • 2018-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多