1

What's the best way to create a placeholder for a UITextView and the placeholder will have different colors and fonts. An example of what I want is:

_textView = [[UITextView alloc] initWithFrame:CGRectMake(7.0, 35.0, 306.0, 90.0)];
_textView.text = @"This is a variable called foo.";

I want "foo" in the text to be bolder and darker in color.

How's this best done?

cdub
  • 24,555
  • 57
  • 174
  • 303

3 Answers3

6

I have Simple way for this .

Add UILabel in UITextView below and set UITextView clearColor .and set your UILabel text as you required and set that text color same as your requirement color . and if you click on textview then hide that label and if you clear all textview then show your placeholder label.

See this Example

- (void)textViewDidEndEditing:(UITextView *)theTextView
{
    if (![textView hasText])
    {
        lbl.hidden = NO;
    }
}

- (void) textViewDidChange:(UITextView *)textView
{
    if(![textView hasText])
    {
        lbl.hidden = NO;
    }
    else
    {
        lbl.hidden = YES;
    }
}   

I hope this idea is useful for you.

Darshan Kunjadiya
  • 3,323
  • 1
  • 29
  • 31
  • Great idea thanks! It's a shame that a ```UITextView``` doesn't just support a simple placeholder. Oh well, this solution works perfectly :) – Supertecnoboff Sep 13 '15 at 10:05
1

you have to make a label and add it as the subview of your textview. then, in the textview delegate method remove the label at the appropriate time and re-add it again when the textview's text becomes @"";

sj-r
  • 561
  • 2
  • 10
0

Hi have simple solution for that,

Use GCPlaceholderTextView, where you can set placeholder text and color and font.

link for GCPlaceholderTextView

add in GCPlaceholderTextView.h file :

@property(nonatomic, strong) UIFont *placeholderFont;

add in GCPlaceholderTextView.m file :

- (void)setPlaceholderFont:(UIFont *)fontName {
    placeholderFont = fontName;

if ([super.text isEqualToString:self.placeholder]) {
    self.font = placeholderFont;
}
}
Darshan Kunjadiya
  • 3,323
  • 1
  • 29
  • 31
Mehul Sojitra
  • 1,181
  • 9
  • 15