纯代码代码创建textView 输入文字时出现文字不从第一行显示问题 以及textField的初始化
2015-12-04 16:09
471 查看
纯代码代码创建textView 输入文字时出现文字不从第一行显示问题
默默的查了一下,原来是controller的影响,但是从storyboard 拖得控件就不会这样,修改一下控制器的属性
self.automaticallyAdjustsScrollViewInsets = NO;
就解决了。会受到状态栏,导航栏,以及工具条,tabBar的影响 自动添加内部的insets
A Boolean value that indicates whether the view controller should automatically adjust its scroll view insets.
The default value of this property is YES,
which allows the view controller to adjust its scroll view insets in response to the screen areas consumed by the status bar, navigation bar, and toolbar or tab bar. Set to NO if
you want to manage scroll view inset adjustments yourself, such as when there is more than one scroll view in the view hierarchy.
附textView的placeholder效果
实现方法一:
#pragma mark 代理监听 textView的改变
- (void)textViewDidBeginEditing:(UITextView *)textView
{
if ([textView.textisEqualToString:@"请输入内容"])
{
textView.text =
nil;
textView.textColor = [UIColorblackColor];
// self.automaticallyAdjustsScrollViewInsets =NO;
}
NSLog(@"%s",__func__);
return;
}
- (BOOL)textViewShouldEndEditing:(UITextView *)textView
{
if ([textView.textisEqualToString:@""] ) {
textView.text =
@"请输入内容";
textView.textColor =
CFColor(213,
213, 213);
}
return
YES;
}
- (void)textViewDidEndEditing:(UITextView *)textView
{
if ([textView.textisEqualToString:@""] ) {
textView.text =
@"请输入内容";
textView.textColor =
CFColor(213,
213, 213);
}
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
if ([self.contentField.textisEqualToString:@""]
) {
self.contentField.text =@"请输入内容";
self.contentField.textColor =CFColor(213,213,
213);
[self.contentFieldresignFirstResponder];
}
NSLog(@"touchesBegan");
}
实现方式二:
UITextView上如何加上类似于UITextField的placeholder呢,其实在UITextView上加上一个UILabel,然后再实现
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text这个代理方法就可以了。
具体实现如下:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if (![text isEqualToString:@""]) {
_label1.hidden = YES;
}
if ([text isEqualToString:@""] && range.location == 0 && range.length == 1) {
_label1.hidden = NO;
}
return YES;
}
(1) _label1
是加在UITextView上的UILabel _label1.text = @"请输入内容";
(2) [text isEqualToString:@""] 表示输入的是退格键
(3) range.location == 0 && range.length == 1
表示输入的是第一个字符
textField的代码初始化特殊需求:(storyboard可以设置的就能用代码实现)
self.registerPwdField.clearButtonMode = UITextFieldViewModeWhileEditing;//账号文本框文本框
self.registerPwdField.secureTextEntry = YES;//密码文本框
self.registerPwdField.keyboardType = UIKeyboardTypePhonePad;// 文本框弹出的键盘的类型
默默的查了一下,原来是controller的影响,但是从storyboard 拖得控件就不会这样,修改一下控制器的属性
self.automaticallyAdjustsScrollViewInsets = NO;
就解决了。会受到状态栏,导航栏,以及工具条,tabBar的影响 自动添加内部的insets
A Boolean value that indicates whether the view controller should automatically adjust its scroll view insets.
The default value of this property is YES,
which allows the view controller to adjust its scroll view insets in response to the screen areas consumed by the status bar, navigation bar, and toolbar or tab bar. Set to NO if
you want to manage scroll view inset adjustments yourself, such as when there is more than one scroll view in the view hierarchy.
附textView的placeholder效果
实现方法一:
#pragma mark 代理监听 textView的改变
- (void)textViewDidBeginEditing:(UITextView *)textView
{
if ([textView.textisEqualToString:@"请输入内容"])
{
textView.text =
nil;
textView.textColor = [UIColorblackColor];
// self.automaticallyAdjustsScrollViewInsets =NO;
}
NSLog(@"%s",__func__);
return;
}
- (BOOL)textViewShouldEndEditing:(UITextView *)textView
{
if ([textView.textisEqualToString:@""] ) {
textView.text =
@"请输入内容";
textView.textColor =
CFColor(213,
213, 213);
}
return
YES;
}
- (void)textViewDidEndEditing:(UITextView *)textView
{
if ([textView.textisEqualToString:@""] ) {
textView.text =
@"请输入内容";
textView.textColor =
CFColor(213,
213, 213);
}
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
if ([self.contentField.textisEqualToString:@""]
) {
self.contentField.text =@"请输入内容";
self.contentField.textColor =CFColor(213,213,
213);
[self.contentFieldresignFirstResponder];
}
NSLog(@"touchesBegan");
}
实现方式二:
UITextView上如何加上类似于UITextField的placeholder呢,其实在UITextView上加上一个UILabel,然后再实现
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text这个代理方法就可以了。
具体实现如下:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if (![text isEqualToString:@""]) {
_label1.hidden = YES;
}
if ([text isEqualToString:@""] && range.location == 0 && range.length == 1) {
_label1.hidden = NO;
}
return YES;
}
(1) _label1
是加在UITextView上的UILabel _label1.text = @"请输入内容";
(2) [text isEqualToString:@""] 表示输入的是退格键
(3) range.location == 0 && range.length == 1
表示输入的是第一个字符
textField的代码初始化特殊需求:(storyboard可以设置的就能用代码实现)
self.registerPwdField.clearButtonMode = UITextFieldViewModeWhileEditing;//账号文本框文本框
self.registerPwdField.secureTextEntry = YES;//密码文本框
self.registerPwdField.keyboardType = UIKeyboardTypePhonePad;// 文本框弹出的键盘的类型
相关文章推荐
- django-forms
- firefox php 验证码图片 多次请求问题 待解决
- ASP.NET MVC5-ModelStateDictionary类ModelState模型验证
- windows下C++程序命令行参数读取
- 快速高效学习Java编程在线资源Top 20(转载)
- Bean的作用域
- PHP提示Deprecated: mysql_connect(): The mysql extension is deprecated的解决方法
- 常用C语言函数 - strncpy()
- Asp.Net
- C# 文档注释规范
- 从pyh看Python的工厂模式
- Sping AOP用AspectJ注解的方式拦截不到SpringMVC的controller方法?
- python__The _imaging C module is not installed
- Spring MVC 中 HandlerInterceptorAdapter拦截器的使用
- django如何防止csrf(跨站请求伪造)
- 它可以作为一个代理server或者转发java类
- JiaThis™“分享到”图标式按钮代码
- 常用C语言函数 - strcmp()
- 从头认识java-12.2 Class对象(2)-使用与注意点
- Java项目如何记录日志