web-dev-qa-db-fra.com

partie en gras de la chaîne dans UITextView swift

Voici le texte:

@IBOutlet weak var legalText: UITextView!
let textToAppend = "TERMS OF SERVICE\nLast Updated: May 7, 2015\n\nPLEASE NOTE: The HIGO Terms of Service as stated below is effective as of the 'Last Updated' date above for any user who is browsing the HIGO website, or for any user who creates a HIGO account on or after that date."
legalText.text = legalText.text.stringByAppendingString(textToAppend)

je souhaite mettre en gras les "CONDITIONS D'UTILISATION" et "S'IL VOUS PLAÎT NOTE: Les Conditions d'utilisation de HIGO telles qu'indiquées ci-dessous sont en vigueur à compter de la date de" Dernière mise à jour "ci-dessus pour tout utilisateur qui navigue sur le site Web HIGO, ou pour tout utilisateur qui crée un Compte HIGO à cette date ou après. "

j'essaie d'utiliser uilabel par programme dans uitextview mais n'a pas fonctionné:

var termsofservice : UILabel = UILabel()
termsofservice.numberOfLines = 0
termsofservice.text = "TERMS OF SERVICE"
termsofservice.font = UIFont.boldSystemFontOfSize(20)
termsofservice.textAlignment = NSTextAlignment.Left;

var pleasenote : UILabel = UILabel()
pleasenote.numberOfLines = 0
pleasenote.text = "PLEASE NOTE: The HIGO Terms of Service as stated below is effective as of the 'Last Updated' date above for any user who is browsing the HIGO website, or for any user who creates a HIGO account on or after that date."
pleasenote.font = UIFont.boldSystemFontOfSize(20)
pleasenote.textAlignment = NSTextAlignment.Left;

let textToAppend = "\(termsofservice)\nLast Updated: May 7, 2015\n\n\(pleasenote)"

essayez également avec ceux-ci mais cela n'a pas fonctionné, il n'affiche que "CONDITIONS D'UTILISATION" et "dernière mise à jour ..", n'affiche pas "S'IL VOUS PLAÎT NOTE ..."

var termsofservice = "TERMS OF SERVICE"
var normalText = "\n\nLast Updated: May 7, 2015"
var pleasenote  = "\n\nPLEASE NOTE: The HIGO Terms of Service as stated below is effective as of the 'Last Updated' date above for any user who is browsing the HIGO website, or for any user who creates a HIGO account on or after that date."

var attributedString = NSMutableAttributedString(string:normalText)

var attrs = [NSFontAttributeName : UIFont.boldSystemFontOfSize(15)]
var boldString = NSMutableAttributedString(string:pleasenote, attributes:attrs)
var boldString0 = NSMutableAttributedString(string:termsofservice, attributes:attrs)

boldString0.appendAttributedString(attributedString)
attributedString.appendAttributedString(boldString)
legalText.attributedText = boldString0

Comment mettre en gras cette partie de la chaîne?

Remarque: le texte est toujours long et a plus de partie de chaîne en gras.

19
Sarimin

Mis à jour pour Swift

func attributedText() -> NSAttributedString {

    let string = "TERMS OF SERVICE\nLast Updated: May 7, 2015\n\nPLEASE NOTE: The HIGO Terms of Service as stated below is effective as of the 'Last Updated' date above for any user who is browsing the HIGO website, or for any user who creates a HIGO account on or after that date." as NSString

    let attributedString = NSMutableAttributedString(string: string as String, attributes: [NSFontAttributeName:UIFont.systemFont(ofSize: 15.0)])

    let boldFontAttribute = [NSFontAttributeName: UIFont.boldSystemFont(ofSize: 15.0)]

    // Part of string to be bold
    attributedString.addAttributes(boldFontAttribute, range: string.range(of: "TERMS OF SERVICE"))
    attributedString.addAttributes(boldFontAttribute, range: string.range(of: "PLEASE NOTE:"))

    // 4
    return attributedString
}

Et définissez le texte attributext sur votre vue texte comme suit:

legalText.attributedText = attributedText()
45
Hamza Ansari

Mieux encore, vous pouvez continuer et utiliser cette fonction 1 dans votre arsenal, dans un fichier comme Constants.Swift, puis vous pouvez enhardir des mots dans n'importe quelle chaîne, à de nombreuses reprises en appelant simplement ONE LINE de code :

Pour aller dans un fichier sans classe, comme des constantes.Swift dans mon cas:

import Foundation
import UIKit

func addBoldText(fullString: NSString, boldPartOfString: NSString, font: UIFont!, boldFont: UIFont!) -> NSAttributedString {
    let nonBoldFontAttribute = [NSAttributedString.Key.font:font!]
    let boldFontAttribute = [NSAttributedString.Key.font:boldFont!]
    let boldString = NSMutableAttributedString(string: fullString as String, attributes:nonBoldFontAttribute)
    boldString.addAttributes(boldFontAttribute, range: fullString.range(of: boldPartOfString as String))
    return boldString
}

Ensuite, vous pouvez simplement appeler cette seule ligne de code pour n'importe quel UILabel:

let normalFont = UIFont(name: "INSERT FONT NAME", size: 14)
let boldFont = UIFont(name: "INSERT BOLD FONT NAME", size: 14)
self.UILabel.attributedText = addBoldText("Check again in 30 DAYS to find more friends", boldPartOfString: "30 DAYS", font: normalFont!, boldFont: boldFont!)
7
David West

Swift 3 Update (d'après la réponse de @Hamza Ansari)

func attributedText()-> NSAttributedString
{
    let string = "TERMS OF SERVICE\nLast Updated: May 7, 2015\n\nPLEASE NOTE: The HIGO Terms of Service as stated below is effective as of the 'Last Updated' date above for any user who is browsing the HIGO website, or for any user who creates a HIGO account on or after that date." as NSString

    let attributedString = NSMutableAttributedString(string: string as String, attributes: [NSFontAttributeName:UIFont.systemFont(ofSize: 15.0)])

    let boldFontAttribute = [NSFontAttributeName: UIFont.boldSystemFont(ofSize: 15.0)]

    // Part of string to be bold
    attributedString.addAttributes(boldFontAttribute, range: string.range(of: "TERMS OF SERVICE"))
    attributedString.addAttributes(boldFontAttribute, range: string.range(of: "PLEASE NOTE:"))

    // 4
    return attributedString
}
1
Kevin Sabbe

Mise à jour Swift 4.2

func addBoldText(fullString: String, boldPartOfString: String, baseFont: UIFont, boldFont: UIFont) -> NSAttributedString {

    //1
    let baseFontAttribute = [NSAttributedString.Key.font : baseFont]
    let boldFontAttribute = [NSAttributedString.Key.font : boldFont]

    //2
    let attributedString = NSMutableAttributedString(string: fullString, attributes: baseFontAttribute)

    //3. Note if 'boldPartOfString' is not a substring of 'fullString', enitre 'fullString' will be formatted with 'boldFont'
    attributedString.addAttributes(boldFontAttribute, range: NSRange(fullString.range(of: boldPartOfString) ?? fullString.startIndex..<fullString.endIndex, in: fullString))

    //4
    return attributedString
}
0
Swappy Dwappy