27

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.

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

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.

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.

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.

Custom cell - 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:

- (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 connected to outlet

Font color set to light grey in interface builder:

UITextView font color set to Light Grey

What it looks like in the iPhone simulator:

What the table view looks like in the simulator

jd182
  • 3,180
  • 6
  • 21
  • 30
  • Please show the code where you are creating these cells. – John Jan 19 '14 at 19:12
  • I've added the part of the MasterViewController that sets the cell contents - i think this is the bit you asked for. Thanks! – jd182 Jan 19 '14 at 19:17
  • Use NSAttributedString. See: http://stackoverflow.com/questions/14287386/change-string-color-with-nsattributedstring – user2208214 Mar 29 '16 at 12:31
  • There is a bug in iOS: changing text will change color also. You need to change the text color **after** changing the text, i.e. tv.text = @"asd"; tv.textColor = [UIColor whatever]. – Borzh May 30 '17 at 02:19

5 Answers5

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

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.

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

Thanks for your help!

Community
  • 1
  • 1
jd182
  • 3,180
  • 6
  • 21
  • 30
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:

self.tvPhone.tintColor = [UIColor blueColor];
Dannie P
  • 4,464
  • 3
  • 29
  • 48
  • 1
    This is the only solution that works on a project compiled for iOS 8.2 using Xcode 8 on a device running iOS 10. Thanks. – Duck Sep 21 '16 at 22:00
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

enter image description here

enter image description here

Chuy47
  • 2,391
  • 1
  • 30
  • 29
2

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

cell.txtvComment.selectable = YES;
cell.txtvComment.textColor = [Globals getTextColor]; //set your UIColor
cell.txtvComment.text = comment.commentText;
cell.txtvComment.selectable = NO;
NSGodMode
  • 599
  • 1
  • 6
  • 16
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:

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:

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:

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.

user2105505
  • 686
  • 1
  • 9
  • 18
  • Thanks, i did actually try this. I tried it in two places, in the init method of my custom cell and in the method shown above where the cell contents is set. It didn't work in either place – jd182 Jan 19 '14 at 19:21
  • That's really strange. Did you use breakpoints or NSLogs to determine that this section of code is being called? I would use a series of NSLog(@"%@", cell.descriptionText.textColor) to see what the color is being set to and if it ever changes back to black. – user2105505 Jan 19 '14 at 19:24
  • Thanks, i've obviously messed something up somewhere if what i've done should work normally. Where should I put the NSLog code? – jd182 Jan 19 '14 at 19:25
  • I've put the debug code into the method where the cell contents is set as you suggested. it's outputting (null) - any ideas why that might be? – jd182 Jan 19 '14 at 19:31
  • I added new suggestions to my answer. – user2105505 Jan 19 '14 at 19:40
  • Thanks, i've added the output from the console after also logging the UITextView. Weirdly the UITextView itself is an object, but it's color is null? – jd182 Jan 19 '14 at 19:47