Quiero alinear un text de UILabel
verticalmente con espaciado de línea a través de xib en iOS7, por ejemplo:
hello, how are you?
Por favor, ayúdame.
Sí, es posible ajustar el espaciado de línea a través de xib. Haga clic en UIlabel en xib, luego cambie el tipo de propiedad 'text' a 'Atribuido', luego pase por las siguientes imágenes. Luego ingrese una nueva línea (presione la tecla de método abreviado ctrl-return) y mantenga el cursor entre dos líneas y luego ajuste la propiedad 'línea' y la 'Altura máxima' que desee. Mi post UILael es "Hola, ¿cómo estás?"
Crea esta subclass UILabel simple:
@interface CustomLabel : UILabel @property (assign, nonatomic) CGFloat myLineSpacing; @end @implementation CustomLabel - (void)setMyLineSpacing:(CGFloat)myLineSpacing { _myLineSpacing = myLineSpacing; self.text = self.text; } - (void)setText:(NSString *)text { NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; paragraphStyle.lineSpacing = _myLineSpacing; paragraphStyle.alignment = self.textAlignment; NSDictionary *attributes = @{NSParagraphStyleAttributeName: paragraphStyle}; NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:text attributes:attributes]; self.attributedText = attributedText; }
Establezca el valor de la propiedad myLineSpacing
con IB.
Aún así, no puedes get una vista previa en IB, ¡pero el valor del espaciado de línea está en xib !
Puede establecer label.text
en su código sin preocuparse por el espacio entre líneas.
Nota:
no haga el nombre de propiedad " lineSpacing
". UILabel tiene una propiedad indocumentada lineSpacing y anulación que rompe algo.
Aquí aparece el resultado en Swift 3 Simplemente copie y ejecute este código.
let label = UILabel() let stringValue = "hello\nhow\nare\nyou" let attrString = NSMutableAttributedString(string: stringValue) var style = NSMutableParagraphStyle() style.lineSpacing = 24 // change line spacing between paragraph like 36 or 48 style.minimumLineHeight = 20 // change line spacing between each line like 30 or 40 attrString.addAttribute(NSParagraphStyleAttributeName, value: style, range: NSRange(location: 0, length: stringValue.characters.count)) label.attributedText = attrString
Escriba exactamente de la misma manera que desea en cualquier editor de text como TextEdit, etc. y luego pegue en la sección de text de su label en xib.
Pero asegúrese de que la altura de su label sea más adecuada para el contenido y también aumente la cantidad de líneas. Para este caso, puede ser de 3 o más.