The way to enable Uitextfield to implement placeholder properties in IOS application development _ios

Source: Internet
Author: User
Tags uikit

We all know that the Uitextfield in iOS development has a placeholder attribute, and placeholder can easily guide user input. But Uitextview has no placeholder attribute.

A wretched way.

How to make Uitextview also have placeholder function? Today I would like to share with you a more wretched approach. The idea is probably this:

    • Use the Uitextview text when placeholder.
    • Clears the placeholder in the agent method that starts the edit.
    • In the end of the editing agent method in the set placeholder.

Implementation method:

1. Create Uitextview:

Copy Code code as follows:

Uitextview *textviewplaceholder = [[Uitextview alloc] Initwithframe:cgrectmake (M, screen.width-40, 100)];
Textviewplaceholder.backgroundcolor = [Uicolor Whitecolor];
Textviewplaceholder.text = @ "Jb51.net";
Textviewplaceholder.textcolor = [Uicolor Graycolor];
Textviewplaceholder.delegate = self;
[Self.view Addsubview:textviewplaceholder];

Initializes the Uitextview, assigns a value to the Uitextview's text, and sets the TextColor property of the Uitextview to Gray to make it look more like placeholder.

Don't forget to set up the Uitextview agent, because we need to use the Uitextview two proxy methods later.

2. The agent method to begin editing:

Copy Code code as follows:

-(void) textviewdidbeginediting: (Uitextview *) TextView {

if ([Textview.text isequaltostring:@ "Jb51.net"]) {
Textview.text = @ "";
Textview.textcolor = [Uicolor blackcolor];
}
}


In the beginning of the editing of the proxy method, to determine if the value of Uitextview text is placeholder, then empty text, and TextColor set to the real color of the content, assumed to be black.

3. End the Edit proxy method:

Copy Code code as follows:

-(void) textviewdidendediting: (Uitextview *) TextView {
if (textview.text.length<1) {
Textview.text = @ "Jb51.net";
Textview.textcolor = [Uicolor Graycolor];
}
}

In the end of the edit agent method, to determine if the Uitextview text value is empty, then you need to set the placeholder assigned to Uitextview text, and the TextColor property to Gray.

4. Add a light click gesture

Copy Code code as follows:

UITapGestureRecognizer *tapgesture = [[UITapGestureRecognizer alloc] initwithtarget:self action: @selector (tapgesture :)];
tapgesture.numberoftapsrequired = 1; Number of clicks
tapgesture.numberoftouchesrequired = 1; Click Hand Index
[Self.view Addgesturerecognizer:tapgesture];

Light-click Gesture Trigger method
-(void) Tapgesture: (UITapGestureRecognizer *) sender
{
[Self.view Endediting:yes];
}


At this point, it is very wretched to achieve the placeholder function. To facilitate the test, I added a gesture. The role is to use the keyboard to disappear, so you can test the end of the edit when placeholder will not show.

Demo Address: Iosstrongdemo


second, the usual method
next look at the more common method, haha ~ So, this time I will simply encapsulate a uitextview. For the moment named Ggplaceholdertextview,gg prefix look a bit capricious ha.

Ggplaceholdertextview Introduction:
Ggplaceholdertextview is also the text operation, the specific logic is as follows:

Inherit Uitextview and set the placeholder property:
Register to start editing and ending edit notifications, and then do the appropriate action on text
By uiapplicationwillterminatenotification Notice, remove the notification when app exits.
I wrote the Ggplaceholdertextview down below. However, the micro-mail to see the code is still not very convenient, I have to push the code to: Iosstrongdemo. You can download it down.

Copy Code code as follows:

GGPlaceholderTextView.h

#import <UIKit/UIKit.h>

@interface Ggplaceholdertextview:uitextview
@property (nonatomic, strong) NSString *placeholder;

@end


Defines the placeholder property, similar to Uitextfield.
Copy Code code as follows:

ggplaceholdertextview.m

#import "GGPlaceholderTextView.h"

@implementation Ggplaceholdertextview

-(ID) initWithFrame: (CGRect) Frame {
if (self = [Super Initwithframe:frame]) {
[Self addobserver];
}
return self;
}

-(ID) init {
if (self = [super init]) {
[Self addobserver];
}
return self;
}

-(void) Setplaceholder: (NSString *) placeholder
{
_placeholder = placeholder;
Self.text = placeholder;
Self.textcolor = [Uicolor Graycolor];
}

-(void) addobserver
{
Registration Notice
[[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (didbeginediting:) Name: Uitextviewtextdidbegineditingnotification Object:self];
[[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (didendediting:) Name: Uitextviewtextdidendeditingnotification Object:self];
[[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (Terminate:) Name: Uiapplicationwillterminatenotification object:[uiapplication Sharedapplication]];
}

-(void) Terminate: (nsnotification *) Notification {
Remove notification
[[Nsnotificationcenter Defaultcenter] removeobserver:self];
}

-(void) didbeginediting: (nsnotification *) Notification {
if ([Self.text IsEqualToString:self.placeholder]) {
Self.text = @ "";
Self.textcolor = [Uicolor blackcolor];
}
}

-(void) didendediting: (nsnotification *) Notification {
if (self.text.length<1) {
Self.text = Self.placeholder;
Self.textcolor = [Uicolor Graycolor];
}
}

@end


The above is about the realization of Ggplaceholdertextview, if you have similar needs, directly to use it! Please look down the specific usage.

Practice:

Copy Code code as follows:

Ggplaceholdertextview *textview = [[Ggplaceholdertextview alloc] Initwithframe:cgrectmake (0, $, SCREEN.width, 200)];
Textview.backgroundcolor = [Uicolor Whitecolor];
Textview.placeholder = @ "Jb51.net";
[Self.view Addsubview:textview];


After the encapsulation of the Ggplaceholdertextview, use is not very similar to Uitextfield. Of course, my encapsulation is relatively simple, there are some friends on the GitHub package with placeholder attributes Uitextview. For example: Textviewplaceholder. Interested children's shoes can be tried out.


Related Article

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.