UITextView text color not getting set when disable

2020-03-13 06:36发布

Setup :

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.

Scenario :

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 ).

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.

enter image description here

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

Issues :

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

  2. 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 )

( 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. )

3条回答
霸刀☆藐视天下
2楼-- · 2020-03-13 06:46

Old question, but I just ran into this issue yesterday. This question and the answers helped.

However since my UITextView as in a table cell, isSelectable = false was not a usable option. Here is what worked for me:

  1. In the storyboard, leave the UITextView with userInteractionEnabled checked
  2. In the code, set the UITextView textColor then use setUserInteractionEnabled: to set it to NO

self.textView.textColor = [UIColor blackColor]; [self.textView setUserInteractionEnabled:NO];

Using setUserInteractionEnabled: instead of .userInteractionEnabled is apparently the difference that works.

Hope this further helps anyone trying to still support iOS 9.

查看更多
时光不老,我们不散
3楼-- · 2020-03-13 07:00

Looks like this bug was fixed on iOS 10.

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

P.S.

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

查看更多
ゆ 、 Hurt°
4楼-- · 2020-03-13 07:02

For disabling interaction with a textView (making it non touchable) you can use:

_textViewOne.userInteractionEnabled = NO;
查看更多
登录 后发表回答