2011-12-02 13 views
5

Sono alle prese con un problema molto semplice, ho diversi NSTextField (non posso usare NSTextView in questo momento) e ho bisogno di modificare l'interlinea del testo visualizzato. Cosa posso fare per ridurre l'altezza della riga o l'interlinea? La riduzione della dimensione del carattere non è un'opzione.Cocoa NSTextCampo linea di spaziatura

Qualsiasi aiuto sarebbe molto apprezzato!

Avere un grande fine settimana,

!)

+7

Perché non è possibile utilizzare NSTextView? NSTextField viene generalmente utilizzato per campi di testo a riga singola. – sidyll

risposta

2

è possibile utilizzare NSAttributedString per visualizzare il testo.

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

È possibile visualizzare il testo non per l'immissione di testo. E so solo come impostare l'interlinea.

+0

Grazie. Ha funzionato per me. – Sid

3

Per riferimento si desidera leggere questa descrizione degli stili di paragrafo: Cocoa Paragraph Styles e si noti che tutto ciò che c'è dentro è uno spazio aggiuntivo aggiunto tra le righe, tra paragrafi, prima dei paragrafi, ecc. È possibile impostare i valori in NSMutableParagraphStyle a zero ma non inferiore.

a ridursi ulteriormente la spaziatura tra le righe, l'uso setMaximumLineHeight, grazie a "6 1" per il codice (ho aggiungere il setMaximumLineHeight):

versione
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 di ottima obj- di Jason Harrison c risposta:

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

Volevo che il testo fosse più compatto in verticale, ma se avessi superato la dimensione del carattere, il testo sarebbe stato ritagliato dal fotogramma. Una soluzione alternativa consisteva nel compensare il valore textField.bounds.origin.y con un paio di pixel. Fondamentalmente: (font.size - leading) – eonist