2017-10-05 11 views
0

J'ai le code suivant pour ajouter la chaîne attribuée au texte UITextView. J'ai besoin d'ajouter la police personnalisée et la taille de la police dans swift. Comment faire?Ajout de la taille de police et du nom de police personnalisé à la chaîne attribuée dans UITextView

func setAttributedString(string: String) -> NSAttributedString { 
    let attrString = NSMutableAttributedString(string: string) 
    let paragraphStyle = NSMutableParagraphStyle() 
    paragraphStyle.lineSpacing = 7 
    paragraphStyle.minimumLineHeight = 7 
    attrString.addAttribute(NSParagraphStyleAttributeName, value:paragraphStyle, range:NSRange(location: 0, length:attrString.length)) 
    attrString.addAttribute(NSFontAttributeName, value: UIFont.systemFont(ofSize: 30), range: NSRange(location: 0, length:attrString.length)) 
    return attrString 
    } 

Je dois ajouter la police et la taille de police personnalisées à la fonction suivante. Comment y parvenir?

+0

Copie possible de [Comment créer une chaîne avec Swift?] (Https://stackoverflow.com/questions/24666515/how-do-i-make-an-attributed-string-using-swift –

+0

Vous obtiendrez, espérons-le, votre réponse depuis le lien. Il suffit de convertir la syntaxe à la dernière syntaxe swift. –

+0

https://stackoverflow.com/questions/46460034/swift-3-render-textview-using-nsattributedstring-but-want-change-the-default-fon/46460119#46460119 –

Répondre

0

swift3

let attributs = [NSAttributedStringKey.font: UIFont (nom: "HelveticaNeue-Bold", taille: 17) !, NSAttributedStringKey.foregroundColor: UIColor.white]

laisser attributedString = NSMutableAttributedString (string: "Votre chaîne", les attributs: les attributs)