0

I am trying to use the solution on the following page: UITextView highlightedTextColor or similar option?

However, I am finding that when I call setTextColor on my UITextView and set the color to anything other than Color blackColor, the content in the UITextView appears shifted, and the color I do set it to doesn't take.

Playing around, I can repeat this behaviour by modifying the following initialization code in my UITableViewCell:

_notesTextView = [[UITextView alloc] initWithFrame:CGRectZero];
[_notesTextView setFont:[UIFont systemFontOfSize:12.0]];
[_notesTextView setTextColor:[UIColor redColor]];
[_notesTextView setUserInteractionEnabled:NO];
[self.contentView addSubview:_notesTextView];

The code above will cause the textView to display offset from what I expect, as compared to when I leave the color defaulted or set to blackColor, and the text doesn't show as red either.

This is so weird - any idea what could be wrong?

Community
  • 1
  • 1
DaveDude
  • 295
  • 2
  • 13

2 Answers2

1

Do Not Use CGRectZero as it initiates the frame to some value you might know depending upon situation so if you have created a custom UITableViewCell then just set the frames in init or

-(void)layoutSubViews
{

}
Abhishek Singh
  • 6,068
  • 1
  • 23
  • 25
0

I've confirmed that the textColor does indeed shift the text position (up and down for me). I've also tried it with initWithFrame(50, 20, 250, 31), and still experience the same problem.

What I've discovered fixes this issue is using the property textAlignment and setting it to one of the values: UITextAlignmentCenter, UITextAlignmentLeft, UITextAlignmentRight.

Vadoff
  • 9,219
  • 6
  • 43
  • 39
  • I found the same thing as @Vadoff. The other weird thing is that I only found this artifact with a font size of 12. When I tried sizes of 10,11,13,14,16, and 18 the placement of the text was the same regardless of color without setting the alignment. I didn't see the behavior mentioned by the op, that the color set didn't show up. – rdelmar May 24 '12 at 04:58
  • Setting the textAlignment does fix my offsetting text issue. – DaveDude May 24 '12 at 12:33
  • I still have the issue of the text color not changing. I will go back to the linked example to ask the question there about that. – DaveDude May 24 '12 at 12:34