A method in iOS application development that enables UITextField to implement placeholder properties

  • 2020-05-30 21:09:48
  • OfStack

We all know that UITextField in iOS development has an placeholder property, and placeholder makes it easy to direct user input. But UITextView has no placeholder property.

1. Obscene methods

How do you get UITextView to have placeholder? Today I would like to share with you a rather obscene practice. The idea goes something like this:

Use text of UITextView as placeholder. Clear placeholder in the agent method to start editing. Set placeholder in the end edit agent method.

Implementation method:

1. Create UITextView:


UITextView *textViewPlaceholder = [[UITextView alloc] initWithFrame:CGRectMake(20, 70, SCREEN.width - 40, 100)];
textViewPlaceholder.backgroundColor = [UIColor whiteColor];
textViewPlaceholder.text = @"ofstack.com";
textViewPlaceholder.textColor = [UIColor grayColor];
textViewPlaceholder.delegate = self;
[self.view addSubview:textViewPlaceholder];

Initialize UITextView, assign text of UITextView, and gray textColor of UITextView so that it looks more like placeholder.

Don't forget to set up UITextView's proxy, as we will use the two proxy methods of UITextView later.

2. Agent method to start editing:


- (void)textViewDidBeginEditing:(UITextView *)textView {     if ([textView.text isEqualToString:@"ofstack.com"]) {
        textView.text = @"";
        textView.textColor = [UIColor blackColor];
    }
}

In the agent method to start editing, determine that if UITextView's text value is placeholder, then clear text and set textColor to the true content color, assuming black.

3. Agent method for ending editing:


- (void)textViewDidEndEditing:(UITextView *)textView {
    if (textView.text.length<1) {
        textView.text = @"ofstack.com";
        textView.textColor = [UIColor grayColor];
    }
}

In the closing agent method, determine that if the text value of UITextView is null, assign the placeholder you need to set to text of UITextView, and set the textColor property to gray.

4. Add a tap gesture


UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
tapGesture.numberOfTapsRequired = 1; // Click on the number of times
tapGesture.numberOfTouchesRequired = 1; // Click hand index
[self.view addGestureRecognizer:tapGesture]; // Tap gesture trigger method
-(void)tapGesture:(UITapGestureRecognizer *)sender
{
    [self.view endEditing:YES];
}

At this point, it is very obscene to achieve the placeholder function. To facilitate the test, I added a gesture. The function is to disappear with the keyboard, so that you can test whether placeholder will be displayed at the end of editing.

Demo address: iOSStrongDemo


2. The usual way
Let's take a look at some common methods. Haha ~ so, this time I will simply encapsulate an UITextView. I'm going to call it GGPlaceholderTextView, GG looks a little bit capricious.

GGPlaceholderTextView introduction:
GGPlaceholderTextView is also an operation on text. The specific logic is as follows:

Inherit UITextView and set placeholder property:
Register the start and end edit notifications, and then do something about text
Remove notifications when APP exits via UIApplicationWillTerminateNotification notifications.
Let me write GGPlaceholderTextView down here. However, it is still not convenient to see the code in WeChat, I have already put the code push to :iOSStrongDemo. You can download it.


GGPlaceholderTextView.h #import <UIKit/UIKit.h> @interface GGPlaceholderTextView : UITextView
@property(nonatomic, strong) NSString *placeholder; @end

Define the placeholder property, similar to UITextField.

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
    [[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 the 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 the implementation of GGPlaceholderTextView, if you have similar requirements, just use it! See below for more details.

Practice:


GGPlaceholderTextView *textView = [[GGPlaceholderTextView alloc] initWithFrame:CGRectMake(0, 64, SCREEN.width , 200)];
textView.backgroundColor = [UIColor whiteColor];
textView.placeholder = @"ofstack.com";
[self.view addSubview:textView];

After packaging, GGPlaceholderTextView is not very similar to UITextField in use. Of course, it's easy for me to package, and there are some friends on github that package UITextView with placeholder attribute. For example: TextViewPlaceholder. Interested in the children's shoes can go to try 1.



Related articles: