本文介绍了禁用可选属性时,UITextView文本颜色未设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

设置:

我在基于故事板的视图中添加了简单的textview,并在viewcontroller的.m文件中添加了插座。我正在使用它设置文本。例如 [_ textViewOne setText:@Test text]; 。我正在使用Xcode 6.1,使用iOS 8.1进行编译。

I have simple textview added in storyboard based view, with outlet taken in viewcontroller's .m file. I am setting text using it. e.g [_textViewOne setText:@"Test text"];. I am using Xcode 6.1, compiling with iOS 8.1.

场景:

我想让UITextView不可触摸,就像UILabel一样(我使用的是UITextView而不是UILabel,因为我的文本可能是1或2行,UILabel在垂直中心对齐1行文本,2行从左上角开始,我希望它在两种情况下从左上角开始,这可以通过textview轻松实现)。

I wanted to make UITextView non touchable, just like UILabel ( i am using UITextView instead of UILabel because my text may be 1 or 2 line and UILabel aligns text at vertically center of view for 1 line and for 2 lines it starts from top left, i want it to start from top left in both cases which is easily achievable by textview ).

为了使它不可触摸,我首先从Storyboard中禁用可选属性并尝试运行应用程序。取消选中可选属性后,我的文本视图的文本不使用我为其设置的颜色(我已设置红色),并且它使用默认的黑色。

To make it non touchable first i disabled selectable property of it from Storyboard and tried to run application. After unchecking selectable property my text view's text not using the colour i set for it ( i have set red colour ) and it is using default black colour.

如果我检查 selectable ,则文本颜色会跟随我在界面构建器中设置的颜色。

If i check selectable, text colour follows whatever i set in interface builder.

问题:


  1. 为什么取消选中可选择属性不遵循我为textview设置的任何文本颜色。

  1. Why unchecking of selectable property from interface builder doesn't follow whatever textcolor i set for textview.

我注意到还有一件事,如果我取消选中从界面构建器中选择属性并尝试以编程方式设置它,如 _textViewOne.selectable = YES; 然后TextView也不会跟随任何textcolor我在接口构建器中设置,仅检查接口构建器中的该属性是否有效。 (尝试干净的构建,也重新启动xcode)

One more thing i noticed, if i uncheck selectable property from interface builder and tried to set it programmatically like _textViewOne.selectable = YES; then also TextView doesn't follow whatever textcolor i set in interface builder, only checking that property from interface builder works. ( Tried with clean build, restarting xcode also )

(我知道我应该禁止textview的用户交互它不可触摸,但我不小心取消选中可选择并花了我的时间弄清楚发生了什么,所以只是想知道我做错了什么或这是某种问题或这是此控件的正确行为。)

( I know i should just make textview's user interaction disabled to make it non touchable, but i accidentally unchecked selectable and it took my time to figure out what was happening, so just wanted to know was i doing something wrong or this is some kind of issue or this is correct behaviour of this control. )

推荐答案

看起来这个错误已在iOS 10上修复。

Looks like this bug was fixed on iOS 10.

然而,当我在我们的bugtracker中看到这个bug并且无法在iOS 10上重现时我感到非常惊讶,但是在iOS9上成功复制了它。

However, I was very surprised when I saw this bug in our bugtracker and cannot reproduce on iOS 10, but successfully reproduced it on iOS9.

在我的案例中,解决方案是按照以下顺序在代码中写入:

Solution in my case was to write it in code in this order:

textView.textColor = UIColor.red
textView.isSelectable = false

PS

textViewOne.userInteractionEnabled = false 对我不起作用,因为我是nt textview是可滚动的,但没有选择选项。

textViewOne.userInteractionEnabled = false is not works for me, because I want textview to be scrollable, but without selection option.

这篇关于禁用可选属性时,UITextView文本颜色未设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 04:38