2017-09-22 14 views
0

我有一个标签,我给它添加了一个属性字符串。该字符串是,即使行数为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 
} 

我给约束到这样的标签,

enter image description here

即使我设置的行数0无喜GHT约束标签被截断这样, enter image description here

但是,当我不使用这行代码attributedText.addAttribute(NSAttributedStringKey.baselineOffset, value: 2, range: NSMakeRange(0, superScriptString.characters.count))它是工作的罚款这样,

enter image description here

并且如果在添加*中间(不删除attributedText.addAttribute(NSAttributedStringKey.baselineOffset, value: 2, range: NSMakeRange(0, superScriptString.characters.count)))它工作正常,但如果我在文本的开始使用它只是不工作,

屏幕截图:

enter image description here

而且如果我增加字体大小,它也可以。

我认为NSAttributedString存在问题,如果不是,我想知道问题是什么。有人能帮助我吗?

+0

怎么一回事,因为这让superscriptAttributedString = attributedText 让段= NSMutableParagraphStyle() paragraph.lineBreakMode = .byTruncatingTail superscriptAttributedString.addAttribute(NSAttributedStringKey.paragraphStyle,值:段落,范围:NSMakeRange(0,superscriptAttributedString.length) )你的标签可以放入行数 –

+0

我没有得到你所说的(你的标签可以放入行数?)。你能否详细说明一下。 –

+0

NSMutableParagraphStyle这意味着你的单行转换成多个,所以如果你想文本即时单行不要使用NSMutableParagraphStyle。 –

回答

0

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

// 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" 
+0

我的问题是假设即使在当前的实施中也能工作。但似乎有'NSAttributedString'或'UILabel'的问题。而且我不想在当前实现应该工作的地方快速修复。但是现在我没有在应用程序中添加任何属性字符串,只是为了尽快释放构建。是的,你的建议很有效。但我想知道什么是问题,我希望它能够与当前的实现一起工作,无需任何调整。 –

+0

我已经向您解释过,如果您尝试更改任何字符的偏移量,Xcode有一个错误,它无法计算得到的字符串的确切大小。在这种情况下,您必须根据字符串计算标签的高度。 –

相关问题