iOS
来源:互联网 发布:王者荣耀红莲斗篷数据 编辑:程序博客网 时间:2024/06/05 22:35
一、使用KVC,查找textView内部变量
UITextView
1.新建UITextView的Category,命名为UITextView+PlaceHolder,.h和.m代码分别如下#import <UIKit/UIKit.h>@interface UITextView (Placeholder)@property (nonatomic, copy) NSString *placeHolder;@end
#import "UITextView+Placeholder.h"@implementation UITextView (Placeholder)- (void)setPlaceHolder:(NSString *)placeHolder { UILabel *placeHolderStr = [[UILabel alloc] init]; placeHolderStr.text = placeHolder; placeHolderStr.numberOfLines = 0; placeHolderStr.textColor = [UIColor lightGrayColor]; [placeHolderStr sizeToFit]; [self addSubview:placeHolderStr]; placeHolderStr.font = self.font; [self setValue:placeHolderStr forKey:@"_placeholderLabel"];}@end
2.调用时引入头文件后- (void)textViewplaceHolderMethod { self.textView.layer.masksToBounds = YES; self.textView.layer.cornerRadius = 4; self.textView.layer.borderWidth = 0.5; self.textView.layer.borderColor = [UIColor blackColor].CGColor; self.textView.placeHolder = @"请留下您对我们的建议或意见";}
1.具体思路:在UITextView的delegate方法中控制
self.textView.text = @"";或者
self.placeHolderLabel.hidden = Yes;2.具体代码(UITextView.text)
- (void)setupTextView { self.textView.delegate = self; self.textView.text = @"请输入您的建议"; self.textView.textColor = [UIColor lightGrayColor]; self.textView.layer.masksToBounds = YES; self.textView.layer.cornerRadius = 4; self.textView.layer.borderWidth = 0.5; self.textView.layer.borderColor = [UIColor blackColor].CGColor;}- (void)textViewDidBeginEditing:(UITextView *)textView { if ([textView.text isEqualToString:@"请输入您的建议"]) { self.textView.text = @""; self.textView.textColor = [UIColor blackColor]; }}- (void)textViewDidEndEditing:(UITextView *)textView { if (textView.text.length == 0) { self.textView.text = @"请输入您的建议"; self.textView.textColor = [UIColor lightGrayColor]; }}- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { [self.view endEditing:YES];}
注:这个方法的缺点是当你输入 “请输入您的建议” 后,键盘失去响应后,在点击想继续输入的时候,原输入的 “请输入您的建议” 会消失,所以比较推荐方法一。
阅读全文
1 0
- iOS
- iOS
- IOS
- iOS
- iOS
- IOS
- ios
- iOS
- iOS
- IOS
- iOS
- ios
- ios ~~~~~
- ios
- IOS
- IOS
- IOS
- ios
- 系统测试与集成测试的区别
- (一个坑)关于ViewPager滑动监听的一只
- vue路由 遍历生成复数router-link
- 类加载机制的奥妙
- Java多线程(六):线程池
- iOS
- 面向对象程序设计上机练习七(类和对象)
- Android常见的面试题总结(四)
- http请求中的方法
- c++ 循环简单说(for)(读书体会)
- Scratch第一课:让小猫走起来
- 微信公众平台PHP源代码
- RF+Selenium2Library元素定位不到的问题
- 什么是回调函数?