2011-12-02 12 views
5

मैं एक बहुत ही साधारण समस्या से जूझ रहा हूं, मेरे पास कई एनएसटीक्स्टफिल्ड हैं (मैं अभी एनएसटीक्स्टव्यू का उपयोग नहीं कर सकता) और मुझे प्रदर्शित टेक्स्ट की लाइन स्पेस बदलने की जरूरत है। पंक्ति ऊंचाई या रेखा अंतर को कम करने के लिए मैं क्या कर सकता हूं? फ़ॉन्ट आकार को हटाना एक विकल्प नहीं है।कोको एनएसटीक्स्टफाल्ड लाइन स्पेसिंग

किसी भी मदद की वास्तव में सराहना की जाएगी!

एक महान सप्ताह के अंत में,

!)

+7

आप NSTextView का उपयोग क्यों नहीं कर सकते? NSTextField आमतौर पर एकल पंक्ति पाठ फ़ील्ड के लिए उपयोग किया जाता है। – 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 और ध्यान दें कि वहां सबकुछ पैराग्राफ के बीच पैराग्राफ के बीच अतिरिक्त स्थान जोड़ा गया है। आप अपने एनएसएमयूटेबल पैराग्राफ स्टाइल में शून्य को शून्य पर सेट कर सकते हैं लेकिन नहीं कम।

आगे कोड के लिए "6 1" के लिए लाइनों, उपयोग setMaximumLineHeight, धन्यवाद के बीच का अंतर हटना करने के लिए (मैं 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

स्विफ्ट जेसन हैरिसन का उत्कृष्ट Obj- के संस्करण सी उत्तर:

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 मान को ऑफसेट करना एक वर्कअराउंड था। असल में: (font.size - अग्रणी) – eonist

संबंधित मुद्दे