Design of TextView with placeholder

Source: Internet
Author: User

Design of TextView with placeholder

Effect:

Source:

PlaceholderTextView.h and placeholdertextview.m

////PlaceholderTextView.h//Yxtextview////Created by youxianming on 14/12/23.//Copyright (c) 2014 youxianming. All rights reserved.//#import<UIKit/UIKit.h>@interfacePlaceholdertextview:uiview//Gets the string@property (Nonatomic, Strong,ReadOnly) NSString *string;//TextView@property (nonatomic, strong) Uitextview *TextView;//placeholder Characters@property (nonatomic, strong) NSString *placeholderstring;//text Edge left white@property (nonatomic, assign) uiedgeinsets Textcontainerinset;//Color Settings@property (nonatomic, strong) Uicolor *Edittextcolor, @property (nonatomic, strong) Uicolor*Placeholdercolor;//returns whether the key is used to cancel the first responder@property (nonatomic, assign) BOOL Returnbuttontoresignfirstresponder;//Cancel First Responder- (void) Resigntextviewfirstresponder;@end
////placeholdertextview.m//Yxtextview////Created by youxianming on 14/12/23.//Copyright (c) 2014 youxianming. All rights reserved.//#import "PlaceholderTextView.h"@interfacePlaceholdertextview () <UITextViewDelegate>@property (nonatomic, strong) NSString*string;@end@implementationPlaceholdertextview-(Instancetype) initWithFrame: (cgrect) frame{ Self=[Super Initwithframe:frame]; if(self) {[self createtextview]; }    returnSelf ;}- (void) Createtextview {Self.textview=[[Uitextview alloc] initWithFrame:self.bounds]; Self.textview.Delegate=Self ; Self.textView.backgroundColor=[Uicolor Clearcolor]; Self.textView.textColor=[Uicolor Graycolor]; [Self AddSubview:self.textView];}#pragmaMark-Proxy Method-(BOOL) textviewshouldbeginediting: (Uitextview *) TextView {//set edit state text colorTextview.textcolor = (Self.edittextcolor = = nil?)[Uicolor Blackcolor]: self.edittextcolor); //if the text is placeholder text    if([Textview.text isEqualToString:self.placeHolderString]) {Textview.text=@""; }        returnYES;}-(BOOL) textviewshouldendediting: (Uitextview *) TextView {//if the length is 0, the placeholder text is displayed    if(TextView.text.length = =0) {Textview.text=self.placeholderstring; Textview.textcolor= (Self.placeholdercolor = = nil?)[Uicolor Graycolor]: self.placeholdercolor); }        returnYES;}-(BOOL) TextView: (Uitextview *) TextView Shouldchangetextinrange: (nsrange) Range Replacementtext: (NSString *) Text {if(_returnbuttontoresignfirstresponder = =YES) {        if([Text isequaltostring:@"\ n"]) {[TextView resignfirstresponder]; returnNO; }    }        returnYES;}- (void) Resigntextviewfirstresponder {[Self.textview resignfirstresponder];}#pragmaMark-Overriding the Setter,getter method@synthesize string=_string;-(NSString *)string {    if([Self.textView.text isEqualToString:self.placeHolderString]) {return @""; } Else {        returnSelf.textView.text; }}@synthesizePlaceholdercolor =_placeholdercolor;- (void) Setplaceholdercolor: (Uicolor *) Placeholdercolor {_placeholdercolor=Placeholdercolor; Self.textView.textColor=_placeholdercolor;}-(Uicolor *) Placeholdercolor {return_placeholdercolor;}@synthesizePlaceholderstring =_placeholderstring;- (void) Setplaceholderstring: (NSString *) placeholderstring {_placeholderstring=placeholderstring; _textview.text=placeholderstring;}-(NSString *) placeholderstring {return_placeholderstring;}@synthesizeTextcontainerinset =_textcontainerinset;- (void) Settextcontainerinset: (uiedgeinsets) textcontainerinset {_textcontainerinset=Textcontainerinset; _textview.textcontainerinset=Textcontainerinset;}-(uiedgeinsets) textcontainerinset {return_textcontainerinset;}@end

Controller Source:

////VIEWCONTROLLER.M//Yxtextview////Created by youxianming on 14/12/23.//Copyright (c) 2014 youxianming. All rights reserved.//#import "ViewController.h"#import "PlaceholderTextView.h"@interfaceViewcontroller ()@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; Self.view.backgroundColor=[Uicolor Blackcolor]; Placeholdertextview*textview = [[Placeholdertextview alloc] Initwithframe:cgrectmake (-1, -,322, -)];        [Self.view Addsubview:textview]; TextView.layer.borderWidth=1. F; TextView.layer.borderColor=[Uicolor Purplecolor].        Cgcolor; Textview.returnbuttontoresignfirstresponder=YES; TextView.textView.font= [Uifont fontwithname:@"Helveticaneue-thin"Size A. f]; Textview.placeholdercolor=[Uicolor Cyancolor]; Textview.edittextcolor=[Uicolor Redcolor]; Textview.textcontainerinset= Uiedgeinsetsmake (Ten,Ten,Ten,Ten); Textview.placeholderstring=@"Input your name, please ...";}@end

Some details to be aware of:

Design of TextView with placeholder

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.