Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 563 Vote(s) - 3.54 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Placeholder in UITextView

#1
My application uses an `UITextView`. Now I want the `UITextView` to have a placeholder similar to the one you can set for an `UITextField`.

How to do this?
Reply

#2
What you can do is set up the text view with some initial value in the `text` property, and change the `textColor` to `[UIColor grayColor]` or something similar. Then, whenever the text view becomes editable, clear the text and present a cursor, and if the text field is ever empty again, put your placeholder text back. Change the color to `[UIColor blackColor]` as appropriate.

It's not exactly the same as the placeholder functionality in a UITextField, but it's close.
Reply

#3
You could also create a new class TextViewWithPlaceholder as a subclass of UITextView.

(This code is kind of rough -- but I think it's on the right track.)

@interface TextViewWithPlaceholder : UITextView
{

NSString *placeholderText; // make a property
UIColor *placeholderColor; // make a property
UIColor *normalTextColor; // cache text color here whenever you switch to the placeholderColor
}

- (void) setTextColor: (UIColor*) color
{
normalTextColor = color;
[super setTextColor: color];
}

- (void) updateForTextChange
{
if ([self.text length] == 0)
{
normalTextColor = self.textColor;
self.textColor = placeholderColor;
self.text = placeholderText;
}
else
{
self.textColor = normalTextColor;
}

}

In your delegate, add this:

- (void)textViewDidChange:(UITextView *)textView
{
if ([textView respondsToSelector: @selector(updateForTextChange)])
{
[textView updateForTextChange];
}

}


Reply

#4
this is how I did it:

UITextView2.h

#import <UIKit/UIKit.h>

@interface UITextView2 : UITextView <UITextViewDelegate> {
NSString *placeholder;
UIColor *placeholderColor;
}

@property(nonatomic, retain) NSString *placeholder;
@property(nonatomic, retain) UIColor *placeholderColor;

-(void)textChanged:(NSNotification*)notif;

@end

UITextView2.m

@implementation UITextView2

@synthesize placeholder, placeholderColor;

- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
[self setPlaceholder:@""];
[self setPlaceholderColor:[UIColor lightGrayColor]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChanged:) name:UITextViewTextDidChangeNotification object:nil];
}
return self;
}

-(void)textChanged:(NSNotification*)notif {
if ([[self placeholder] length]==0)
return;
if ([[self text] length]==0) {
[[self viewWithTag:999] setAlpha:1];
} else {
[[self viewWithTag:999] setAlpha:0];
}

}

- (void)drawRect:(CGRect)rect {
if ([[self placeholder] length]>0) {
UILabel *l = [[UILabel alloc] initWithFrame:CGRectMake(8, 8, 0, 0)];
[l setFont:self.font];
[l setTextColor:self.placeholderColor];
[l setText:self.placeholder];
[l setAlpha:0];
[l setTag:999];
[self addSubview:l];
[l sizeToFit];
[self sendSubviewToBack:l];
[l release];
}
if ([[self text] length]==0 && [[self placeholder] length]>0) {
[[self viewWithTag:999] setAlpha:1];
}
[super drawRect:rect];
}

- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
}


@end

Reply

#5
You can set the label on the `UITextView` by

[UITextView addSubView:lblPlaceHoldaer];

and hide it on `TextViewdidChange` method.

This is the simple & easy way.
Reply

#6
I made my own version of the subclass of 'UITextView'. I liked **Sam Soffes**'s idea of using the notifications, but I didn't liked the drawRect: overwrite. Seems overkill to me. I think I made a very clean implementation.

You can look at my subclass [here][1]. A demo project is also included.


[1]:

[To see links please register here]

Reply

#7
OK my ansewer is a bit different
I create a small class to do it for you.

TextViewShader.m file

#import "TextViewShader.h"

@implementation TextViewShader
-(id)initWithShadedTextView:(NSString *)text textViewToShade:(UITextView *)textview {
self = [super initWithFrame:textview.frame];
if (self) {
if (shadeLabel==nil)
{
shadeLabel= [[UILabel alloc]initWithFrame:CGRectMake(10, 0, textview.frame.size.width, 30)];


}
shadeLabel.text =text;// @"Enter Your Support Request";
shadeLabel.textColor = [UIColor lightGrayColor];
[textview setDelegate: self];
[textview addSubview:shadeLabel];
}
return self;
}

-(void)textViewDidChange:(UITextView *)textView{
if (textView.text.length==0)
{
shadeLabel.hidden=false;
}
else
{
shadeLabel.hidden=true;
}

}

@end


TextViewShader.h file

#import <UIKit/UIKit.h>

@interface TextViewShader : UIView<UITextViewDelegate>{
UILabel *shadeLabel;

}
-(id)initWithShadedTextView:(NSString *)text textViewToShade:(UITextView *)textview ;
@end



this is the simple one line of code usage (dont forget to add #import "TextViewShader.h")



TextViewShader* shader = [[TextViewShader alloc]initWithShadedTextView:@"Enter Your Support Request" textViewToShade: youruitextviewToshade];


have fun :)

Reply

#8
Simple way to use this within some line of code:

Take one label up to UITextView in .nib
connecting this label to your code ,
After it.

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

if (range.location>0 || text.length!=0) {
placeholderLabel1.hidden = YES;
}else{
placeholderLabel1.hidden = NO;
}
return YES;
}


Reply

#9
- (void)textViewDidChange:(UITextView *)textView
{
placeholderLabel.hidden = YES;
}

put a label over the textview.
Reply

#10
Here's yet another way to do it, one that reproduces the slight indentation of `UITextField`'s placeholder:

Drag a `UITextField` right under the `UITextView` so that their top left corners are aligned. Add your placeholder text to the text field.

In viewDidLoad, add:

[tView setDelegate:self];
tView.contentInset = UIEdgeInsetsMake(-8,-8,0,0);
tView.backgroundColor = [UIColor clearColor];

Then add:

- (void)textViewDidChange:(UITextView *)textView {
if (textView.text.length == 0) {
textView.backgroundColor = [UIColor clearColor];
} else {
textView.backgroundColor = [UIColor whiteColor];
}
}
Reply



Forum Jump:


Users browsing this thread:
2 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through