2017-02-26 3 views
4

J'ai UILabel avec des informations de paragraphe (texte multiligne) et je veux ajouter des espaces entre les lignes, similaire à cette image.UILabel - Comment ajouter de l'espace entre les lignes dans Swift 3

this image

S'il vous plaît aidez-moi à le faire. J'ai essayé de vérifier toute la documentation du développeur Apple concernant l'étiquette et l'espacement des lignes, mais j'ai pu trouver.

+0

Qu'avez-vous essayé? Post code reproductible afin que les gens peuvent vous aider. Pour améliorer votre question afin d'obtenir de meilleures réponses, consultez http://stackoverflow.com/help/how-to-ask. –

+0

Merci Brian O'Donnell, pour votre suggestion. Je l'ai vu. Je suis nouveau sur ce site, donc je ne savais pas terminé avec le modèle de réponse à la question. –

Répondre

5

De Interface Builder (Storyboard/XIB):

enter image description here

Programmatically:

SWift 4

en utilisant l'extension de l'étiquette

extension UILabel { 

    // Pass value for any one of both parameters and see result 
    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) 
     } 

     // Line spacing attribute 
     attributedString.addAttribute(NSAttributedStringKey.paragraphStyle, value:paragraphStyle, range:NSMakeRange(0, attributedString.length)) 

     self.attributedText = attributedString 
    } 
} 

appellent maintenant la fonction d'extension

let label = UILabel() 
let stringValue = "How to\ncontrol\nthe\nline spacing\nin UILabel" 

// 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 

Ou à l'aide par exemple d'étiquette (Il suffit de copier & exécuter ce code pour voir le résultat)

let label = UILabel() 
let stringValue = "How to\ncontrol\nthe\nline spacing\nin UILabel" 
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 = "Sample text" 
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 
+1

Cette extension Swift 4 est __awesome__. Merci! –

0
@IBOutlet weak var myLabel: UILabel! 
let textForLabel = “Lorem Ipsum is simply dummy text of the printing and 
typesetting industry. Lorem Ipsum has been the industry’s standard dummy text 
ever since the 1500s, when an unknown printer took a galley of type and 
scrambled it to make a type specimen book. It has survived not only five 
centuries, but also the leap into electronic typesetting, remaining 
essentially unchanged.” 

let paragraphStyle = NSMutableParagraphStyle() 
//line height size 
paragraphStyle.lineSpacing = 1.4 
let attrString = NSMutableAttributedString(string: titleText) 
attrString.addAttribute(NSParagraphStyleAttributeName, value:paragraphStyle, 
range:NSMakeRange(0, attrString.length)) 
myLabel.attributedText = attrString 
myLabel.textAlignment = NSTextAlignment.Center 

enter image description here