The placeholder property in Ios&uitextview

Source: Internet
Author: User

A look at the title, it is very cock silk!

Yes, the system is not for us, so let's do it ourselves!

Specific steps:

1, create a class, inherit Uitextview. Name Zhhtextview;

2, in DrawRect: Implement the placeholder, which uses the notification to listen to the change of text.

The approximate step is two steps, concrete implementation, look at the code. < A line of code to solve thousands of words >

Now publish the. m file code as follows:

#import "ZHHTextView.h"

@implementation Zhhtextview

-(Instancetype) initWithFrame: (CGRect) Frame {

if (self = [Super Initwithframe:frame]) {

NSLog (@ "%s", __func__);

Registration notice. The purpose of the notification: The change of listening content

[[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (viewtextchange) Name: Uitextviewtextdidchangenotification Object:nil];

}

return self;

}

-(void) Viewtextchange {

To execute this method, the system automatically calls DrawRect: Method

[Self setneedsdisplay];

NSLog (@ "%s", __func__);

}

-(void) Dealloc {

Cancel the notice, you do not cancel to try to see if your project will collapse

[[Nsnotificationcenter Defaultcenter] removeobserver:self];

}

Only override Drawrect:if perform custom drawing.

An empty implementation adversely affects performance during animation.

-(void) DrawRect: (cgrect) Rect {

Drawing Code

If there is content, the return

if (Self.hastext) return;

adding attributes to Placeholder

Nsmutabledictionary *attributes = [Nsmutabledictionary dictionary];

It's better to sync with Self.font here.

Attributes[nsfontattributename] = [Uifont systemfontofsize:15.0];

Attributes[nsforegroundcolorattributename] = [Uicolor graycolor];

In fact, placeholder is painted, since it is painted, must be given the specific location and size

CGFloat x = 5;

CGFloat w = rect.size.width-2 * x;

CGFloat y = 8;

CGFloat h = rect.size.height-2 * y;

CGRect Placeholderrect = CGRectMake (x, Y, W, h);

nsstring* placeholder = @ "Please enter the QQ of Hung song ...";

This is a classic method.

[Placeholder Drawinrect:placeholderrect withattributes:attributes];

}

@end

It's OK.

The placeholder property in Ios&uitextview

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.