在UITextView上设置NSAttributedString上的字体忽略行间距 [英] Setting font on NSAttributedString on UITextView disregards line spacing

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

问题描述

我正在尝试将属性字符串设置为iOS 6中的UITextView。问题是,如果我尝试在属性字符串上设置font属性,则会忽略行间距。但是,如果我没有设置字体,并使用默认字体,则行间距有效。

I'm trying to set an attributed string to a UITextView in iOS 6. The problem is, if I attempt to set the font property on the attributed string, the line spacing is ignored. However, if I don't set the font, and the default font is used, then line spacing works.

NSString *string = @" Hello \n world";
attrString = [[NSMutableAttributedString alloc] initWithString:string];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];

paragraphStyle.minimumLineHeight = 50;
// setting the font below makes line spacing become ignored
[attrString addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:20] range:NSMakeRange(0, string.length)];
[attrString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, string.length)];

mainTextView.attributedText = attrString;

知道发生了什么事吗?

推荐答案

Attributed字符串编程指南:

UIFont *font = [UIFont fontWithName:@"Palatino-Roman" size:14.0];
NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject:font
                                forKey:NSFontAttributeName];
NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:@"strigil" attributes:attrsDictionary];

更新:我尝试使用 addAttribute:在我自己的应用程序中的方法,但它似乎无法在iOS 6模拟器上工作:

Update: I tried to use addAttribute: method in my own app, but it seemed to be not working on the iOS 6 Simulator:

NSLog(@%@,textView。 attributedText);

日志似乎显示正确添加的属性,但iOS模拟器上的视图未显示属性。

The log seems to show correctly added attributes, but the view on iOS simulator was not display with attributes.

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

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