3

I'm using colorWithPatternImage to change text color of UITextView. Its working fine in IO7 but not working in IO6.

Here is my Code :-

text.textColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"ColorName.png"]];
iBhavin
  • 1,261
  • 15
  • 30

2 Answers2

6

The internals of UITextView changed hugely between iOS 6 and 7. Before iOS7, UITextView didn't support pattern colours for the text colour. Several components gained support for pattern colours in iOS 7, and UITextView was one of them.

Simon
  • 25,468
  • 44
  • 152
  • 266
  • colorWithPatternImage is work with UITextView backgroundColor but not working with textColor. – iBhavin Jul 28 '14 at 12:29
  • 1
    Yes - a lot of components supported pattern colours for backgroundColor, but they didn't gain support for them as foreground colours until iOS 7. – Simon Jul 28 '14 at 12:34
  • Is there any alternative for it ?? – iBhavin Jul 29 '14 at 03:54
  • None that I know of. You might be able to do something by overriding `drawRect:` and drawing the text yourself. – Simon Jul 29 '14 at 06:34
0

Indeed, colorWithPatternImage is not fit for your purposes in iOS 6. I would go for a solution that uses the text of the UITextView as a mask. Check the answer by David to this question: Transparent UILabel textColor on superview.superview (sort of). He makes a path from the string he wants to see through and creates a mask out of it. For completeness check also iOS UIView subclass, draw see-through text to background.

Hope this helps

EDIT

Turns out it was not so easy to achieve the masking effect you are after with a UITextView in iOS 6. I have only managed to "emulate" the look of UITextView by doing the following to a UIView:

UIView *tView = [[UIView alloc] initWithFrame:self.view.frame];

tView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"pattern.jpg"]];

NSString *text = @"Lorem ipsum...";

CATextLayer *mask = [CATextLayer layer];

mask.frame = frame;

mask.backgroundColor = [UIColor colorWithWhite:0 alpha:0].CGColor;
mask.foregroundColor = [UIColor colorWithWhite:0 alpha:1].CGColor;
mask.string = text;
mask.wrapped = YES;
mask.fontSize = 25;

tView.layer.mask = mask;

[self.view addSubview:tView];

Here is a sample image from that view

enter image description here

To have this view scrolling you would need to place it inside a UIScrollView.

UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:self.view.frame];
// Setup tView as above
[scroll addSubview:tView];
[self.view addSubview:scroll];

If you follow this route you would need to play around with the scroll view and tView so that they look and behave like a UITextView.

Community
  • 1
  • 1
Don Miguel
  • 882
  • 9
  • 19
  • yes it can apply to UILable as per that ans but not to UITextView.I want to apply this in textColor. – iBhavin Aug 04 '14 at 09:11