gpt4 book ai didi

objective-c - 将 CAGradient mask 层应用于 UITextView

转载 作者:太空狗 更新时间:2023-10-30 03:57:52 24 4
gpt4 key购买 nike

我有一个带有可滚动文本的 UITextView。我正在尝试对其应用渐变层,因此可见文本的底部总是略微淡出。

这是我的代码:

CAGradientLayer *maskLayer = [CAGradientLayer layer];

maskLayer.colors = [NSArray arrayWithObjects:(id)[[UIColor clearColor] CGColor], (id)[[UIColor yellowColor] CGColor], nil];
maskLayer.locations = [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.0],
[NSNumber numberWithFloat:0.2],
[NSNumber numberWithFloat:0.8],
[NSNumber numberWithFloat:1.0], nil];
maskLayer.bounds = clueTextView.bounds;
myUITextView.layer.mask = maskLayer;

现在,这导致 UITextViewLayer 完全透明 - 我看到 UITextViewLayer 作为其 subview 的 UIView 的背景颜色。我看不到其中包含的任何文本,尽管我可以选择并复制文本以粘贴到笔记程序中。

知道我遗漏了什么吗?

最佳答案

你有几个问题。

渐变图层的 colors 属性需要与其 locations 属性具有相同的计数。您提供了 4 个位置,但只有 2 种颜色。

您正在根据 clueTextView 设置渐变层的边界,但您将其设置为 myUITextView 的 mask 。

您设置的是渐变图层的边界,而不是它的位置。默认的position是0,0,意思是渐变层的中心在myUITextView的左上角。将渐变层的 frame 设置为 TextView 的 bounds 会更简单。

如果您解决了所有这些问题,您可能会获得想要的效果。

- (void)initTextViewMask {
CAGradientLayer *maskLayer = [CAGradientLayer layer];
maskLayer.colors = @[
(id)[UIColor whiteColor].CGColor,
(id)[UIColor whiteColor].CGColor,
(id)[UIColor clearColor].CGColor];
maskLayer.locations = @[ @0.0f, @0.8f, @1.0f ];
maskLayer.frame = textView_.bounds;
textView_.layer.mask = maskLayer;
}

但是,您可能会发现新的问题。

如果用户可以滚动 TextView ,你会发现渐变随着 TextView 移动,这可能不是你想要的。解决此问题的最简单方法是将 TextView 嵌入到容器 UIView 中,并在容器 View 上设置 mask 。

如果 TextView 可以改变大小(可能由于设备旋转), mask 将不会自动改变大小。您需要在 layoutSubviewsviewDidLayoutSubviews 中更新 mask 的 frame

关于objective-c - 将 CAGradient mask 层应用于 UITextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12845590/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com