本文主要是介绍借用runtime来实现UITextView的占位符placehold功能,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1、 首先我们需要倒入runtime的头文件 #import <objc/runtime.h>
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);
#warning 通过打印可以发现 uitextField 有 ‘_placeholderLabel’占位符属性}
通过打印我没可以看到UITextField是存在placeholder属性的
- 布局UITextField
- (void)setupTextView
{CGFloat margin = 15;UILabel *tipLabel = [[UILabel alloc]initWithFrame:CGRectMake(margin, 0, SCREEN_width - 2 * margin, 50)];tipLabel.text = @"你的批评和建议能帮助我们更好的完善产品,请留下你的宝贵意见!";tipLabel.numberOfLines = 2;tipLabel.textColor = [UIColor colorWithRed:255 green:10 blue:10 alpha:1];tipLabel.font = [UIFont systemFontOfSize:16];[self.view addSubview:tipLabel];CGFloat height = 200;
#ifndef __IPHONE_4_0height = 100;
#endifUITextView *iderTextView = [[UITextView alloc] initWithFrame:CGRectMake(margin, CGRectGetMaxY(tipLabel.frame) + margin, SCREEN_width - 2 * margin, height)];iderTextView.backgroundColor = [UIColor whiteColor];iderTextView.layer.borderColor = [UIColor lightGrayColor].CGColor;iderTextView.layer.borderWidth = 1;iderTextView.scrollEnabled = YES;iderTextView.scrollsToTop = YES;iderTextView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;// iderTextView.delegate = self;self.iderTextView = iderTextView;[self.view addSubview:iderTextView];// _placeholderLabelUILabel *placeHolderLabel = [[UILabel alloc] init];placeHolderLabel.text = @"请输入宝贵意见(300字以内)";placeHolderLabel.numberOfLines = 0;placeHolderLabel.font = [UIFont systemFontOfSize:14];placeHolderLabel.textColor = [UIColor lightGrayColor];[placeHolderLabel sizeToFit];[iderTextView addSubview:placeHolderLabel];
#warning 通过‘_placeholderLabel’属性来设置他的占位符[iderTextView setValue:placeHolderLabel forKey:@"_placeholderLabel"];iderTextView.font = placeHolderLabel.font;
}
这篇关于借用runtime来实现UITextView的占位符placehold功能的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!