2011-12-02 8 views
5

Estoy luchando con un problema muy simple, tengo varios NSTextField (no puedo usar NSTextView en este momento) y tengo que cambiar el espacio entre líneas del texto que se muestra. ¿Qué puedo hacer para reducir la altura de la fila o el espaciado entre líneas? Reducir el tamaño de la fuente no es una opción.Cacao NSTextField espaciamiento de línea

¡Cualquier ayuda sería muy apreciada!

tener un gran fin de semana,

!)

+7

¿Por qué no puedes usar NSTextView? NSTextField generalmente se usa para campos de texto de una sola línea. – sidyll

Respuesta

2

puede utilizar NSAttributedString para mostrar el texto.

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

Está bien que aparezca el texto para no ingresar texto. Y solo sé cómo establecer el espaciado entre líneas.

+0

Gracias. Funcionó para mí – Sid

3

Como referencia que desea leer esta descripción de los estilos de párrafo: Cocoa Paragraph Styles y tenga en cuenta que todo en que hay espacio adicional añadido entre líneas, entre los párrafos, los párrafos antes, etc Usted puede establecer los valores en su NSMutableParagraphStyle a cero, pero sin inferior.

Para reducir aún más el espacio entre líneas, el uso setMaximumLineHeight, gracias a "6 1" para el código (he añadir el setMaximumLineHeight):

versión de excelente obj- de Jason Harrison
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 c respuesta:

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

Quería que el texto fuera más compacto verticalmente, pero si iba por debajo del tamaño de la fuente, el texto quedaría recortado por el marco. Una solución alternativa era compensar el valor de textField.bounds.origin.y con un par de píxeles. Básicamente: (font.size - leading) – eonist