How to add default text to Uitextview in iOS development

Source: Internet
Author: User

With the placeholder property in Uitextfield, you can use it to prompt for input box information. But Uitextview does not have this feature
Two methods are introduced to achieve:
The first type:
Initialize Uitextview
Define Uitextview First
Uitextview *textview = [[Uitextview alloc] init];
Textview.font = [Uifont systemfontofsize:14];
Textview.frame =cgrectmake (Ten, 0, cell.contentview.bounds.size.width-20, side);
Textview.autoresizingmask = Uiviewautoresizingflexibleheight | Uiviewautoresizingflexiblewidth;
Textview.backgroundcolor = [Uicolor Whitecolor];
[Cell.contentview Addsubview:textview];
Textview.hidden = NO;
Textview.delegate = self;
Next, the Uitextview overrides a uilable,uilable set to global variables.
Uilabel.frame =cgrectmake (8, cell.contentview.bounds.size.width-side+10, 20);
Uilabel.text = @ "Please fill in the approval comments ...";
uilabel.enabled = no;//lable must be set to not available
Uilabel.backgroundcolor = [Uicolor Clearcolor];
[Cell.contentview Addsubview:uilabel];
Implementing the Uitextview Agent
-(void) Textviewdidchange: (Uitextview *) TextView
{
Self.examinetext = Textview.text;
if (TextView.text.length = = 0) {
Uilabel.text = @ "Please fill in the approval comments ...";
}else{
Uilabel.text = @ "";
}
}

The second type:
Uitextview implements placeholder and hides the keyboard

#import <Foundation/Foundation.h>
@interface Uiplaceholdertextview:uitextview {
NSString *placeholder;
Uicolor *placeholdercolor;

@private
UILabel *placeholderlabel;
}

@property (nonatomic, retain) UILabel *placeholderlabel;
@property (nonatomic, retain) NSString *placeholder;
@property (nonatomic, retain) Uicolor *placeholdercolor;

-(void) textChanged: (nsnotification*) notification;

@end

#import "UIPlaceHolderTextView.h"
@implementation Uiplaceholdertextview
@synthesize Placeholderlabel;
@synthesize placeholder;
@synthesize Placeholdercolor;

-(void) dealloc
{
[[Nsnotificationcenter Defaultcenter] removeobserver:self];
[Placeholderlabel release]; Placeholderlabel = nil;
[Placeholdercolor release]; Placeholdercolor = nil;
[Placeholder release]; placeholder = nil;
[Super Dealloc];
}

-(void) awakefromnib
{
[Super awakefromnib];
[Self setplaceholder:@ "];
[Self Setplaceholdercolor:[uicolor lightgraycolor]];
[[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (textChanged:) Name: Uitextviewtextdidchangenotification Object:nil];
}

-(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 *) notification
{
if ([[Self placeholder] length] = = 0)
{
Return
}

if ([[Self text] length] = = 0)
{
[[Self viewwithtag:999] setalpha:1];
}
Else
{
[[Self viewwithtag:999] setalpha:0];
}
}

-(void) SetText: (NSString *) Text {
[Super Settext:text];
[Self textchanged:nil];
}

-(void) DrawRect: (cgrect) rect
{
if ([[Self placeholder] length] > 0)
{
if (Placeholderlabel = = nil)
{
Placeholderlabel = [[UILabel alloc] Initwithframe:cgrectmake (8,8,self.bounds.size.width-16,0)];
Placeholderlabel.linebreakmode = Uilinebreakmodewordwrap;
Placeholderlabel.numberoflines = 0;
Placeholderlabel.font = Self.font;
Placeholderlabel.backgroundcolor = [Uicolor Clearcolor];
Placeholderlabel.textcolor = Self.placeholdercolor;
Placeholderlabel.alpha = 0;
Placeholderlabel.tag = 999;
[Self Addsubview:placeholderlabel];
}

Placeholderlabel.text = Self.placeholder;
[Placeholderlabel SizeToFit];
[Self Sendsubviewtoback:placeholderlabel];
}

if ([self text] length] = = 0 && [[self placeholder] length] > 0)
{
[[Self viewwithtag:999] setalpha:1];
}

[Super Drawrect:rect];
}
@end

Hide the keyboard for uitextviewdelegate
-(BOOL) TextView: (Uitextview *) TextView Shouldchangetextinrange: (nsrange) Range Replacementtext: (nsstring*) text
{
if ([text isequaltostring:@ "\ n"]) {
[M_textview Resignfirstresponder];
return NO;
}
return YES;
}

How to add default text to Uitextview in iOS development

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.