prior to the development encountered Uitextfield added a placeholder problem, directly set up, but this time it is necessary to implement a placeholder in Uitextview, a bit different from before. In the online reference to your predecessors of the solution, there are probably two ways, the first way is very wretched, is directly to Uitextview text assignment, such as the default hint is "blog Park Flyelephant", in Textviewdidchange judge is not " Blog Park Flyelephant ", if it is empty, if not continue to hint, the disadvantage is that the user input content can not be the same as your default hint, the second way to join a uilabel, the same in textviewdidchange judgment, is generally so realized, But some are created textview in the time to create Uilabel, this is understandable, but it is best to abstract out. Inherit Uitextview extension, create a new Feplaceholdertextview:
Header file:
feplaceholdertextview.h// mytextviewdemo//http://www.cnblogs.com/xiaofeixiang// Created by Keso On 15/5/17.// Copyright (c) 2015 Keso. All rights reserved.//#import <UIKit/UIKit.h> @interface feplaceholdertextview:uitextview@property (nonatomic, Retain) NSString *placeholder; @property (nonatomic, retain) Uicolor *placeholdercolor;-(void) textChanged: ( nsnotification*) notification; @end
Implementation file:
feplaceholdertextview.m//mytextviewdemo//http://www.cnblogs.com/xiaofeixiang//Created by Keso on 15/5/17.//Cop Yright (c) 2015 Keso. All rights reserved.//#import "FEPlaceHolderTextView.h" @interface Feplaceholdertextview () @property (nonatomic, Retain) UILabel *placeholderlabel, @end @implementation feplaceholdertextviewcgfloat const UI_PLACEHOLDER_TEXT_ Changed_animation_duration = 0.25;-(void) dealloc{[[Nsnotificationcenter Defaultcenter] removeobserver:self];} -(void) awakefromnib{[Super awakefromnib]; if (!self.placeholder) {[Self setplaceholder:@ "]; } if (!self.placeholdercolor) {[self setplaceholdercolor:[uicolor lightgraycolor]]; } [[Nsnotificationcenter defaultcenter] addobserver:self selector: @selector (textChanged:) Name:uitextviewtextdidcha Ngenotification 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,10)]; _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
Call:
Self.textview=[[feplaceholdertextview Alloc]initwithframe:cgrectmake (Cgrectgetwidth, Self.view.frame)-20, )]; [Email protected] "Blog Park flyelephant\n Blog address: Http://www.cnblogs.com/xiaofeixiang"; Self.textview.layer.bordercolor=[uicolor Lightgraycolor]. Cgcolor; self.textview.layer.borderwidth=1.0; self.textView.scrollEnabled = YES; Self.textView.autoresizingMask = uiviewautoresizingflexibleheight;//Adaptive height self.textView.returnKeyType = Uireturnkeydefault; The type of the return key self.textView.keyboardType = Uikeyboardtypedefault;//keyboard type [Self.view AddSubview:self.textView];
Effect:
iOS development-uitextview How to implement placeholder