Faire certains mots dans un NSSsortingng gras et cliquable comme des balises

Je dois faire en sorte que certains mots d'un NSSsortingng soient cliquables et que le style de police soit différent d'un tag.

J'ai un code comme ça:

NSSsortingng *str = @"This is my ssortingng and it is #cool and #fun. Please click on the tags."; 

Donc les mots #cool et #fun ci-dessus deviendraient des buttons pour une action d'uibutton. Dans la fonction je passerais cool ou fun à un nouveau UIViewController.

Merci!

Voici un extrait de code

 NSMutableAtsortingbutedSsortingng * str = [[NSMutableAtsortingbutedSsortingng alloc] initWithSsortingng:@"Google"]; [str addAtsortingbute: NSLinkAtsortingbuteName value: @"http://www.google.com" range: NSMakeRange(0,str.length)]; [str addAtsortingbute:kCTFontAtsortingbuteName value: boldFontName range: NSMakeRange(0,str.length)]; yourTextField.atsortingbutedText = str; 

modifier

La chose la plus proche de l'implémentation de methods similaires à l'action UIButton pour une string de ce type serait de find d'abord le rect de la plage sélectionnée dans UITextView utilisant la méthode firstRectForRange: puis de superposer un UIButton invisible réel avec l'action connectée.

Découvrez cette réponse.

Cela devrait être un NSAtsortingbutedSsortingng, pas un NSSsortingng. Un NSAtsortingbutedSsortingng vous permet d'appliquer un style à une seule partie du text. Et une telle exécution de style peut inclure un lien cliquable.

Vous pouvez modifier la police en une variante en gras avec l'atsortingbut NSFontAtsortingbuteName et vous pouvez append le lien avec l'atsortingbut NSLinkAtsortingbuteName .

S'il vous plaît se référer à l'exemple de code ci-dessous: –

 NSSsortingng *str = @"This is my ssortingng and it is #cool and #fun. Please click on the tags."; NSMutableAtsortingbutedSsortingng *yourAtt=[[NSMutableAtsortingbutedSsortingng alloc]init]; for (NSSsortingng *word in [str componentsSeparatedBySsortingng:@" "]) { if ([word isEqualToSsortingng:@"#cool"] || [word isEqualToSsortingng:@"#fun."]) { [yourAtt appendAtsortingbutedSsortingng:[[NSAtsortingbutedSsortingng alloc]initWithSsortingng:word atsortingbutes:@{NSLinkAtsortingbuteName:@"http://www.google.com"}]]; } else { [yourAtt appendAtsortingbutedSsortingng:[[NSAtsortingbutedSsortingng alloc]initWithSsortingng:word atsortingbutes:@{NSFontAtsortingbuteName:[NSFont boldSystemFontOfSize:12]}]]; } [yourAtt appendAtsortingbutedSsortingng:[[NSAtsortingbutedSsortingng alloc]initWithSsortingng:@" "]]; } self.yourAttStr=yourAtt; 

La sortie est deux mots #cool et #fun est cliquable maintenant et les fonts restantes sont en gras: – entrez la description de l'image ici