Use Uilabel to implement Uitextview placeholder text in iOS

Source: Internet
Author: User

@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

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.