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

标签 html ios uilabel nsattributedstring uistackview

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

enter image description here

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

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

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

enter image description here

几乎可以证明我的布局允许 UILabel 根据分配给它的文本自由调整大小。

下面是分配给标签的 html 以及当我将它们记录到控制台时生成的 NSAttributedString 值。这些对应于您在上面第一张图片中的黄色 UILabel 对象中看到的内容。这是被转换为属性字符串并分配的 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 对象具有额外垂直空间的原因。 Fwiw,每个单元格(黄色和白色)上的一对 UILabel 对象都包含在 UIStackView 中,所以它可能以某种方式造成了恶作剧。但是只有四个约束,尾随、前导、底部和顶部,并且如上所述,标签可以很好地调整大小,因此这些约束似乎对此没有贡献。

最佳答案

我遇到了同样的问题,需要删除我的 <p> 添加的不需要的换行符标签并且无法在服务器端更改内容。所以我首先想到了这个解决方案:

swift

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
    }

}

关于html - 如何从 iOS UILabel 对象中的 NSAttributedString(从 HTML 转换而来)中消除多余的换行符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53272720/

相关文章:

html - div 覆盖在文本 p 标签上

html - 如何仅在某些页面上隐藏我的左侧导航菜单

html - 无法在字符串 'validator' 上创建属性 'userForm' - 使用组件工厂

ios - 允许 UITableViewCell 上的手势落入底层 View

ios - iPhone 模拟器 - 黑屏

ios - 根据框架调整 View 的边界

Swift - 多个 UILabel 单击事件到同一功能

javascript - 为什么 requests.get() 使用 Python 检索的 HTML 与浏览器不同?

ios - Swift - 无法通过导航 Controller 中的自定义选项卡寻呼机传递数据

ios - UILabel 无法成为第一响应者并在 UITableView 中显示 inputView