In iOS development, for many beginners, it is possible to encounter the color of the placeholder text that needs to be modified Uitextfield, and the color of the cursor when Uitextfield becomes the first responder, then the following small section describes the changes to the placeholder text and the color of the cursor.
1: When you are using storyboard development is, click on the Uitextfield, in the top right corner of the property detector, actually in this case you are not able to modify the placeholder text and cursor color properties.
2: Then go into the Uitextfield agreement to find, but still can't find,
3: In the agent inside to find out, see if there is a proxy method, return color and control the placeholder text method, but still can't find
4: Then you need to click Uitextfield into this class to find out if there are any related attributes, this one has a @property (nullable, nonatomic,copy) nsattributedstring * Attributedplaceholder property, this property is nsattributedstring type, and we just need to use this property to make the text color change.
Nsattributedstring: Text with attributes is also called Rich Text, but this is immutable, once the creation of the completion of the property can not be changed, so similar to the Nsmutablearray and nsarray the same difference, The nsattributedstring also has a corresponding nsmutableattributestring. You can modify the properties in this to achieve the purpose of changing the placeholder text
-(void) Viewdidload {
[Super Viewdidload];
[Self Maketextfield];
Self.view.backgroundColor = [Uicolor Whitecolor];
}
-(void) maketextfield{
1: Instantiate a text input box
Uitextfield *mytextfield = [[Uitextfield alloc]init];
2: Set the properties of the text input box
2.1 Frame with text input box set
Mytextfield.frame = CGRectMake (100, 50, 200, 40);
2.2: Set the border style of the text input box
Mytextfield.borderstyle = Uitextborderstyleroundedrect;
2.3: Set placeholder text for text input box
Mytextfield.placeholder = @ "placeholder text";
Nsmutabledictionary *attrs = [Nsmutabledictionary dictionary];
Attrs[nsforegroundcolorattributename] = [Uicolor redcolor];
Nsattributedstring: Text with attributes (called Rich Text, can make your text rich and colorful) but this is immutable with the attributes of the text, created after the completion of the can not be changed so need to change
nsmutableattributedstring *placeholder = [[nsmutableattributedstring alloc]initwithstring:@ "placeholder text" Attributes:attrs] ;
Mytextfield.attributedplaceholder = PlaceHolder;
3: Add a text input box to the view
[Self.view Addsubview:mytextfield];
}
Operating effect:
You can also use a method of nsmutableattributestring to set the text color by range
-(void) Viewdidload {
[Super Viewdidload];
[Self Maketextfield];
Self.view.backgroundColor = [Uicolor Whitecolor];
}
-(void) maketextfield{
1: Instantiate a text input box
Uitextfield *mytextfield = [[Uitextfield alloc]init];
2: Set the properties of the text input box
2.1 Frame with text input box set
Mytextfield.frame = CGRectMake (100, 50, 200, 40);
2.2: Set the border style of the text input box
Mytextfield.borderstyle = Uitextborderstyleroundedrect;
2.3: Set placeholder text for text input box
Mytextfield.placeholder = @ "placeholder text";
nsmutableattributedstring *placeholder = [[nsmutableattributedstring alloc]initwithstring:@ "placeholder text"];
[PlaceHolder setattributes:@{nsforegroundcolorattributename:[uicolor Greencolor]} range:nsmakerange (0, 1)];
[PlaceHolder setattributes:@{nsforegroundcolorattributename:[uicolor Redcolor]} range:nsmakerange (1, 1)];
[PlaceHolder setattributes:@{nsforegroundcolorattributename:[uicolor Bluecolor]} range:nsmakerange (2, 1)];
Mytextfield.attributedplaceholder = PlaceHolder;
3: Add a text input box to the view
[Self.view Addsubview:mytextfield];
}
@end
Modify the color of the cursor is to modify the Uitextfield Tintcolor
Set the color of the placeholder text for Uitextfield in iOS development, and the color of the cursor