xcode IOS 7 - 无法在自定义 UITableView 单元格中设置 UITextView 的字体颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21221281/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 04:34:23  来源:igfitidea点击:

IOS 7 - Can't set font color of UITextView in custom UITableView cell

iosiphoneobjective-cxcodeuitableview

提问by jd182

I'm stuck on what I thought would be a simple thing to sort out. I have a custom table cell which contains a UITextView object. I want the text in this object to be grey. When I set the color in the interface builder it shows it as grey, but in the simulator it remains black.

我被困在我认为很简单的事情上。我有一个包含 UITextView 对象的自定义表格单元格。我希望此对象中的文本为灰色。当我在界面构建器中设置颜色时,它显示为灰色,但在模拟器中它保持黑色。

Here's a run down of how it's set up:

以下是它的设置方式:

  1. The application is based on the master/detail template
  2. I created a class that is a subclass of UITableViewCell
  3. I added two properties to the class, an outlet for a UILabel and a UITextView
  4. I altered the default prototype cell by adding a UITextView (it already has a UILabel)
  5. I changed the class of the prototype cell to my custom cell class
  6. I connected the UITextView and UILabel in the cell to the two outlets in my cell class
  1. 该应用程序基于主/细节模板
  2. 我创建了一个类,它是 UITableViewCell 的子类
  3. 我在类中添加了两个属性,一个用于 UILabel 的插座和一个 UITextView
  4. 我通过添加一个 UITextView(它已经有一个 UILabel)改变了默认的原型单元格
  5. 我将原型单元格的类更改为我的自定义单元格类
  6. 我将单元格中的 UITextView 和 UILabel 连接到单元格类中的两个插座

I updated the MasterViewController to populate my custom cell. When I run the simulator it all works fine in terms of the custom cells displaying the data.

我更新了 MasterViewController 以填充我的自定义单元格。当我运行模拟器时,它在显示数据的自定义单元格方面一切正常。

As I mentioned before, the problem comes when I try to change the color of the text in the custom cell's UITextView. If I set it to Light Grey in the interface builder it has no effect in the simulator.

正如我之前提到的,当我尝试更改自定义单元格的 UITextView 中文本的颜色时,问题就出现了。如果我在界面构建器中将它设置为浅灰色,它在模拟器中没有任何影响。

I've included the relevant code snippets and screenshots below. I'm not 100% sure what's relevant to the problem and what isn't, if you need to see something I haven't shown below just ask and i'll provide it.

我在下面包含了相关的代码片段和屏幕截图。我不是 100% 确定什么与问题相关,什么不相关,如果您需要查看我在下面未显示的内容,请询问,我会提供。

** EDIT

** 编辑

When I log out the color property of my UITextView it outputs (null). See the MasterViewController.m code below.

当我注销 UITextView 的 color 属性时,它输出(空)。请参阅下面的 MasterViewController.m 代码。

Custom cell - NTFYNoteCell.h

自定义单元格 - NTFYNoteCell.h

#import <UIKit/UIKit.h>

@interface NTFYNoteCell : UITableViewCell

@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UITextView *descriptionText;

@end

Taken from the MasterViewController.m:

取自 MasterViewController.m:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NTFYNoteCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"     forIndexPath:indexPath];

    NTFYNote *note = _notes[indexPath.row];

    cell.titleLabel.text = [note title];
    cell.descriptionText.text = [note shortDescription];

    NSLog(@"TEXTVIEW: %@", cell.descriptionText);
    NSLog(@"TEXT COLOR: %@", cell.descriptionText.textColor); // This outputs (null)

    return cell;
}

Debug out from above code:

从上面的代码调试出来:

2014-01-19 19:42:52.001 appname[5050:70b] TEXTVIEW: <UITextView: 0xa1bfe00; frame = (14 26; 273 57); text = 'This is a generic descrip...'; clipsToBounds = YES; autoresize = RM+BM; gestureRecognizers = <NSArray: 0x8be0530>; layer = <CALayer: 0x8be1fe0>; contentOffset: {0, 0}>
2014-01-19 19:42:52.001 appname[5050:70b] TEXT COLOR: (null)
2014-01-19 19:42:52.004 appname[5050:70b] TEXTVIEW: <UITextView: 0xa1cd400; frame = (14 26; 273 57); text = 'This is a generic descrip...'; clipsToBounds = YES; autoresize = RM+BM; gestureRecognizers = <NSArray: 0x8b8fb40>; layer = <CALayer: 0x8bf5300>; contentOffset: {0, 0}>
2014-01-19 19:42:52.005 appname[5050:70b] TEXT COLOR: (null)
2014-01-19 19:42:52.007 appname[5050:70b] TEXTVIEW: <UITextView: 0xe17e000; frame = (14 26; 273 57); text = 'This is a generic descrip...'; clipsToBounds = YES; autoresize = RM+BM; gestureRecognizers = <NSArray: 0xd857bb0>; layer = <CALayer: 0xd85d610>; contentOffset: {0, 0}>
2014-01-19 19:42:52.007 appname[5050:70b] TEXT COLOR: (null)
2014-01-19 19:42:52.009 appname[5050:70b] TEXTVIEW: <UITextView: 0x99af200; frame = (14 26; 273 57); text = 'This is a generic descrip...'; clipsToBounds = YES; autoresize = RM+BM; gestureRecognizers = <NSArray: 0x8a81ea0>; layer = <CALayer: 0x8a580c0>; contentOffset: {0, 0}>
2014-01-19 19:42:52.009 appname[5050:70b] TEXT COLOR: (null)

UITextView connected to the outlet in my Custom Cell:

UITextView 连接到我的自定义单元中的插座:

UITextView connected to outlet

UITextView 连接到插座

Font color set to light grey in interface builder:

在界面构建器中将字体颜色设置为浅灰色:

UITextView font color set to Light Grey

UITextView 字体颜色设置为浅灰色

What it looks like in the iPhone simulator:

它在 iPhone 模拟器中的样子:

What the table view looks like in the simulator

模拟器中的表格视图是什么样的

回答by jd182

After working through the problem a bit I went back to Google armed with the new information and found the following Stack Overflow question:

在解决了这个问题之后,我带着新信息回到了谷歌,发现了以下 Stack Overflow 问题:

UITextView font is nil

UITextView 字体为零

Turns out if you don't check the 'Selectable' checkbox under the UITextView's attributes it doesn't respond to any font changes, including the color.

事实证明,如果您不选中 UITextView 属性下的“可选”复选框,它不会响应任何字体更改,包括颜色。

I checked this box and now it's all working as expected.

我选中了这个框,现在一切都按预期工作了。

Thanks for your help!

谢谢你的帮助!

回答by Dannie P

If your UITextView contains text that is detected to be address/phone number/etc, you've got to set tintColor to change text colour of detected items, like this:

如果您的 UITextView 包含被检测为地址/电话号码/等的文本,您必须设置 tintColor 以更改检测到的项目的文本颜色,如下所示:

self.tvPhone.tintColor = [UIColor blueColor];

回答by Chuy47

I wanted my UITextView "Read Only" but when I uncheck Editable and Selectable the behavior was not what I expected, sometimes the text color was not changing and sometimes the font was reset, so my solution was to check Editable and Selectable and uncheck User Interaction Enabled in interface builder

我想要我的 UITextView“只读”但是当我取消选中 Editable 和 Selectable 时,行为不是我所期望的,有时文本颜色没有改变,有时字体被重置,所以我的解决方案是选中 Editable 和 Selectable 并取消选中 User Interaction在界面构建器中启用

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

回答by NSGodMode

This worked for me. set selectable YES and set No after setting the color

这对我有用。设置可选 YES 并在设置颜色后设置 No

cell.txtvComment.selectable = YES;
cell.txtvComment.textColor = [Globals getTextColor]; //set your UIColor
cell.txtvComment.text = comment.commentText;
cell.txtvComment.selectable = NO;

回答by user2105505

It is kind of puzzling that this change doesn't take effect in your app, but since you have the outlet created for your text field, you might as well add a line of code to set the color programmatically:

令人费解的是,此更改不会在您的应用程序中生效,但由于您为文本字段创建了插座,您不妨添加一行代码以编程方式设置颜色:

cell.descriptionText.textColor = [UIColor grayColor];

This ensures that the color is set to what you want so you don't have to rely on the Interface Builder.

这可确保将颜色设置为您想要的颜色,因此您不必依赖 Interface Builder。

EDIT: You mentioned that you already try this. I would try inserting some NSLog() statements into your code to see the series of changes you are trying to make:

编辑:你提到你已经尝试过这个。我会尝试在您的代码中插入一些 NSLog() 语句以查看您尝试进行的一系列更改:

NTFYNoteCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"     forIndexPath:indexPath];

NTFYNote *note = _notes[indexPath.row];

cell.titleLabel.text = [note title];
NSLog(@"descriptionText color:%@", cell.descriptionText.textColor);
cell.descriptionText.text = [note shortDescription];
cell.descriptionText.textColor = [UIColor grayColor];
NSLog(@"descriptionText color:%@", cell.descriptionText.textColor);

return cell;

If you are getting (null) as the output, something must be wrong with your text field, as it MUST have a color, even a default color (black). Replace one of the NSLog statements with:

如果您将 (null) 作为输出,则您的文本字段一定有问题,因为它必须具有颜色,甚至是默认颜色(黑色)。将 NSLog 语句之一替换为:

NSLog(@"%@", cell.descriptionText)

to make sure that "descriptionText" is actually pointing to your text field. It should, as you were able to set the text using that pointer.

确保“descriptionText”实际上指向您的文本字段。它应该,因为您可以使用该指针设置文本。