如何消除iOS UILabel对象中NSAttributedString(从HTML转换)的多余换行符?

时间:2018-11-13 02:04:28

标签: html ios uilabel nsattributedstring uistackview

我有一些表视图单元格,它们需要显示来自我们服务器的一些简单html。将html转换为NSAttributedString,以便可以将其显示在单元格上的UILabel对象中。但是当表格视图绘制单元格时,UILabel对象似乎在它们的末尾添加了一些换行符。注意此处的额外空间:

enter image description here

(顺便说一句,图片提供了两个tableview单元,因为我尝试了两种不同形式的html,并且在两种情况下似乎都插入了换行符) 我认为也许是由于我的布局所致,也许UILabel被布局所逼到其高度,而不能随意根据其分配的文本调整大小/缩小自身。但是,当我为同一标签提供一个简单的NSAttributedString时,会以这种方式创建:

 sRet = [[NSAttributedString alloc] initWithString:@"[Dummy text string 111]"];

(黄色)UILabel确实像这样缩小响应:

enter image description here

充分表明我的布局允许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>

这是对应的属性字符串:

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;
}

在html和属性字符串中,我都没有看到UILabel对象具有额外垂直空间的原因。首先,每个单元格上的一对UILabel对象(黄色和白色)都包含在UIStackView中,因此也许它可能以某种恶作剧。但是只有四个约束条件,即尾随,前导,底部和顶部,并且如上所述,可以对标签进行适当地调整大小,因此这些约束似乎对此没有帮助。

1 个答案:

答案 0 :(得分:4)

我遇到了同样的问题,需要删除我的<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
}

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)];
}

(我将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
    }

}