cdub
cdub

Reputation: 25751

Creating a UITextView placeholder with different colors and fonts

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?

Upvotes: 1

Views: 874

Answers (3)

Darshan Kunjadiya
Darshan Kunjadiya

Reputation: 3329

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.

Upvotes: 6

Mehul Sojitra
Mehul Sojitra

Reputation: 1181

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

Upvotes: 0

sj-r
sj-r

Reputation: 571

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 @"";

Upvotes: 1

Related Questions