我有一些代码可以创建一堆 UITextViews
并将它们放入另一个 View 中。我将背景颜色更改为交替颜色,并始终将文本颜色设置为黑色。除了它创建的最后一个之外,它们都运行良好。该方法将其 textColor 更改为 TextView 的背景颜色,然后停止更新其 UI。
如果我在 GDB 中检查有问题的 textColor 的值,它是黑色的,即使它不是这样显示的。然后我以编程方式更改它(按下按钮,循环遍历所有创建的 TextView 并将 textColors 全部设置为紫色),除了最后一个之外,它们都发生变化,其背景颜色与文本颜色相同。再次,当我检查 GDB 时,textColor 的值被设置为紫色,即使这没有反射(reflect)在屏幕上。
什么鬼?!?!?!有任何想法吗?这可能只是一个错误吗?
这是我用来添加 UITextViews
的代码。我在中间有 UILabels,这样我就可以获得居中的文本。
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”,而我得到的唯一匹配项是无关紧要的。
最佳答案
我通过 hack 解决了这个问题。我认为这是一个错误。
最初,我在 UITextView 后面添加了一个 UIView
,其中填充了我想要的背景颜色。
但是,我发现滚动 UITextView
也会将背景颜色更改为其设置的颜色。
因此,我在 .h 中添加了一个 BOOL hasScrolled;
,并在 viewDidAppear:
中添加了对 [selfscrollLevelLabelsViews]
的调用(首先检查以确保确保它没有 alreadyScrolled
因为 viewDidAppear:
可以被调用多次,例如,如果呈现模态视图),并添加了以下方法来 ScrollView 。我还遇到了最后一个 TextView 无法正确显示文本但忽略创建它的问题,直到 View 出现后的 alreadyScrolled
方法使其显示正常。
以下是我创建的用于向下和向后滚动 TextView 的方法。只需将 levelLablesView
更改为包含您的 textView 的 View ,添加我在上一段中解释的方法调用和变量,它应该适合您。
- (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];
}
即使您没有遇到此问题,这也是一个很酷的效果,因为它让用户知道还有更多他们看不到的文本,并且 TextView 是可滚动的。
关于iphone - UITextView 文本颜色自动变得与背景颜色相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9426972/