Tengo una etiqueta que tiene pocas líneas de texto y quiero aumentar el espacio entre las líneas. Hay preguntas similares hechas por otros, pero las soluciones no resuelven mis problemas. Además, mi etiqueta puede contener párrafos o no. Soy nuevo en Swift
. ¿Existe una solución usando storyboard? ¿O solo a través de NSAttributedString
su posible?
ios
swift
uilabel
line-spacing
Sneha
fuente
fuente
NSMutableAttributedString
en su lugarNSAttributedString
. He actualizado una respuesta.NSMutableAttributedString
. Puede usarNSAttributedString(string: "Your text", attributes: [NSAttributedString.Key.paragraphStyle : paragraphStyle])
Desde Interface Builder:
Programáticamente:
SWift 4 y 4.2
Usando extensión de etiqueta
extension UILabel { func setLineSpacing(lineSpacing: CGFloat = 0.0, lineHeightMultiple: CGFloat = 0.0) { guard let labelText = self.text else { return } let paragraphStyle = NSMutableParagraphStyle() paragraphStyle.lineSpacing = lineSpacing paragraphStyle.lineHeightMultiple = lineHeightMultiple let attributedString:NSMutableAttributedString if let labelattributedText = self.attributedText { attributedString = NSMutableAttributedString(attributedString: labelattributedText) } else { attributedString = NSMutableAttributedString(string: labelText) } // (Swift 4.2 and above) Line spacing attribute attributedString.addAttribute(NSAttributedString.Key.paragraphStyle, value:paragraphStyle, range:NSMakeRange(0, attributedString.length)) // (Swift 4.1 and 4.0) Line spacing attribute attributedString.addAttribute(NSAttributedStringKey.paragraphStyle, value:paragraphStyle, range:NSMakeRange(0, attributedString.length)) self.attributedText = attributedString } }
Ahora llame a la función de extensión
let label = UILabel() let stringValue = "Set\nUILabel\nline\nspacing" // Pass value for any one argument - lineSpacing or lineHeightMultiple label.setLineSpacing(lineSpacing: 2.0) . // try values 1.0 to 5.0 // or try lineHeightMultiple //label.setLineSpacing(lineHeightMultiple = 2.0) // try values 0.5 to 2.0
O usando una instancia de etiqueta (simplemente copie y ejecute este código para ver el resultado)
let label = UILabel() let stringValue = "Set\nUILabel\nline\nspacing" 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 // Line spacing attribute attrString.addAttribute(NSAttributedStringKey.paragraphStyle, value: style, range: NSRange(location: 0, length: stringValue.characters.count)) // Character spacing attribute attrString.addAttribute(NSAttributedStringKey.kern, value: 2, range: NSMakeRange(0, attrString.length)) label.attributedText = attrString
Swift 3
let label = UILabel() let stringValue = "Set\nUILabel\nline\nspacing" 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
fuente
Puede controlar el interlineado
storyboard
.La misma pregunta.
fuente
ascender
propiedad como se menciona aquí .Solución reciente para Swift 5.0
private extension UILabel { // MARK: - spacingValue is spacing that you need func addInterlineSpacing(spacingValue: CGFloat = 2) { // MARK: - Check if there's any text guard let textString = text else { return } // MARK: - Create "NSMutableAttributedString" with your text let attributedString = NSMutableAttributedString(string: textString) // MARK: - Create instance of "NSMutableParagraphStyle" let paragraphStyle = NSMutableParagraphStyle() // MARK: - Actually adding spacing we need to ParagraphStyle paragraphStyle.lineSpacing = spacingValue // MARK: - Adding ParagraphStyle to your attributed String attributedString.addAttribute( .paragraphStyle, value: paragraphStyle, range: NSRange(location: 0, length: attributedString.length )) // MARK: - Assign string that you've modified to current attributed Text attributedText = attributedString } }
Y el uso:
let yourLabel = UILabel() let yourText = "Hello \n world \n !" yourLabel.text = yourText yourLabel.addInterlineSpacing(spacingValue: 1.5)
fuente
UILabel.text
y noUILabel.attributedText
Puede utilizar esta extensión reutilizable:
extension String { func lineSpaced(_ spacing: CGFloat) -> NSAttributedString { let paragraphStyle = NSMutableParagraphStyle() paragraphStyle.lineSpacing = spacing let attributedString = NSAttributedString(string: self, attributes: [NSAttributedString.Key.paragraphStyle: paragraphStyle]) return attributedString } }
fuente
Swift 4 y Swift 5
extension NSAttributedString { func withLineSpacing(_ spacing: CGFloat) -> NSAttributedString { let attributedString = NSMutableAttributedString(attributedString: self) let paragraphStyle = NSMutableParagraphStyle() paragraphStyle.lineBreakMode = .byTruncatingTail paragraphStyle.lineSpacing = spacing attributedString.addAttribute(.paragraphStyle, value: paragraphStyle, range: NSRange(location: 0, length: string.count)) return NSAttributedString(attributedString: attributedString) } }
Cómo utilizar
let example = NSAttributedString(string: "This is Line 1 \nLine 2 \nLine 3 ").withLineSpacing(15) testLabel.attributedText = example
fuente
Respuesta de Dipen actualizada para Swift 4
let attr = NSMutableAttributedString(string: today) let paragraphStyle = NSMutableParagraphStyle() paragraphStyle.lineSpacing = 2 attr.addAttribute(.paragraphStyle, value: paragraphStyle, range: NSMakeRange(0, attr.length)) label.attributedText = attr;
fuente
//Swift 4: func set(text:String, inLabel:UILabel, withLineSpacing:CGFloat, alignment:NSTextAlignment){ let paragraphStyle = NSMutableParagraphStyle() paragraphStyle.lineSpacing = withLineSpacing let attrString = NSMutableAttributedString(string: text) attrString.addAttribute(NSAttributedStringKey.paragraphStyle, value:paragraphStyle, range:NSMakeRange(0, attrString.length)) inLabel.attributedText = attrString inLabel.textAlignment = alignment }
fuente