@interface Bspublishtextview:uitextview
/** External Property Placeholder character placeholder */
@property (nonatomic, copy) NSString *placeholder;
/** External Property Placeholder Color */
@property (nonatomic, strong) Uicolor *placeholdercolor;
@end
@interface Bspublishtextview ()
/** Placeholder character label */
@property (nonatomic, weak) UILabel *placeholderlabel;
@end
@implementation Bspublishtextview
-(UILabel *) placeholderlabel{
if (!_placeholderlabel) {
UILabel *placeholderlabel = [[UILabel alloc]init];
Placeholderlabel.backgroundcolor = [Uicolor Greencolor];
Placeholderlabel.numberoflines = 0;
placeholderlabel.x = 4;
PLACEHOLDERLABEL.Y = 7;
[Self Addsubview:placeholderlabel];
_placeholderlabel = Placeholderlabel;
}
return _placeholderlabel;
}
-(Instancetype) initWithFrame: (CGRect) frame
{
self = [super Initwithframe:frame];
if (self) {
Permanent vertical with spring effect
self.alwaysbouncevertical = YES;
Self.font = [Uifont systemfontofsize:15];
Self.placeholdercolor = [Uicolor Graycolor];
Registration notice when you receive uitextviewtextdidchangenotification TextView text changes
[[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (textchange) Name: Uitextviewtextdidchangenotification Object:nil];
}
return self;
}
Notification Invocation method
-(void) textchange{
Self.placeholderLabel.hidden = Self.hastext;
}
-(void) dealloc{
[[Nsnotificationcenter defaultcenter]removeobserver:self];
}
-(void) layoutsubviews{
[Super Layoutsubviews];
Layoutsubviews get Uitextview width must be correct, here if the outside even change the width of the uitextview here can be counted right
Set the width of the Placeholderlabel placeholder character first
Self.placeholderLabel.width = self.width-2 * self.placeholderlabel.x;
The size of the Placeholderlabel placeholder character varies with the size of the content inside
[Self.placeholderlabel SizeToFit];
}
Setneedsdisplay method: Automatically calls DrawRect at the right moment: Method
Setneedslayout method: The Layoutsubviews method is called at the right time
-(void) Setplaceholdercolor: (Uicolor *) placeholdercolor{
_placeholdercolor = Placeholdercolor;
Self.placeholderLabel.textColor = Placeholdercolor;
}
-(void) Setplaceholder: (NSString *) placeholder{
_placeholder = [placeholder copy];
Self.placeholderLabel.text = placeholder;
[Self setneedslayout]; Will call the Layoutsubviews method at the right time. The code is not written.
}
-(void) SetFont: (Uifont *) font{
[Super Setfont:font];
Self.placeholderLabel.font = font;
[Self setneedslayout]; Will call the Layoutsubviews method at the right time. The code is not written.
}
-(void) SetText: (NSString *) text{
[Super Settext:text];
[Self textchange];
}
-(void) Setattributedtext: (nsattributedstring *) attributedtext{
[Super Setattributedtext:attributedtext];
[Self textchange];
}
@end
Use Uilabel to implement Uitextview placeholder text in iOS