2011-12-02 25 views
5

Tôi đang gặp khó khăn với một vấn đề rất đơn giản, tôi đã có một số NSTextField (tôi không thể sử dụng NSTextView ngay bây giờ) và tôi cần phải thay đổi khoảng cách dòng của văn bản được hiển thị. Tôi có thể làm gì để giảm chiều cao hàng hoặc khoảng cách dòng? Thu hẹp kích thước phông chữ không phải là một tùy chọn.Khoảng cách dòng NSTextField Ca cao

Mọi trợ giúp sẽ thực sự được đánh giá cao!

Có một cuối tuần tuyệt vời,

!)

+7

Tại sao bạn không thể sử dụng NSTextView? NSTextField thường được sử dụng cho các trường văn bản dòng đơn. – sidyll

Trả lời

2

bạn có thể sử dụng NSAttributedString để hiển thị văn bản.

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

OK để hiển thị văn bản không phải cho văn bản nhập liệu. Và tôi chỉ biết cách đặt khoảng cách dòng.

+0

Cảm ơn. Nó làm việc cho tôi. – Sid

3

Để tham khảo, bạn muốn đọc mô tả kiểu đoạn này: Cocoa Paragraph Styles và lưu ý rằng mọi thứ trong đó có thêm khoảng trắng giữa các dòng, giữa các đoạn, trước đoạn văn, v.v. Bạn có thể đặt giá trị trong NSMutableParagraphStyle thành 0 nhưng không thấp hơn.

Để thu nhỏ hơn nữa khoảng cách giữa các dòng, sử dụng setMaximumLineHeight, nhờ "6 1" cho mã (Tôi đã thêm 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]; 
3

Swift phiên bản của obj- tuyệt vời Jason Harrison c trả lời:

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

Tôi muốn văn bản gọn gàng hơn theo chiều dọc, nhưng nếu tôi đi dưới cỡ chữ, văn bản sẽ bị cắt bớt bởi khung. Cách giải quyết là bù đắp giá trị textField.bounds.origin.y với một vài pixel. Về cơ bản: (font.size - hàng đầu) – eonist