Simulating Uitextfield's placeholder with Uitextview

Source: Internet
Author: User

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

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.