#import <objc/runtime.h>
#import <objc/message.h>
@interface ViewController ()
@property (nonatomic, strong) UITextView *textView;
@end
@implementation ViewController
- (void)viewDidLoad {
// 通过运行时,发现UITextView有一个叫做“_placeHolderLabel”的私有变量
unsigned int count = 0;
Ivar *ivars = class_copyIvarList([UITextView class], &count);
for (int i = 0; i < count; i++) {
Ivar ivar = ivars[i];
const char *name = ivar_getName(ivar);
NSString *objcName = [NSString stringWithUTF8String:name];
NSLog(@"%d : %@", i, objcName);
}
[self setupTextView];
}
- (void)setupTextView {
UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(0, 10, 200, 100)];
_textView = textView;[_textView setBackgroundColor:[UIColor whiteColor]];
// _placeholderLabel
UILabel *placeHolderLabel = [[UILabel alloc] init];
placeHolderLabel.text = @"请填写地址";
placeHolderLabel.numberOfLines = 0;
placeHolderLabel.textColor = [UIColor colorWithRed:208 / 255.0 green:208 / 255.0 blue:208 / 255.0 alpha:1.0];
[placeHolderLabel sizeToFit];
[_textView addSubview:placeHolderLabel];
[_textView setValue:placeHolderLabel forKey:@"_placeholderLabel"];
}