Uitextview implementation of placeholder functions

Source: Internet
Author: User

In iOS we Uitextfield have placeholder function, but Uitextview does not have this method, sometimes we also need to implement such placeholder function, StackOverflow gives us a way to rewrite the Uitextview, a small amount of code to achieve this function, it is worth learning.

#import <UIKit/UIKit.h> @interface Uiplaceholdertextview:uitextview@property (nonatomic, retain) NSString * placeholder; @property (nonatomic, retain) Uicolor *placeholdercolor;-(void) textChanged: (nsnotification*) notification; @end

#import "UIPlaceHolderTextView.h" @interface Uiplaceholdertextview () @property (nonatomic, retain) UILabel * Placeholderlabel, @end @implementation uiplaceholdertextviewcgfloat const Ui_placeholder_text_changed_animation_ DURATION = 0.25;-(void) dealloc{[[Nsnotificationcenter Defaultcenter] removeobserver:self]; #if __has_feature (objc_ ARC) #else [_placeholderlabel release];    _placeholderlabel = nil; [_placeholdercolor release];    _placeholdercolor = nil; [_placeholder release];    _placeholder = nil;        [Super Dealloc]; #endif}-(void) awakefromnib{[Super awakefromnib]; Use Interface Builder User Defined Runtime Attributes to set//placeholder and placeholdercolor in Interface Builde    R. if (!self.placeholder) {[Self setplaceholder:@ "];    } if (!self.placeholdercolor) {[self setplaceholdercolor:[uicolor lightgraycolor]]; } [[Nsnotificationcenter defaultcenter] addobserver:self selector: @selector (textChanged:) Name:uitextviewtextdidchaNgenotification Object:nil];}        -(ID) initWithFrame: (CGRect) frame{if (self = [super Initwithframe:frame])} {[Self setplaceholder:@ "];        [Self Setplaceholdercolor:[uicolor lightgraycolor]]; [[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (textChanged:) Name:    Uitextviewtextdidchangenotification Object:nil]; } return self;}    -(void) textChanged: (nsnotification *) notification{if ([[Self placeholder] length] = = 0) {return; } [UIView animatewithduration:ui_placeholder_text_changed_animation_duration animations:^{if ([[Self TEXT] L        Ength] = = 0) {[[Self viewwithtag:999] setalpha:1];        } else {[self viewwithtag:999] setalpha:0]; }    }];}    -(void) SetText: (NSString *) text {[Super Settext:text]; [Self textchanged:nil];}        -(void) DrawRect: (CGRect) rect{if ([[Self placeholder] length] > 0) {if (_placeholderlabel = = nil)       {     _placeholderlabel = [[UILabel alloc] Initwithframe:cgrectmake (8,8,self.bounds.size.width-16,0)];            _placeholderlabel.linebreakmode = nslinebreakbywordwrapping;            _placeholderlabel.numberoflines = 0;            _placeholderlabel.font = Self.font;            _placeholderlabel.backgroundcolor = [Uicolor Clearcolor];            _placeholderlabel.textcolor = Self.placeholdercolor;            _placeholderlabel.alpha = 0;            _placeholderlabel.tag = 999;        [Self Addsubview:_placeholderlabel];        } _placeholderlabel.text = Self.placeholder;        [_placeholderlabel SizeToFit];    [Self Sendsubviewtoback:_placeholderlabel];  if ([self text] length] = = 0 && [[self placeholder] length] > 0) {[[Self viewwithtag:999]    SETALPHA:1]; } [Super Drawrect:rect];} @end




Uitextview implementation of placeholder functions

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.