问题
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.
If i check selectable
, text colour follows whatever i set in interface builder.
Issues :
Why unchecking of
selectable
property from interface builder doesn't follow whatever textcolor i set for textview.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. )
回答1:
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.
回答2:
For disabling interaction with a textView (making it non touchable) you can use:
_textViewOne.userInteractionEnabled = NO;
回答3:
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:
- In the storyboard, leave the
UITextView
withuserInteractionEnabled
checked - In the code, set the
UITextView
textColor
then usesetUserInteractionEnabled:
to set it toNO
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.
来源:https://stackoverflow.com/questions/27677024/uitextview-text-color-not-getting-set-when-disabled-selectable-property