Fis*_*man 5 html nsattributedstring uilabel ios autolayout
我对标签中的属性文本有疑问,该文本占用了底部的大量空间。当我使用未归属的尺寸时,尺寸还可以,但现在我总是有底部空间 - 在图像上标记为粉红色。我尝试在 Reveal.app 中更改高度约束的值(由 iOS 生成),当我将其设置为比实际小一点时,文本就会被截断。
\n\n我需要属性标签,因为我正在从 HTML 字符串创建属性:
\n\n<p style=\\"font-family: EncodeSans-Thin; font-size: 10.0; color: #1f2e3b;\\">\n Od 2004 roku wspieramy organizacje biznesowe w rozwoju sztuki zarz\xc4\x85dzania projektami. \n Nasza dzia\xc5\x82alno\xc5\x9b\xc4\x87 opiera si\xc4\x99 na trzech filarach \xe2\x80\x93 \n <span style=\\"font-family: EncodeSans-Light\\">\n doradztwie, realizacji projekt\xc3\xb3w na zlecenie oraz szkoleniach.\n </span>\n</p>\nRun Code Online (Sandbox Code Playgroud)\n\n\n\n你遇到过这样的问题吗?
\n\n更新:
\n\n我正在使用下面的函数从 HTML 字符串(作为字符串扩展)生成 NSAttributedString。
\n\nfunc makeAttributesFromHTML() -> NSAttributedString {\n guard let htmlData = self.dataUsingEncoding(NSUTF8StringEncoding) else {\n return NSAttributedString()\n }\n\n do {\n let atributedString = try NSAttributedString(data: htmlData, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: NSUTF8StringEncoding], documentAttributes: nil)\n\n return atributedString\n } catch {\n return NSAttributedString()\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n
我以前肯定经历过这个。我发现一旦你在标签上设置了文本,你就必须使用更新它
label.attributedText = foo;
[label setNeedsLayout]; // this line normally isn't required
[label layoutIfNeeded];
Run Code Online (Sandbox Code Playgroud)
这应该将标签大小调整为正确的大小。如果失败,我不完全确定您的自动布局约束是如何设置的,但您可以手动计算标签的新高度并据此设置其高度约束。但这通常是没有必要的。
最后,文本末尾可能有尾随空格。如果您不需要,请务必将其删除。
| 归档时间: |
|
| 查看次数: |
3358 次 |
| 最近记录: |