0

我使用UILabel'sattributedText来加载 HTML 字符串,例如:

<p style="text-align: center;">sometext</p>

我用它NSParagraphStyle来改变line-height这个 HTML 的所有元素。

NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
paragraphStyle.minimumLineHeight = 20; // line-height: 20;

[attributedString addAttribute:NSParagraphStyleAttributeName
                         value:paragraphStyle
                         range:NSMakeRange(0, attributedString.length)];

有用。但它将重置text-align为左。

4

1 回答 1

0

属性就像字典一样工作:定义范围内的键/值。键具有唯一性,因此您在不复制其先前样式的情况下覆盖该值。

要执行您想要的操作,您需要枚举属性字符串以查找NSParagraphStyleAttributeName并在必要时对其进行修改。

[attributedString enumerateAttribute:NSParagraphStyleAttributeName inRange:NSMakeRange(0, [attributedString length]) options:0 usingBlock:^(id  _Nullable value, NSRange range, BOOL * _Nonnull stop) {
    if ([value isKindOfClass:[NSParagraphStyle class]]) {
        NSMutableParagraphStyle *style = [value mutableCopy];
        style.minimumLineHeight = 20;
        [attributedString addAttribute:NSParagraphStyleAttributeName value:style range:range];
    }
}];
于 2018-06-15T09:26:29.853 回答