例如,我使用UILabel的attributedText来加载HTML:
<p style="text-align: center;">sometext</p>我使用NSParagraphStyle来更改这个HTML的所有元素的line-height。
NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
paragraphStyle.minimumLineHeight = 20; // line-height: 20;
[attributedString addAttribute:NSParagraphStyleAttributeName
value:paragraphStyle
range:NSMakeRange(0, attributedString.length)];它起作用了。但是它会将text-align重置为左。
发布于 2018-06-15 09:26:29
属性的工作方式类似于字典:键/值在定义的范围内。键是统一的,所以您要重写值,而不复制其以前的样式。
要做您想做的事情,您需要枚举属性化字符串,查找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];
}
}];https://stackoverflow.com/questions/50872280
复制相似问题