J'ai deux NSAttributedString , où j'essaie de mettre le premier en gras. J'essaie de le mettre en gras mais cela ne fonctionne pas.
var attrTitle : NSAttributedString
var attrBody : NSAttributedString
let mutableAttributedString = NSMutableAttributedString()
let boldAttributedString = NSAttributedString(attributedString:attrTitle)
let regularAttributedString = NSAttributedString(attributedString:attrBody)
mutableAttributedString.append(boldAttributedString)
mutableAttributedString.append(regularAttributedString)
y a-t-il un moyen de le mettre en gras..Merci de l'aide
3 Réponses :
Il semble que vous ayez manqué des attributs en gras pour la chaîne de titre, vous pouvez y parvenir via NSAttributedString.Key.font avec n'importe quelle valeur de police en gras.
let attrTitle = "Title" let attrBody = "Body" let mutableAttributedString = NSMutableAttributedString() let attributes = [NSAttributedString.Key.font : UIFont.boldSystemFont(ofSize: 15)] let boldAttributedString = NSMutableAttributedString(string: attrTitle, attributes: attributes) let regularAttributedString = NSAttributedString(attributedString: attrBody) mutableAttributedString.append(boldAttributedString) mutableAttributedString.append(regularAttributedString)
Merci mais mes chaînes sont attribuées string..est-il possible avec ça?
Vous pouvez initialiser votre attrTitle également avec des attributs et passer une valeur pour la clé .font .
Vous pouvez ajouter un attribut comme celui-ci
let boldAttributedString = NSMutableAttributedString(attributedString: attrTitle)
boldAttributedString.addAttributes([.font : UIFont.boldSystemFont(ofSize: 15)], range: NSMakeRange(0, boldAttributedString.length))
donnant une erreur comme "La valeur de type 'NSAttributedString' n'a pas de membre 'addAttributes'"
@ user7423463 Relisez la réponse. Avez-vous utilisé NSAttributedString au lieu de NSMutableAttributedString ?
oui @Sweeper a raison ... utilisez NSMutableAttributedString
Nous avons trouvé un moyen de le rendre gras tout en utilisant NSAttributedString .
Vous devez attribuer les propriétés au préalable.
var attrTitle : NSAttributedString
var attrBody : NSAttributedString
var mutableAttributedString = NSMutableAttributedString()
//Properties
let boldFont = UIFont.boldSystemFont(ofSize: //your size)
let attributes = [NSAttributedString.Key.font: boldFont]
let boldAttributedString = NSAttributedString(string: attrTitle.string, attributes: attributes)
let regularAttributedString = NSAttributedString(attributedString:attrBody)
mutableAttributedString.append(boldAttributedString)
mutableAttributedString.append(regularAttributedString)
Aussi rappelez-vous que si vous souhaitez afficher la chaîne attribuée, vous devez appeler la propriété .attributedText de votre objet avant l'attribution.
J'espère que cela vous aidera.
If est censé fonctionner si la valeur de l'attribut
.fontdeattrTitleest un style de police en gras.Les chaînes sont des chaînes attribuées
Ensuite, cela devrait fonctionner. Les attributs sont conservés lors de la concaténation des chaînes. Et il est redondant de créer une chaîne attribuée à partir d'une chaîne d'attributs.
attrTitle.font ne reçoit pas
Bien sûr que non, cela fait partie du dictionnaire des attributs.
pouvez-vous s'il vous plaît aider avec le code ..
Votre question n'est pas claire. Si
attrTitleest déjà en gras, il suffit de mettre les chaînes ensemble sans en créer de nouvelles. Sinon, utilisez la suggestion dans la réponse de Maxim.attrTitle ce n'est pas gras ... je veux le rendre gras