iBhavin
iBhavin

Reputation: 1261

colorWithPatternImage not working in io6?

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

Upvotes: 3

Views: 680

Answers (2)

Don Miguel
Don Miguel

Reputation: 912

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.

Upvotes: 0

Simon
Simon

Reputation: 25983

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.

Upvotes: 6

Related Questions