NSParagraphStyle行间距被忽略

bor*_*den 24 objective-c uitextview nsattributedstring ios6

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

- (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}];
}
Run Code Online (Sandbox Code Playgroud)

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

Coc*_*ics 24

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

问题来自于字体CSS简写中容易遗漏的特殊功能.如果您使用字体速记指定像素大小,则此设置字体大小以及行高.由于NSHTMLWriter将字体放在行高后,这会导致行高被字体大小抵消.

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

我建议您提交错误报告,并提及我的雷达#12863734.

  • 已在iOS 7中修复. (4认同)

nac*_*o4d 23

我不知道这是否足以满足您的需要,但我可以通过设置最小和最大线高来调整行间距.此外,为了使用字体,我将其放入font文本视图的属性中,而不是将其作为NSFontAttributeName属性字典中的值传递.(也许这部分没有(很好)记录?)

关于你的属性

lineSpacing从行的底部到上面行的底部计算,并且该空间被约束为minimumLineHeight和之间的值miximumLineHeight.我想说的是,你的属性中的某些值可能会取消覆盖其他值.

此外,如果你只需要调整线之间的间距你可能不需要使用paragraphStyle.lineHeightMultiple:)

代码

这对我有用:

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.minimumLineHeight = 35.f;
paragraphStyle.maximumLineHeight = 35.f;

UIFont *font = [UIFont fontWithName:@"AmericanTypewriter" size:18.f];
NSString *string = @"This is a test.\nWill I pass?\n?????????English\nEnglish y Español";
NSDictionary *attributtes = @{
    NSParagraphStyleAttributeName : paragraphStyle,
};
self.textView.font = font;
self.textView.attributedText = [[NSAttributedString alloc] initWithString:string
                                 attributes:attributtes];
Run Code Online (Sandbox Code Playgroud)

补充说明

似乎有日语/中文的情况,也许在同一行中混有字母字符的其他字符.它将使该线具有更大的前导来解决您需要像我一样设置最小和最大线高度.渲染没有属性的示例时,您可以看到问题.

  • 当我通过NSFontAttributeName设置字体时,行间距开始再次被忽略..这是一个大问题(没有其他方法可以在同一标签中混合样式).http://stackoverflow.com/questions/13298748/using-attributedtext-nsattributedstring-on-a-uitextview-on-ios6-only-works-wit?rq=1 (2认同)

Hen*_*rtz 9

设置maximumLineHeight似乎为我解决了这个问题;

CGFloat fontSize = 22.f;
titleLabel.font = [UIFont boldSystemFontOfSize:fontSize];
NSMutableParagraphStyle *paragraphStyle = [[[NSMutableParagraphStyle  alloc] init] autorelease];
paragraphStyle.maximumLineHeight = fontSize/2;
titleLabel.attributedText = [[[NSAttributedString alloc]
                              initWithString:@"This is a test.\nWill I pass?"
                              attributes: @{ NSParagraphStyleAttributeName : paragraphStyle,
                                             NSFontAttributeName : titleLabel.font}]
                             autorelease];
Run Code Online (Sandbox Code Playgroud)

重叠的线条


pro*_*vit 5

对于此特定字符串,您需要设置paragraphSpacing.有什么关系lineSpacing,我相信它在iOS上还没有得到支持.