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