iPhone的UITextView设置行距
我怎样才能增加UITextView行空间,使它看起来像iPhone中的“Notes”应用程序?
那么现在在iOS6上,使用NSParagraphStyle是有可能的,但是它的logging很差,似乎很less工作。
即时通讯目前正在这样的工作:
UITextView *lab = [LocalTexts objectAtIndex:j]; NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; paragraphStyle.lineHeightMultiple = 50.0f; paragraphStyle.maximumLineHeight = 50.0f; paragraphStyle.minimumLineHeight = 50.0f; NSString *string = lab.text; NSDictionary *ats = @{ NSFontAttributeName : [UIFont fontWithName:@"DIN Medium" size:16.0f], NSParagraphStyleAttributeName : paragraphStyle, }; lab.attributedText = [[NSAttributedString alloc] initWithString:string attributes:ats];
问题是,当你设置字体,行高停止工作..很奇怪,还没有find一个修复它呢。
你也可以创build一个自定义的Attributed CoreText视图…但是它有点技术性,你可以在这里find一个演示
以及我希望有所帮助。
要更改行距:
NSString *textViewText =self.myTextView.text; NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:textViewText]; NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; paragraphStyle.lineSpacing = 30; NSDictionary *dict = @{NSParagraphStyleAttributeName : paragraphStyle }; [attributedString addAttributes:dict range:NSMakeRange(0, [textViewText length])]; self.myTextView.attributedText = attributedString;
查看UITextView的文档就足以确定该控件不支持更改行间距。
对于iOS 6及以上版本:
有可能使用NSParagraphStyle,
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; paragraphStyle.lineHeightMultiple = 50.0f; paragraphStyle.maximumLineHeight = 50.0f; paragraphStyle.minimumLineHeight = 50.0f; NSString *string = @"your paragraph here"; NSDictionary *attribute = @{ NSParagraphStyleAttributeName : paragraphStyle, }; [textview setFont:[uifont fontwithname:@"Arial" size:20.0f]]; textview.attributedText = [[NSAttributedString alloc] initWithString:string attributes:attribute];
对于Swift 2.2
let paragraphStyle: NSMutableParagraphStyle = NSMutableParagraphStyle() paragraphStyle.lineHeightMultiple = 20.0 paragraphStyle.maximumLineHeight = 20.0 paragraphStyle.minimumLineHeight = 20.0 let ats = [NSFontAttributeName: UIFont(name: "Helvetica Neue", size: 11.0)!, NSParagraphStyleAttributeName: paragraphStyle] cell.textView.attributedText = NSAttributedString(string: "you string here", attributes: ats)
在上面的几个答案中,属性lineHeightMultiple
被滥用:
paragraphStyle.lineHeightMultiple = 50.0f;
以下是官方文档lineHeightMultiple
是一个乘数,而不是string的绝对高度:
在被最小和最大线高度约束之前,接收器的自然线高度乘以该因子(如果为正)。 该属性的默认值是0.0。 https://developer.apple.com/library/prerelease/ios/documentation/Cocoa/Reference/ApplicationKit/Classes/NSParagraphStyle_Class/index.html#//apple_ref/occ/instp/NSParagraphStyle/maximumLineHeight
因此,下面的代码:
paragraphStyle.lineHeightMultiple = 50.0f; paragraphStyle.maximumLineHeight = 50.0f; paragraphStyle.minimumLineHeight = 50.0f;
相当于
paragraphStyle.lineHeight = 50.0f
如果最终结果是增加行间距,则可以直接在界面构build器中完成。 将Text属性设置为“Attributed”,然后单击右侧的…。 在那里设置间距属性应该正确地更新行距。
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; paragraphStyle.lineHeightMultiple = 50.0f; paragraphStyle.maximumLineHeight = 50.0f; paragraphStyle.minimumLineHeight = 50.0f; NSString *string = @"if you want reduce or increase space between lines in uitextview ,you can do this with this,but donot set font on this paragraph , set this on uitextveiw."; NSDictionary *ats = @{ NSParagraphStyleAttributeName : paragraphStyle, }; [textview setFont:[uifont fontwithname:@"Arial" size:20.0f]]; textview.attributedText = [[NSAttributedString alloc] initWithString:string attributes:ats];