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

时间:2022-11-02 21:07:43

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. 应用程序基于主/详细模板
  3. I created a class that is a subclass of UITableViewCell
  4. 我创建了一个类,它是UITableViewCell的子类
  5. I added two properties to the class, an outlet for a UILabel and a UITextView
  6. 我向类添加了两个属性,一个用于UILabel的outlet和一个UITextView
  7. I altered the default prototype cell by adding a UITextView (it already has a UILabel)
  8. 我通过添加UITextView(它已经有UILabel)修改了默认的原型单元格
  9. I changed the class of the prototype cell to my custom cell class
  10. 我将原型单元格的类更改为自定义单元格类
  11. I connected the UITextView and UILabel in the cell to the two outlets in my cell class
  12. 我将单元格中的UITextView和UILabel连接到单元格类中的两个outlet

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.

我在下面包含了相关的代码片段和屏幕截图。我不是百分之百确定什么与问题有关,什么与问题无关,如果你需要看到我下面没有展示的东西,请提问,我将提供它。

** EDIT

* *编辑

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

当我退出UITextView的颜色属性时,它输出(null)。看到MasterViewController。下面的代码。

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连接到我的自定义单元格中的outlet:

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

Font color set to light grey in interface builder:

界面建设者将字体颜色设置为浅灰色:

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

What it looks like in the iPhone simulator:

iPhone模拟器的样子是:

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

5 个解决方案

#1


114  

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

在解决了这个问题之后,我带着新的信息回到谷歌,发现了下面的堆栈溢出问题:

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!

谢谢你的帮助!

#2


4  

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];

#3


3  

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“只读”,但当我取消可编辑性和可选性时,行为不是我所期望的,有时文本颜色没有改变,有时字体被重置,所以我的解决方案是在interface builder中检查可编辑、可选择和不检查用户交互

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

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

#4


2  

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;

#5


0  

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:

让人有点困惑的是,这个更改在您的应用程序中没有生效,但是既然您已经为您的文本字段创建了outlet,那么您不妨添加一行代码,以编程方式设置颜色:

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.

这确保颜色设置为您想要的颜色,因此您不必依赖接口构建器。

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”实际上指向文本字段。它应该,因为您可以使用该指针设置文本。

#1


114  

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

在解决了这个问题之后,我带着新的信息回到谷歌,发现了下面的堆栈溢出问题:

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!

谢谢你的帮助!

#2


4  

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];

#3


3  

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“只读”,但当我取消可编辑性和可选性时,行为不是我所期望的,有时文本颜色没有改变,有时字体被重置,所以我的解决方案是在interface builder中检查可编辑、可选择和不检查用户交互

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

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

#4


2  

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;

#5


0  

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:

让人有点困惑的是,这个更改在您的应用程序中没有生效,但是既然您已经为您的文本字段创建了outlet,那么您不妨添加一行代码,以编程方式设置颜色:

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.

这确保颜色设置为您想要的颜色,因此您不必依赖接口构建器。

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”实际上指向文本字段。它应该,因为您可以使用该指针设置文本。