Ankahathara
Ankahathara

Reputation: 2934

How to color text in UITextView

I've got four buttons on the view controller and a text view. These five buttons has colors, for example red, yellow, green, blue and black.

When the user started to type without pressing those buttons the color of the text view being typed should have black color text. If user press red button then the color of the text from that point should be red until the user press any other colored button.

enter image description here

How to do this ? Ive followed this tutorial https://www.objc.io/issues/5-ios7/getting-to-know-textkit/

But do not know how to customized it to what I want to achieve.

Upvotes: 1

Views: 864

Answers (3)

MRKT
MRKT

Reputation: 56

Here is how I proceed if it can helps you:

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- implements 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- Add IBAction for changing color =>

 - (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];
   }

Upvotes: 1

pkacprzak
pkacprzak

Reputation: 5629

You can use NSMutableAttributedString to achieve that. The idea is the following (I didn't tested it, just wrote it here by hand):

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)];

Using this method you can achieve what you want applying color to ranges you want in the text.

You can set the attributed text to you UILabel like that:

yourLabel.attributedText = attributedString

Upvotes: 1

Mikhail Grebionkin
Mikhail Grebionkin

Reputation: 3836

You need to use NSAttributedString class.

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

Upvotes: 1

Related Questions