Placeholder of uitextfield and uitextview

Source: Internet
Author: User


1. We all know that uitextfield supports setting placeholder and can change the placeholder font size and color. Refer to the Code:

/* Set placeholder */[textfield setplaceholder: @ "Placeholder in textfield"];/* change the color of placeholder */[textfield setvalue: [uicolor redcolor] forkeypath: @ "_ placeholderlabel. textcolor "];/* change the font size of placeholder */[textfield setvalue: [uifont systemfontofsize: effecf] forkeypath: @" _ placeholderlabel. font "];

2. uitextview does not support placeholder, but it's okay. You can manually add a label to set the font size and color. You only need to operate the label:

/* Add uitextview */uitextview * textview = [[uitextview alloc] initwithframe: cgrectmake (0, 0, 320.f, 47.f)]; textview. delegate = self; // textview. backgroundcolor = [uicolor clearcolor]; [self addsubview: textview];/* Add placeholder label */uilabel * placeholderlabel = [[uilabel alloc] initwithframe: cgrectzero]; placeholderlabel. textcolor = [uicolor lightgraycolor]; placeholderlabel. TEXT = @ "Placeholder in textview"; [placeholderlabel sizetofit]; placeholderlabel. frame = cgrectmake (0, 5.f, placeholderlabel. frame. size. width, placeholderlabel. frame. size. height); [self addsubview: placeholderlabel];/* listens for textview changes. If no content is displayed, placeholder */-(void) textviewdidchange :( uitextview *) textview {If ([textview. text stringbytrimmingcharactersinset: [nscharacterset whitespaceandnewlinecharacterset] is%tostring: @ ""]) {placeholderlabel. TEXT = @ "Placeholder in textview";} else {placeholderlabel. TEXT = @"";}}


Placeholder of uitextfield and 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.