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