UITextView textColor自动变为与背景色相同 [英] UITextView textColor automatically becoming the same as the background color

查看:27
本文介绍了UITextView textColor自动变为与背景色相同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码可以创建一堆 UITextViews ,并将它们放入另一个视图.我将backgroundColor更改为交替的颜色,并始终将textColor设置为黑色.除了创建的最后一个之外,它们都工作正常.那将其textColor更改为文本视图的backgroundColor,然后停止更新其UI.

I have some code that creates a bunch of UITextViews and puts them into another view. I change the backgroundColor to alternating colors and always set the textColor to black. They all work out fine, except for the last one that it creates. That one changes it's textColor to whatever the backgroundColor of the text view is and then stops updating it's UI.

如果我在GDB中检查有问题的textColor的值,即使它不是那样显示的,它也是黑色的.然后,我以编程方式对其进行更改(按一个按钮,遍历所有已创建的文本视图,并将textColors全部设置为紫色),除了最后一个颜色(背景颜色与文本颜色相同)之外,它们都发生了变化.同样,当我签入GDB时,textColor的值被设置为紫色,即使它没有反映在屏幕上.

If I check the value of textColor of the problematic in GDB, it is black, even though it isn't displayed that way. Then I change it programmatically (push a button, loop through all the created text views and set the textColors all to purple), they all change except for the last one, who's background color is the same as it's text color. Again, when I check in GDB, the value of textColor, it is set at purple, even though this is not reflected on the screen.

WTF?!?!?!有任何想法吗?难道这只是一个错误?

WTF?!?!?! Any ideas? Could this just be a bug?

这是我用来添加 UITextViews 的代码.我之间有UILabels,所以我可以获取居中的文本.

Here's the code I'm using to add the UITextViews. I have UILabels in between so I can get centered text.

UIColor *evenColor = [self RGBColorR:90 G:95 B:90];
UIColor *oddColor = [self RGBColorR:70 G:75 B:70];

if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
    // Loop through adding the buttons.
    for (int i = 0; i < numberOfSections; i++) {
        // Add the label for the actual title of the level.
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, ((i * sectionHeight) - 1), self.levelLablesView.frame.size.width, 30)];
        [label setText:[levels objectAtIndex:i]];
        label.font = [UIFont fontWithName:@"Helvetica-Bold" size:24];
        [label setTextAlignment:UITextAlignmentCenter];
        [label setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
        if (i % 2 == 0) {
            // It's an even number.
            [label setBackgroundColor:evenColor];
        } else {
            // Its an odd number.
            [label setBackgroundColor:oddColor];
        }
        [self.levelLablesView addSubview:label];
        [label release];

        // Add a scrolling UITextView for the other stuff.
        UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(0, ((i * sectionHeight) + 24), self.levelLablesView.frame.size.width, (sectionHeight - 24))];
        [textView setText:[[[dictionary objectForKey:self.chosenCategory]
                            objectForKey:[levels objectAtIndex:i]]
                           objectForKey:@"Description"]];
        [textView setEditable:NO];
        [textView setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
        if (i % 2 == 0) {
            // It's an even number.
            [textView setBackgroundColor:evenColor];
        } else {
            // Its an odd number.
            [textView setBackgroundColor:oddColor];
        }
        if (i == (numberOfSections-1)) {
            [textView setFrame:CGRectMake(0,((i * sectionHeight) + 24), self.levelLablesView.frame.size.width, ((sectionHeight - 24) + 2))];
        }
        textView.font = [UIFont systemFontOfSize:24];
        [textView setTextColor:[UIColor blackColor]];

        [self.levelLablesView addSubview:textView];
        [textView release];
    }

}

:如果有人想知道,我不会在程序中的其他任何地方设置文本颜色.我找到了"textColor",但得到的唯一匹配项是不相关的.

In case anyone is wondering, I don't set the text color anywhere else in the program by accident. I did a find for "textColor" and the only matches I get are irrelevant.

推荐答案

我通过黑客解决了这个问题.我认为这是一个错误.

I fixed this with hacks. I think that this is a bug.

最初,我在UITextView后面添加了一个 UIView ,其中填充了我想要的背景颜色.

Originally, I added a UIView filled with the background color that I wanted behind the UITextView.

但是,我发现滚动 UITextView 也会将背景色更改为设置为的颜色.因此,我在.h中添加了 BOOL已经滚动; ;在 viewDidAppear:中添加了对 [self scrollLevelLabelsViews] 的调用(首先进行检查以使确保没有 alreadyScrolled ,因为例如,如果提供了模式视图,则 viewDidAppear:可以被调用多次,并添加了以下方法来滚动视图.我也遇到了一个问题,即最后一个文本视图无法正确显示文本,但是在视图出现后直到 alreadyScrolled 方法使它显示正常之前,才忽略创建文本.

However, I discovered that scrolling the UITextView also changes the background color to the color that it is set as. So I added a BOOL alreadyScrolled; to my .h, added a call to [self scrollLevelLabelsViews] in viewDidAppear: (which first checks to make sure it hasn't alreadyScrolled because viewDidAppear: can get called multiple times if a modal view is presented, for example), and added the following method to scroll the views. I was also having an issue with the last text view not displaying text correctly but neglecting to create it until the alreadyScrolled method after the view has appeared makes it display fine.

以下是我创建的用于上下滚动文本视图的方法.只需将 levelLablesView 更改为包含textViews的视图,添加我在上一段中介绍的方法调用和变量,它便会为您工作.

The following are the methods I created to scroll the text views down and back up. Just change levelLablesView to the view that contains your textViews, add the method calls and variables I explained in the last paragraph, and it should work for you.

- (void)scrollLevelLabelsViewsBackUp{

    // Scroll all UITextViews in levelLabelsView to the top.
    for (UIView *view in self.levelLablesView.subviews) {
        if ([view isKindOfClass:[UITextView class]]) {
            UITextView *textView = (UITextView *)view;
            [textView scrollRangeToVisible:NSMakeRange(1, 1)];
        }
    }
    alreadyScrolled = YES;

}

- (void)scrollLevelLabelsViews{

   /* Setup last UITextView that displays weirdly if it is set up with the rest of them.
    * ...
    */

    // Scroll all UITextViews in levelLabelsView to the bottom.
    for (UIView *view in self.levelLablesView.subviews) {
        if ([view isKindOfClass:[UITextView class]]) {
            UITextView *textView = (UITextView *)view;
            [textView scrollRangeToVisible:NSMakeRange([[textView text] length] - 2, 1)];
        }
    }

    // In .8 seconds, scroll them back up.
    [NSTimer scheduledTimerWithTimeInterval:.8 target:self selector:@selector(scrollLevelLabelsViewsBackUp) userInfo:nil repeats:NO];

}

即使您没有遇到此问题,这也只是一个很酷的效果,因为它使用户知道他们看不到更多的文本,并且文本视图是可滚动的.

This is also just a cool effect even if you're not having this problem as it lets the user know that there is more text that they can't see and that the text view is scrollable.

这篇关于UITextView textColor自动变为与背景色相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆