2016-04-05 1 views
1

J'ai quatre boutons sur le contrôleur de vue et une vue de texte. Ces cinq boutons ont des couleurs, par exemple rouge, jaune, vert, bleu et noir. Lorsque l'utilisateur a commencé à taper sans appuyer sur ces boutons, la couleur de la vue de texte tapée doit avoir un texte de couleur noire. Si l'utilisateur appuie sur le bouton rouge, la couleur du texte à partir de ce point doit être rouge jusqu'à ce que l'utilisateur appuie sur un autre bouton de couleur.Comment colorer du texte dans UITextView

enter image description here

Comment faire? J'ai suivi ce tutoriel https://www.objc.io/issues/5-ios7/getting-to-know-textkit/

Mais je ne sais pas comment personnaliser ce que je veux atteindre.

+0

Vous pourriez vouloir essayer 'UITextView' et le sous-classement mettre en œuvre/override' textStylingAtPosition (_: inDirection:) '' du protocole UITextInput' – zcui93

Répondre

1

Voici comment je procède si elle peut vous aide:

1- add one property to retain current Color and initialize it with black color 

@property (nonatomic, retain) UIColor *curColor;//in your interface declaration 

self.curColor = [UIColor blackColor];//Init it in Viewdidload for example 

2- implémente UITextViewDelegate

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text 
{ 

    NSAttributedString *currentText = self.Textview.attributedText;//To store current text and its attributs 
    NSAttributedString *newOneText = [[NSAttributedString alloc] initWithString:text attributes:@{NSForegroundColorAttributeName:self.curColor}];//for the new text with selected color 

    NSMutableAttributedString *shouldDisplayText = [[NSMutableAttributedString alloc] initWithAttributedString: currentText]; 

    [shouldDisplayText appendAttributedString: newOneText];// add old and new text 

    self.Textview.attributedText = shouldDisplayText;//set it ton control 


    return NO; 
} 

3- Ajouter IBAction pour changer de couleur =>

- (IBAction) redColorClicked 
    { 
    self.curColor = [UIColor colorWithRed:1.0f green: 0.0f blue:0.0f alpha:1.0f]; 
    } 

- (IBAction) blueColorClicked 
     { 
     self.curColor = [UIColor colorWithRed:0.0f green: 0.0f blue:1.0f alpha:1.0f]; 
    } 
+0

Bien que ce soit le moyen d'atteindre le résultat attendu, lorsque vous appuyez sur les caractères du clavier il double le caractère tapé. Comment le faire sans dupliquer le personnage? – Ankahathara

+0

Salut! Désolé pour répondre tard. Pour Comment le faire sans dupliquer le personnage? changer juste le retunr du délégué UITextView: - (BOOL) textView: (UITextView *) textView shouldChangeTextInRange: (NSRange) replacementText gamme: (NSString *) Texte NO et vous avez terminé. J'ai modifié le code ci-dessus pour répondre à vos besoins – MRKT

1

Vous devez utiliser la classe NSAttributedString.

let defaultAttributes = [NSFontAttributeName: UIFont.systemFontOfSize(UIFont.systemFontSize()), 
         NSForegroundColorAttributeName: UIColor.blackColor()] 
let text = "this text is red and yellow" 
let str = NSMutableAttributedString(string: text, attributes: defaultAttributes) 
str.setAttributes([NSForegroundColorAttributeName: UIColor.redColor()], range: (text as NSString).rangeOfString("red")) 
str.setAttributes([NSForegroundColorAttributeName: UIColor.yellowColor()], range: (text as NSString).rangeOfString("yellow")) 
textView.attributedText = str 
1

Vous pouvez utiliser NSMutableAttributedString pour cela. L'idée est la suivante (je ne l'ai pas testé, juste écrit ici à la main):

NSString *str = @"stackoverflow"; 
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:str]; 

// Set foreground color of "stack" substring in our string to red 
[attributedString addAttribute:NSForegroundColorAttributeName 
    value:[UIColor redColor]; 
    range:NSMakeRange(0, 5)]; 

utilisant cette méthode, vous pouvez obtenir ce que vous voulez appliquer la couleur à des plages que vous voulez dans le texte.

Vous pouvez définir le texte attribué à vous UILabel comme ça:

yourLabel.attributedText = attributedString