objective-c - cocoa NSTextField 行间距

标签 objective-c cocoa nstextfield nstextfieldcell

我正在努力解决一个非常简单的问题,我有几个 NSTextField(我现在不能使用 NSTextView),我需要更改显示文本的行间距。 我可以做些什么来减少行高或行间距?缩小字体大小不是一种选择。

非常感谢任何帮助!

周末愉快

!)

最佳答案

作为引用,您想阅读段落样式的描述:Cocoa Paragraph Styles并注意其中的所有内容都在行之间、段落之间、段落之前等处添加了额外的空间。您可以将 NSMutableParagraphStyle 中的值设置为零但不能更低。

要进一步缩小行间距,请使用 setMaximumLineHeight,感谢代码中的“6 1”(我添加了 setMaximumLineHeight):

NSString *title = @"title here";
NSFont *bold14 = [NSFont boldSystemFontOfSize:14.0];
NSColor *textColor = [NSColor redColor];
NSMutableParagraphStyle *textParagraph = [[NSMutableParagraphStyle alloc] init];
[textParagraph setLineSpacing:10.0];  // this sets the space BETWEEN lines to 10points
[textParagraph setMaximumLineHeight:12.0]; this sets the MAXIMUM height of the lines to 12points

NSDictionary *attrDic = [NSDictionary dictionaryWithObjectsAndKeys:bold14, NSFontAttributeName, textColor, NSForegroundColorAttributeName, textParagraph, NSParagraphStyleAttributeName, nil];
NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:title attributes:attrDic]; 
[self.titleField setAllowsEditingTextAttributes:YES];
[self.titleField setAttributedStringValue:attrString];

关于objective-c - cocoa NSTextField 行间距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8356904/

相关文章:

objective-c - 如何从 SQLite 表中检索所有行?

ios - UICollectionView 显示为空白

objective-c - NSTextContainer 排除路径 setter 无法识别?

xcode - cocoa 中 NSTextField 上的 "rich text"按钮的用途是什么?

swift - 在输入 NSTextField 时过滤 NSTable - 自动选择第一行

iphone - 增加 xcode 上的启动图像时间

objective-c - 在另一个方法中设置后,numberOfRowsInTableView 中的变量变为 nil

iphone - 当用户旋转到横向时全屏播放视频,然后在视频停止时以编程方式旋转回来

objective-c - 更改 NSTextField 的边框颜色

iphone - 我正在尝试在 map View 上放置多个图钉,但出现错误