RunTime in iOS app with Uitextfield placeholder placeholder custom

Source: Internet
Author: User

Runtime is a relatively low-level pure C language API, belongs to 1 C language Library, contains a lot of the underlying C language API, in the OC code that we usually write, the program runs the process, in fact, is actually turned into the runtime C language code, runtime is OC's behind-the-scenes workers.

Sometimes we want to change a property of a UI control, to see if the header file is not found, when we use RunTime to see the control of some hidden or some system Apple does not want users to modify the properties

For example, we want to see some properties of Uitextfield

It's in his. m file.

Import Header File

#import <objc/runtime.h>

Use this method when you check it.

+ (void) initialize{

unsigned int count = 0;

Copies all the member variable lists Ivars is a pointer to this array and a pointer to the first element

Ivar *ivars = Class_copyivarlist ([Uitextfield class], &count);

for (int i = 0; i < count; ++i) {

The array name is actually a pointer to the first element of the array. If the pointer is to the first element of the array, you can use the pointer as an array

Ivar t = ivars[i];

Print member variable name find to _placeholderlabel

NSLog (@ "%s", Ivar_getname (t));

}

Release pointer variable ivars is copied so run out to release

Free (ivars);

}

-(void) awakefromnib{

[Super awakefromnib];

Self.textcolor = [Uicolor Whitecolor];

Tintcolor can set the color of the cursor, etc.

Self.tintcolor = Self.textcolor;

When loading this textfield, make the inside placeholder gray and let the text input box lose focus Z (because the text is dimmed when the override loses focus)

[Self resignfirstresponder];

}

When the text input box becomes the first responder, it calls the

-(BOOL) becomefirstresponder{

[Self setValue:self.textColor forkeypath:@ "_placeholderlabel.textcolor"];

return [Super Becomefirstresponder];

}

A method that is called when the text input box loses focus

-(BOOL) resignfirstresponder{

Set color by KVC Access _placeholderlabel.textcolor property

[Self setvalue:[uicolor graycolor] forkeypath:@ "_placeholderlabel.textcolor"];

return [Super Resignfirstresponder];

}

RunTime in iOS app with Uitextfield placeholder placeholder custom

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.