Le problème que je rencontre est que je veux être en mesure de changer le textColor de certains texts dans un TextView. J'utilise une string concaténée, et je veux juste les strings que j'ajoute dans le text de TextView. Il semble que ce que je veux utiliser est NSMutableAtsortingbutedSsortingng
, mais je ne trouve aucune ressource pour l'utiliser dans Swift. Ce que j'ai jusqu'ici est quelque chose comme ceci:
let ssortingng = "A \(ssortingngOne) with \(ssortingngTwo)" var atsortingbutedSsortingng = NSMutableAtsortingbutedSsortingng(ssortingng: ssortingng) textView.atsortingbutedText = atsortingbutedSsortingng
De là, je sais que j'ai besoin de find la gamme de mots qui doivent avoir changé leur textColor et ensuite les append à la string atsortingbuée. Ce que je dois savoir, c'est comment find les strings correctes à partir de atsortingbuteSsortingng, puis changer leur textColor.
Comme j'ai une note trop basse, je ne peux pas répondre à ma propre question, mais voici la réponse que j'ai trouvée
J'ai trouvé ma propre réponse en traduisant de la traduction d'un code de
Modifier les attributes des sous-strings dans un NSAtsortingbutedSsortingng
Voici l'exemple de l'implémentation dans Swift:
let ssortingng = "A \(ssortingngOne) and \(ssortingngTwo)" var atsortingbutedSsortingng = NSMutableAtsortingbutedSsortingng(ssortingng:ssortingng) let ssortingngOneRegex = NSRegularExpression(pattern: nameSsortingng, options: nil, error: nil) let ssortingngOneMatches = ssortingngOneRegex.matchesInSsortingng(longSsortingng, options: nil, range: NSMakeRange(0, atsortingbutedSsortingng.length)) for ssortingngOneMatch in ssortingngOneMatches { let wordRange = ssortingngOneMatch.rangeAtIndex(0) atsortingbutedSsortingng.addAtsortingbute(NSForegroundColorAtsortingbuteName, value: UIColor.nameColor(), range: wordRange) } textView.atsortingbutedText = atsortingbutedSsortingng
Puisque je veux changer le textColor de plusieurs strings je ferai une fonction d'aide pour manipuler ceci, mais ceci fonctionne pour changer le textColor.
Je vois que vous avez répondu quelque peu à la question, mais pour fournir un moyen un peu plus concis sans utiliser regex pour répondre à la question du titre:
Pour changer la couleur d'une longueur de text, vous devez connaître l'index de début et de fin des caractères de couleur à être dans la string, par exemple
var main_ssortingng = "Hello World" var ssortingng_to_color = "World" var range = (main_ssortingng as NSSsortingng).rangeOfSsortingng(ssortingng_to_color)
Ensuite, vous convertissez en string atsortingbuée et utilisez 'add atsortingbute' avec NSForegroundColorAtsortingbuteName:
var atsortingbutedSsortingng = NSMutableAtsortingbutedSsortingng(ssortingng:main_ssortingng) atsortingbutedSsortingng.addAtsortingbute(NSForegroundColorAtsortingbuteName, value: NSColor.redColor() , range: range)
Une list d'autres attributes standard que vous pouvez définir peut être trouvée dans la documentation d'Apple
Mise à jour de Swift 2.1:
let text = "We sortinged to make this app as most intuitive as possible for you. If you have any questions don't hesitate to ask us. For a detailed manual just click here." let linkTextWithColor = "click here" let range = (text as NSSsortingng).rangeOfSsortingng(linkTextWithColor) let atsortingbutedSsortingng = NSMutableAtsortingbutedSsortingng(ssortingng:text) atsortingbutedSsortingng.addAtsortingbute(NSForegroundColorAtsortingbuteName, value: UIColor.redColor() , range: range) self.helpText.atsortingbutedText = atsortingbutedSsortingng
self.helpText
est un UILabel
vente UILabel
.
SWIFT 3.0
let txtfield1 :UITextField! var main_ssortingng = "Hello World" let ssortingng_to_color = "World" let range = (main_ssortingng as NSSsortingng).range(of: ssortingng_to_color) let atsortingbute = NSMutableAtsortingbutedSsortingng.init(ssortingng: main_ssortingng) atsortingbute.addAtsortingbute(NSForegroundColorAtsortingbuteName, value: UIColor.red , range: range) txtfield1 = UITextField.init(frame:CGRect(x:10 , y:20 ,width:100 , height:100)) txtfield1.atsortingbutedText = atsortingbute
La réponse est déjà donnée dans les messages précédents, mais j'ai une façon différente de le faire
Swift 3x:
var myMutableSsortingng = NSMutableAtsortingbutedSsortingng() myMutableSsortingng = NSMutableAtsortingbutedSsortingng(ssortingng: "Your full label textSsortingng") myMutableSsortingng.setAtsortingbutes([NSFontAtsortingbuteName : UIFont(name: "HelveticaNeue-Light", size: CGFloat(17.0))! , NSForegroundColorAtsortingbuteName : UIColor(red: 232 / 255.0, green: 117 / 255.0, blue: 40 / 255.0, alpha: 1.0)], range: NSRange(location:12,length:8)) // What ever range you want to give yourLabel.atsortingbutedText = myMutableSsortingng
J'espère que cela aide tout le monde!
La réponse de Chris m'a beaucoup aidé, j'ai donc utilisé son approche et je me suis transformée en une fonction que je peux réutiliser. Cela me permet d'atsortingbuer une couleur à une sous-string tout en donnant une autre couleur au rest de la string.
static func createAtsortingbutedSsortingng(fullSsortingng: Ssortingng, fullSsortingngColor: UIColor, subSsortingng: Ssortingng, subSsortingngColor: UIColor) -> NSMutableAtsortingbutedSsortingng { let range = (fullSsortingng as NSSsortingng).rangeOfSsortingng(subSsortingng) let atsortingbutedSsortingng = NSMutableAtsortingbutedSsortingng(ssortingng:fullSsortingng) atsortingbutedSsortingng.addAtsortingbute(NSForegroundColorAtsortingbuteName, value: fullSsortingngColor, range: NSRange(location: 0, length: fullSsortingng.characters.count)) atsortingbutedSsortingng.addAtsortingbute(NSForegroundColorAtsortingbuteName, value: subSsortingngColor, range: range) return atsortingbutedSsortingng }
Swift 2.2
var myMutableSsortingng = NSMutableAtsortingbutedSsortingng() myMutableSsortingng = NSMutableAtsortingbutedSsortingng(ssortingng: "1234567890", atsortingbutes: [NSFontAtsortingbuteName:UIFont(name: kDefaultFontName, size: 14.0)!]) myMutableSsortingng.addAtsortingbute(NSForegroundColorAtsortingbuteName, value: UIColor(red: 0.0/255.0, green: 125.0/255.0, blue: 179.0/255.0, alpha: 1.0), range: NSRange(location:0,length:5)) self.lblPhone.atsortingbutedText = myMutableSsortingng
Si vous utilisez Swift 3x et UITextView, NSForegroundColorAtsortingbuteName ne fonctionnera peut-être pas (cela n'a pas fonctionné pour moi quelle que soit l'approche que j'ai essayée).
Donc, après quelques searchs, j'ai trouvé une solution.
//Get the textView somehow let textView = UITextView() //Set the atsortingbuted ssortingng with links to it textView.atsortingbutedSsortingng = atsortingbutedSsortingng //Set the tint color. It will apply to the link only textView.tintColor = UIColor.red
Le moyen le plus facile de faire des labels avec des styles différents tels que la couleur, la police, etc. est d'utiliser la propriété "Atsortingbué" dans l'inspecteur des attributes. Choisissez simplement une partie du text et changez-la comme vous voulez