Simulating Uitextfield's placeholder with Uitextview
Effect:
Source:
////VIEWCONTROLLER.M//TextView////Created by youxianming on 14/12/18.//Copyright (c) 2014 youxianming. All rights reserved.//#import "ViewController.h"StaticNSString *placeholderstr =@"User Name";@interfaceViewcontroller () <UITextViewDelegate>@property (nonatomic, strong) Uitextview*TextView, @property (nonatomic, strong) UIButton*button;@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; //button for reactionSelf.button =[[UIButton alloc] initWithFrame:self.view.bounds]; [Self.button addtarget:self Action: @selector (buttonevent) Forcontrolevents:uicontroleventtouch Upinside]; [Self.view AddSubview:self.button]; //TextViewSelf.textview = [[Uitextview alloc] Initwithframe:cgrectmake (0, -, the, $)]; Self.textView.layer.borderWidth=1. F; Self.textView.layer.borderColor=[Uicolor Graycolor]. Cgcolor; Self.textview.Delegate=Self ; Self.textView.text=Placeholderstr; Self.textView.font= [Uifont systemfontofsize: -. f]; Self.textView.textColor=[Uicolor Graycolor]; [Self.view AddSubview:self.textView];}#pragmaMark-Proxy Method-(BOOL) textviewshouldbeginediting: (Uitextview *) TextView {//set edit state text colorTextview.textcolor =[Uicolor Blackcolor]; //if the text is placeholder text if([Textview.text isequaltostring:placeholderstr]) {Textview.text=@""; } returnYES;}-(BOOL) textviewshouldendediting: (Uitextview *) TextView {//if the length is 0, the placeholder text is displayed if(TextView.text.length = =0) {Textview.text=Placeholderstr; Textview.textcolor=[Uicolor Graycolor]; } returnYES;}/** * Reaction button*/- (void) buttonevent {[Self.textview resignfirstresponder];}@end
Core code:
Simulating Uitextfield's placeholder with Uitextview