关于 iOS UITextView 的各种陨石坑 (一) !

来源:互联网 发布:qq网络硬盘在哪里找 编辑:程序博客网 时间:2024/06/05 19:12
 UITextView (使用它必须要遵守<UITextViewDelegate>协议)- (void)setUpTextView {    UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake( x, y, width, height)];    //设置是否能编辑    textView.editable = YES;    //设置代理    textView.delegate = self;    //设置 textView 内容    textView.text = @"xxxxxxxxxx";    //设置字体    textView.font = [UIFont systemFontOfSize:15.0f];    //设置字体的颜色    textView.textColor = [UIColor whiteColor];    //设置是否可以滚动    //UITextView 继承于 UIScrollView    textView.scrollEnabled = YES;    // textView 边框    textView.layer.borderWidth = 0.8;    textView.layer.borderColor = [UIColor redColor].CGColor;    textView.layer.cornerRadius = 3;    [self.view addSubview:textView];}

第一个陨石坑来了!
没有人跳转 UITextView 的时候不使用导航栏吧 ?
如果你设置了导航栏 那么恭喜你 你中奖啦 !
你会发现你的 UITextView 有多淘气 !

我们来看看为什么会这样呢 ?

我们可以从图中看到 _UITxetContainerView 向下偏移了!
那么它偏移了多少呢 ? 没错, 正好一个 导航栏的高度
iOS7 如果把 UIscrollView 加在导航栏中 内容会向下偏移 64
那么如何解决呢 ?

    self.automaticallyAdjustsScrollViewInsets = NO;

好啦 问题解决了 我们高高兴兴的去解决下一个陨石坑 !
打开键盘 ! 卧槽 这是什么鬼 ?

来张 “无码的”

这次又是这个导航栏 !
你好好的为啥就剩一半了呢? 你到底经历了什么?

如果你的 textView 在点击键盘后 视图上移了
那么多半是你的项目中使用了关于键盘的第三方框架
这个问题多出在 : IQKeyboardManager 这个框架身上

看过很多解决办法 什么视图添加到 scrollView 上 balabala ~~
我给出的解决方式是 禁用掉! 自己做

// 禁用 IQKeyboard- (void)viewWillAppear:(BOOL)animated{    [super viewWillAppear:animated];    [IQKeyboardManager sharedManager].shouldResignOnTouchOutside = YES;     [IQKeyboardManager sharedManager].enable = NO;}- (void)viewWillDisappear:(BOOL)animated{    [textView resignFirstResponder];    [IQKeyboardManager sharedManager].enable = YES;}

解决完毕, 我们继续踏上寻找陨石坑的道路
现在眼前能看见的问题都没了
可是这样的 textView 不满足我们的需求呀
没有 占位文字 呀~
为什么它和 textfield 不一样呢? 它为什么没有 placeHolder 呢?
点语法 点不出来呀 !
嗯 你没敲错代码 textView 还真的”没”有 placeHolder 属性!
简单的解决方法呢 是自己写一个 Label
判断 textView.text.length 有文字就显示 Label 反之不显示 ~
但是这能难道一个会使用搜索引擎的人吗 ?

通过搜索 我知道了 UITextView 中其实有 _placeholderLabel 这个属性的
不过比较麻烦 需要使用 runtime 遍历 UITextView 属性
你会发现一个placeHolderLable的属性 然后再用KVC 设置就可以了

这个是遍历的方法 ~

    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);    }

这个是遍历出来的内容 注意第 21个

0 - _private1 - _textStorage2 - _textContainer3 - _layoutManager4 - _containerView5 - _inputDelegate6 - _tokenizer7 - _inputController8 - _interactionAssistant9 - _textInputTraits10 - _autoscroll11 - _tvFlags12 - _contentSizeUpdateSeqNo13 - _scrollTarget14 - _scrollPositionDontRecordCount15 - _scrollPosition16 - _offsetFromScrollPosition17 - _linkInteractionItem18 - _dataDetectorTypes19 - _preferredMaxLayoutWidth20 - _placeholderLabel21 - _inputAccessoryView22 - _linkTextAttributes23 - _streamingManager24 - _characterStreamingManager25 - _siriAnimationStyle26 - _siriParameters27 - _firstBaselineOffsetFromTop28 - _lastBaselineOffsetFromBottom29 - _cuiCatalog30 - _beforeFreezingTextContainerInset31 - _duringFreezingTextContainerInset32 - _beforeFreezingFrameSize33 - _unfreezingTextContainerSize34 - _adjustsFontForContentSizeCategory35 - _clearsOnInsertion36 - _multilineContextWidth37 - _inputView

具体的使用添加 placeHolder 的方法
懒人看着就可以啦 !

    UILabel *placeHolderLabel = [[UILabel alloc] init];    placeHolderLabel.text = @"xxxxxxxx";    placeHolderLabel.font = [UIFont systemFontOfSize:13.f];    placeHolderLabel.numberOfLines = 0;    placeHolderLabel.textColor = [UIColor lightGrayColor];    [placeHolderLabel sizeToFit];    [textView addSubview:placeHolderLabel];    [textView setValue:placeHolderLabel forKey:@"_placeholderLabel"];

这样添加 placeHolder 还是会有一个小 BUG 的
这个 BUG 就留给你自己体会拉 (不影响正常使用 ~)

有时间我还是介绍关于 UITextView 的陨石坑
还有就是一些必备的功能

键盘如何不遮挡 textView 呀 ~
粘贴大段文字 textView 会上移呀~
返回未保存提示呀 ~
字数限制啊 ~

最后 希望你们会喜欢这篇分享 !

0 0
原创粉丝点击