iOS中UITextView遇到的问题总结

来源:互联网 发布:透视变换算法 编辑:程序博客网 时间:2024/04/30 07:14

前言

由于iOS中的UITextField不支持文本换行,所以在有换行需求时,我们只好用UITextView。

以下是在使用UITextView时很容易遇到的一些问题。

问题一:UITextView显示边框

UITextView默认是没有边框颜色的,没有直接的属性来设置边框颜色。

可以使用layer属性来解决,代码如下

[objc] view plain copy
  1. //设置边框宽度  
  2. self.textView.layer.borderWidth = 1.0;  
  3. //设置边框颜色  
  4. self.textView.layer.borderColor = [UIColor grayColor].CGColor;  
  5. //设置圆角  
  6. self.textView.layer.cornerRadius = 5.0;  

问题二:UITextView居中了,怎么居上?


实际上,UITextView的文本默认就是居上显示的,出现上面的情况很多都是因为在iOS7下使用了navigationController让scrollView自动适应屏幕造成的。(UITextView继承自UIScrollView)。

所以解决方案如以下代码:

[objc] view plain copy
  1. if ([self respondsToSelector:@selector(setAutomaticallyAdjustsScrollViewInsets:)]) {  
  2.     self.automaticallyAdjustsScrollViewInsets = NO;  
  3. }  

问题三:UITextView添加placeholder灰色提示文字

UITextView没有UITextField的placeholder属性来提示输入框信息,也没有其他属性或接口来设置。

有人在UITextView上覆盖个UILabel来显示placeholder信息,也是一种解决思路。

但是从写程序的角度来说,用继承UITextView来解决问题似乎更加合理些。

新建UIPlaceholderTextView类继承自UITextView。

代码如下:

UIPlaceholderTextView.h文件

[objc] view plain copy
  1. @interface UIPlaceholderTextView : UITextView  
  2.   
  3. @property(nonatomicstrongNSString *placeholder;  //灰色提示文字  
  4.   
  5. @end  

UIPlaceholderTextView.m文件
[objc] view plain copy
  1. #import "UIPlaceholderTextView.h"  
  2.   
  3. @implementation UIPlaceholderTextView  
  4.   
  5. - (id)initWithFrame:(CGRect)frame {  
  6.     if ((self = [super initWithFrame:frame])) {  
  7.         [self awakeFromNib];  
  8.     }  
  9.     return self;  
  10. }  
  11.   
  12.   
  13. - (void)awakeFromNib {  
  14.     [self addObserver];  
  15. }  
  16.   
  17. #pragma mark 注册通知  
  18. - (void)addObserver {  
  19.     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidBeginEditing:) name:UITextViewTextDidBeginEditingNotification object:self];  
  20.     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidEndEditing:) name:UITextViewTextDidEndEditingNotification object:self];  
  21. }  
  22.   
  23. #pragma mark 移除通知  
  24. - (void)dealloc {  
  25.     [[NSNotificationCenter defaultCenter] removeObserver:self];  
  26. }  
  27.   
  28. #pragma mark 开始编辑  
  29. - (void)textDidBeginEditing:(NSNotification *)notification {  
  30.     if ([super.text isEqualToString:_placeholder]) {  
  31.         super.text = @"";  
  32.         [super setTextColor:[UIColor blackColor]];  
  33.     }  
  34. }  
  35.   
  36. #pragma mark 结束编辑  
  37. - (void)textDidEndEditing:(NSNotification *)notification {  
  38.     if (super.text.length == 0) {  
  39.         super.text = _placeholder;  
  40.          //如果文本框内是原本的提示文字,则显示灰色字体  
  41.         [super setTextColor:[UIColor lightGrayColor]];  
  42.     }  
  43. }  
  44.   
  45. #pragma mark 重写setPlaceholder方法  
  46. - (void)setPlaceholder:(NSString *)aPlaceholder {  
  47.     _placeholder = aPlaceholder;  
  48.     [self textDidEndEditing:nil];  
  49. }  
  50.   
  51. #pragma mark 重写getText方法  
  52. - (NSString *)text {  
  53.     NSString *text = [super text];  
  54.     if ([text isEqualToString:_placeholder]) {  
  55.         return @"";  
  56.     }  
  57.     return text;  
  58. }  
  59.   
  60.   
  61. @end  

使用UIPlaceholderTextView的控制器的viewDidLoad方法里添加如下代码
[objc] view plain copy
  1. self.placeHolderTextView.placeholder=@"这一刻你想说什么......";  

效果如下:
0 0
原创粉丝点击