attributed - parte en negrita de la cadena en UITextView swift
nsattributedstring swift 3 (3)
Aquí está el texto:
@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)
Quiero escribir en negrita "TÉRMINOS DE SERVICIO" y "TENGA EN CUENTA: Los Términos de servicio de HIGO que se indican a continuación entran en vigencia a partir de la fecha de ''Última actualización'' para cualquier usuario que navegue por el sitio web de HIGO, o para cualquier usuario que cree un Cuenta de HIGO en o después de esa fecha ".
Intento con el uso de uilabel mediante programación en uitextview pero no funcionó:
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)"
También intente con estos pero no funcionó, solo muestra "TÉRMINOS DE SERVICIO" y "última actualización ...", no mostró "POR FAVOR, OBSERVE ..."
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
¿Cómo negrita esa parte de la cuerda?
Nota: el texto aún es largo y tiene más parte de la cadena en negrita.
Mejor aún, puedes seguir adelante y usar esta función 1 en tu armería, en un archivo como Constants.swift y luego puedes envalentonar palabras dentro de cualquier cadena, en numerosas ocasiones llamando solo a UNA LÍNEA de código:
Para ir en un archivo sin clase, como constants.swift en mi caso:
import Foundation
import UIKit
func addBoldText(fullString: NSString, boldPartOfString: NSString, font: UIFont!, boldFont: UIFont!) -> NSAttributedString {
let nonBoldFontAttribute = [NSFontAttributeName:font!]
let boldFontAttribute = [NSFontAttributeName:boldFont!]
let boldString = NSMutableAttributedString(string: fullString as String, attributes:nonBoldFontAttribute)
boldString.addAttributes(boldFontAttribute, range: fullString.rangeOfString(boldPartOfString as String))
return boldString
}
Entonces puedes llamar a esta línea de código para cualquier 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: boldSearchFont!)
Actualizado para Swift 3
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
}
Y establezca el texto del atributo en su vista de texto como:
legalText.attributedText = attributedText()
Swift 3 Update (de la respuesta 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
}