我有一个标签,我给它添加了一个属性字符串。该字符串是,即使行数为0并且没有高度约束,为什么我的标签会被截断?
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
}
我给约束到这样的标签,
但是,当我不使用这行代码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
存在问题,如果不是,我想知道问题是什么。有人能帮助我吗?
怎么一回事,因为这让superscriptAttributedString = attributedText 让段= NSMutableParagraphStyle() paragraph.lineBreakMode = .byTruncatingTail superscriptAttributedString.addAttribute(NSAttributedStringKey.paragraphStyle,值:段落,范围:NSMakeRange(0,superscriptAttributedString.length) )你的标签可以放入行数 –
我没有得到你所说的(你的标签可以放入行数?)。你能否详细说明一下。 –
NSMutableParagraphStyle这意味着你的单行转换成多个,所以如果你想文本即时单行不要使用NSMutableParagraphStyle。 –