colorWithPatternImage在io6中不起作用?

时间:2014-07-26 04:55:53

标签: ios ios7 ios6 uicolor

我使用 colorWithPatternImage 更改UITextView的文字颜色。它在IO7中工作正常,但在IO6中不起作用。

这是我的代码: -

text.textColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"ColorName.png"]];

2 个答案:

答案 0 :(得分:6)

iOS {6}之间UITextView的内部变化很大。在iOS7之前,UITextView不支持文本颜色的图案颜色。有几个组件在iOS 7中获得了对图案颜色的支持,UITextView就是其中之一。

答案 1 :(得分:0)

事实上,colorWithPatternImage不适合您在iOS 6中的用途。我会寻求使用UITextView文本作为掩码的解决方案。请查看David对此问题的回答:Transparent UILabel textColor on superview.superview (sort of)。他从他想要看到的字符串中创建一条路径并从中创建一个掩码。完整性检查也iOS UIView subclass, draw see-through text to background

希望这有帮助

修改

事实证明,在iOS 6中使用UITextView实现屏蔽效果并不是那么容易。我只是设法"模拟"通过对UIView执行以下操作来获取UITextView的外观:

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

以下是该视图中的示例图片

enter image description here

要使此视图滚动,您需要将其放在UIScrollView中。

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

如果您遵循此路线,则需要使用滚动视图和tView,以使它们看起来和行为类似于UITextView。

相关问题