2011-12-02 17 views
5

私は非常に単純な問題で苦労しています。私はいくつかのNSTextField(今はNSTextViewを使用できません)をしています。表示されたテキストの行間を変更する必要があります。 行の高さや行間を減らすにはどうすればよいですか?フォントサイズの縮小はオプションではありません。Cocoa NSTextField行間

本当にありがとうございます。

は、

を素晴らしい週末を!)

+7

なぜNSTextViewを使用できませんか? NSTextFieldは、通常、1行のテキストフィールドに使用されます。 – sidyll

答えて

2

テキストを表示するためにNSAttributedStringを使用することができます。

NSFont *bold14 = [NSFont boldSystemFontOfSize:14.0]; 
NSColor *textColor = [NSColor redColor]; 
NSMutableParagraphStyle *textParagraph = [[NSMutableParagraphStyle alloc] init]; 
[textParagraph setLineSpacing:10.0]; 

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]; 

テキストを入力するのではなく、テキストを表示しても問題ありません。そして私は行間隔を設定する方法だけを知っています。

+0

ありがとうございます。それは私のために働いた。 – Sid

3

参照のためにこの段落スタイルの説明を読んでください:Cocoa Paragraph Stylesとそこにあるすべての行に段落間、段落間などの追加スペースが追加されていることに注意してください。NSMutableParagraphStyleの値をゼロに設定できます低い。

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]; 
3

ジェイソン・ハリソンの優れたObj-のスウィフトバージョン:さらに、線の間隔を縮小setMaximumLineHeightを使用して、コード(私はsetMaximumLineHeightを追加しました)のための「6 1」に感謝する

c答え:

let title:String = "title here" 
let bold14:NSFont = NSFont.boldSystemFontOfSize(14.0) 
let textColor:NSColor = NSColor.redColor() 
let textParagraph:NSMutableParagraphStyle = NSMutableParagraphStyle() 
textParagraph.lineSpacing = 10.0 /*this sets the space BETWEEN lines to 10points*/ 
textParagraph.maximumLineHeight = 12.0/*this sets the MAXIMUM height of the lines to 12points*/ 
let attribs = [NSFontAttributeName:bold14,NSForegroundColorAttributeName:textColor,NSParagraphStyleAttributeName:textParagraph] 
let attrString:NSAttributedString = NSAttributedString.init(string: title, attributes: attribs) 
textField.attributedStringValue = attrString 
+0

私はテキストをもっとコンパクトにしたいと思っていましたが、フォントサイズがぼろぼろになると、テキストはフレームによって切り取られます。回避策は、textField.bounds.origin.yの値を2つのピクセルでオフセットすることでした。基本的に:(font.size - leading) – eonist

関連する問題