Aly*_*hak 5 html nsattributedstring uilabel ios uistackview
我有一些表格视图单元格需要显示我们服务器上的一些简单html。将html转换为NSAttributedString,以便可以将其显示在单元格上的UILabel对象中。但是当表格视图绘制单元格时,UILabel对象似乎在它们的末尾添加了一些换行符。请注意此处的额外空间:
(顺便说一句,图片提供了两个tableview单元,因为我尝试了两种不同形式的html,并且在两种情况下似乎都插入了换行符)我认为这可能是由于我的布局所致,也许UILabel被迫被其高度布局,并且不能根据分配的文本自行调整大小/缩小。但是,当我为同一标签提供一个简单的NSAttributedString时,它就是这样创建的:
sRet = [[NSAttributedString alloc] initWithString:@"[Dummy text string 111]"];
Run Code Online (Sandbox Code Playgroud)
(黄色)UILabel确实会像这样缩小响应:
几乎表明我的布局允许UILabel根据分配给它的文本自由调整大小。
下面是当我将它们记录到控制台时分配给标签的HTML及其结果NSAttributedString值。这些对应于您在上方第一张图片中的黄色UILabel对象中看到的内容。这是将html转换为属性字符串并分配的html:
<h2>Student did poorly</h2>
<p><span style="font-family:comic sans ms,cursive;"><span style="font-size:14px;">Student did ok</span></span></p>
Run Code Online (Sandbox Code Playgroud)
这是对应的属性字符串:
Student did poorly
{
NSColor = "kCGColorSpaceModelRGB 0 0 0 1 ";
NSFont = "<UICTFont: 0x7fce3f8798e0> font-family: \"TimesNewRomanPS-BoldMT\"; font-weight: bold; font-style: normal; font-size: 18.00pt";
NSKern = 0;
NSParagraphStyle = "Alignment 4, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 22/0, LineHeightMultiple 0, LineBreakMode 0, Tabs (\n), DefaultTabInterval 36, Blocks (null), Lists (null), BaseWritingDirection 0, HyphenationFactor 0, TighteningForTruncation NO, HeaderLevel 2";
NSStrokeColor = "kCGColorSpaceModelRGB 0 0 0 1 ";
NSStrokeWidth = 0;
}
Student did ok{
NSColor = "kCGColorSpaceModelRGB 0 0 0 1 ";
NSFont = "<UICTFont: 0x7fce3d5a96c0> font-family: \"Snell Roundhand\"; font-weight: normal; font-style: normal; font-size: 14.00pt";
NSKern = 0;
NSParagraphStyle = "Alignment 4, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 19/0, LineHeightMultiple 0, LineBreakMode 0, Tabs (\n), DefaultTabInterval 36, Blocks (\n), Lists (\n), BaseWritingDirection 0, HyphenationFactor 0, TighteningForTruncation NO, HeaderLevel 0";
NSStrokeColor = "kCGColorSpaceModelRGB 0 0 0 1 ";
NSStrokeWidth = 0;
}
{
NSColor = "kCGColorSpaceModelRGB 0 0 0 1 ";
NSFont = "<UICTFont: 0x7fce3d7959e0> font-family: \"Times New Roman\"; font-weight: normal; font-style: normal; font-size: 12.00pt";
NSKern = 0;
NSParagraphStyle = "Alignment 4, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 15/0, LineHeightMultiple 0, LineBreakMode 0, Tabs (\n), DefaultTabInterval 36, Blocks (\n), Lists (\n), BaseWritingDirection 0, HyphenationFactor 0, TighteningForTruncation NO, HeaderLevel 0";
NSStrokeColor = "kCGColorSpaceModelRGB 0 0 0 1 ";
NSStrokeWidth = 0;
}
Run Code Online (Sandbox Code Playgroud)
在html和属性字符串中,我都没有看到UILabel对象具有额外垂直空间的原因。首先,每个单元格上的一对UILabel对象(黄色和白色)都包含在UIStackView中,因此也许它可能以某种方式作弊。但是只有四个约束条件,即尾随,前导,底部和顶部,并且如上所述,可以对标签进行适当地调整大小,因此这些约束似乎对此没有帮助。
我遇到了同样的问题,需要删除我的<p>标签添加的不需要的换行符,并且无法在服务器端进行更改。所以我首先提出了这个解决方案:
迅速
while !attributedString.string.isEmpty //Validates if the attributed string has at least one character
&& CharacterSet.newlines.contains(attributedString.string.unicodeScalars.last!) //Compares if the last unicode character of the attributed string is inside the `CharacterSet` of newlines
{
attributedString.deleteCharacters(in: NSRange(location: attributedString.length - 1, length: 1)) //Deletes the last character of the attributed string
}
Run Code Online (Sandbox Code Playgroud)
Obj-C(未测试)
while ([attributedString.string length] > 0
&& [[attributedString.string substringFromIndex:[attributedString.string length] - 1] rangeOfCharacterFromSet:NSCharacterSet.newlineCharacterSet].location != NSNotFound)
{
[attributedString deleteCharactersInRange:NSMakeRange([attributedString length] - 1, 1)];
}
Run Code Online (Sandbox Code Playgroud)
(我在将 html 代码转换为属性字符串后立即执行此代码。)
但是我可以从答案中看到<h#>标签也添加了换行符。因此,对于扩展解决方案,可能类似于为每对标签创建一个属性字符串;应用上面的过滤器;之后,将它们加入一个属性字符串中或将它们显示在不同的标签中。
编辑
我使用 的扩展String名将 HTML 代码转换为NSMutableAttributedString:
extension String {
/// Returns a new attributed string loading any HTML tags that the receiver contains.
///
/// If the HTML code is malformed or can not format it, this method returns the receiver but as a `NSMutableAttributedString`. If the formated resulting string contains one or more newlines at the end, they are removed.
func htmlFormat() -> NSMutableAttributedString {
var attributedString = NSMutableAttributedString(string: self, attributes: [.font : UIFont.preferredFont(forTextStyle: .body)])
if let data = data(using: .utf8),
let formatedString = try? NSMutableAttributedString(data: data,
options: [.documentType: NSAttributedString.DocumentType.html,
.characterEncoding: String.Encoding.utf8.rawValue],
documentAttributes: nil) {
attributedString = formatedString
}
while !attributedString.string.isEmpty
&& CharacterSet.newlines.contains(attributedString.string.unicodeScalars.last!) {
attributedString.deleteCharacters(in: NSRange(location: attributedString.length - 1, length: 1))
}
return attributedString
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
207 次 |
| 最近记录: |