NSParagraphStyle行间距被忽略 [英] NSParagraphStyle line spacing ignored

查看:443
本文介绍了NSParagraphStyle行间距被忽略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个失败的简单测试:用一个子视图(UITextView)创建一个新项目并将以下内容放入:

A simple test that is failed: Make a new project with just one subview (UITextView) and put the following in:

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineHeightMultiple = 50.f;
    paragraphStyle.lineSpacing = 100.f;
    paragraphStyle.minimumLineHeight = 200.f;
    paragraphStyle.maximumLineHeight = 500.f;

    UIFont *font = [UIFont fontWithName:@"AmericanTypewriter" size:24.f];

    self.textView.attributedText = [[NSAttributedString alloc] initWithString:
    @"This is a test.\n Will I pass?" attributes:
    @{NSParagraphStyleAttributeName : paragraphStyle, NSFontAttributeName : font}];
}

行间距与属性不存在时相同。有什么能成功地运作吗?我添加了荒谬的数字只是为了表明它不会改变......

Line spacing is the same as if the attribute were not there. Has anything got this to work successfully? I put in ridiculous numbers just to show that it won't change...

推荐答案

这是NSHTMLWriter中的一个错误,它是UITextView用于将attributedText转换为HTML的私有类。在内部,它通过UIWebDocumentView显示此HTML。在我的文章中阅读更多关于UITextView内部工作原理的内容: http:// www。 cocoanetics.com/2012/12/uitextview-caught-with-trousers-down/

This is a bug in NSHTMLWriter which is the private class which UITextView uses to convert attributedText into HTML. Internally it displays this HTML via a UIWebDocumentView. Read more on the inner workings of UITextView in my writeup here: http://www.cocoanetics.com/2012/12/uitextview-caught-with-trousers-down/

问题来自于字体CSS中容易遗漏的特色速记。如果您使用字体速记指定像素大小,则会将字体大小和行高设置为BOTH。由于NSHTMLWriter将字体放在行高后,这会导致行高被字体大小抵消。

The problem comes from an easy to miss speciality in the font CSS shorthand. If you specify a pixel size with the font shorthand then this sets BOTH the font-size as well as the line-height. Since NSHTMLWriter puts the font AFTER the line-height this causes the line-height to be cancelled out by the font size.

请参阅此处查看我的雷达,其中包括完整分析错误: http://www.cocoanetics .com / 2012/12 / radar-uitextview-ignores-minimummaximum-line-height-in-attributes-string /

See here for my Radar which includes the full analysis of the bug: http://www.cocoanetics.com/2012/12/radar-uitextview-ignores-minimummaximum-line-height-in-attributed-string/

我建议你提交bug报告并提及我的雷达#12863734。

I suggest you file a bug report as well and mention my Radar #12863734.

这篇关于NSParagraphStyle行间距被忽略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆