UITextView文本底部阴影

也许你知道解决方案,如何在textView上添加阴影,如:

在此处输入图像描述

所以我需要一个透明的textView,我可以在后台看到视图

在此处输入图像描述

Apple提供了CAReplicatorLayer ,您可以使用它来自动镜像图层(amonst其他东西)。 这甚至适用于video图层,效率非常高(直接在GPU上执行绘图)。

CAReplicatorLayer类为其子层(源层)创建指定数量的副本,每个副本可能应用了几何,时间和颜色转换。

有一个WWDCvideo( Core Animation Essentials大约51:00),简要介绍了如何使用它,以及一个名为ReplicatorDemo的OSX演示项目。

快速搜索显示了一篇博文 ,其中给出了反映图层的示例,尽管我没有检查代码/技术的质量。

我认为你可以结合以下方法(它没有经过测试!):

如何使用NSString绘制function从文本创建UIImage

 UIImage * textImage = [self imageFromText:text]; //You may play with the scale value UIImage *textImageMirrored = [UIImage imageWithCGImage:[textImage CGImage] scale:1.0 orientation:UIImageOrientationDownMirrored]; And then use this mirrored image in the correct position. UIImageView *textShadow = [[UIImageView alloc] initWithImage:textImageMirrored]; textShadow.opacity = 0.6; //or so 

我过去在白色背景上做了这个,制作了一个白色图像,渐变开始完全不透明,淡化清晰。 我将它覆盖在表格视图的底部边缘上方。 它使表格视图的外观在底部边缘渐渐变为白色。

我解决了“底层”问题,只是让图像滑下来,因为我到达了表格滚动内容的最底部,所以最后的位不会褪色。

如果你有一个复杂的,移动的或变化的背景,这可能不是最好的选择,但是对于简单的背景来说效果很好。

我找到了最好的结果。 我使用遮罩层和CAGradientLayer。 因此,如果我们将它用于UITextView,那么我们的图层将是非常的。 我创建UIView,在这个UIView上添加我的UITextView并使用UIView层

 //this float need for creating second sublayer, for scrollIndicator CGFloat layerWidth = 8.f; CAGradientLayer *gl1 = [CAGradientLayer layer]; id col1 = (id)[[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0]CGColor]; id col2 = (id)[[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0]CGColor]; gl1.colors = @[col2, col1, col1, col2]; gl1.locations = @[@0.0, @0.0, @0.85, @1.0]; //underTextView - this is my UIView gl1.frame = CGRectMake(0, 0, self.underTextView.width - layerWidth, self.underTextView.height); //layer for scrollIndicator - it must be visible always good CAGradientLayer *gl2 = [CAGradientLayer layer]; gl2.colors = @[col1, col1]; gl2.locations = @[@0.0, @1.0]; gl2.frame = CGRectMake(self.underTextView.width - layerWidth, 0, layerWidth, self.underTextView.height); //create main layer CAGradientLayer *gl = [CAGradientLayer layer]; [gl addSublayer:gl1]; [gl addSublayer:gl2]; //add to mask of UIView self.underTextView.layer.mask = gl; 

在此处输入图像描述

那么我在方法中更改CAGradientLayer中颜色的位置- (void)scrollViewDidScroll:(UIScrollView *)scrollView用于顶部和底部渐变。 因此,当我打开textView时,我看到顶部是好文本,底部是透明的,滚动时我在顶部和底部都是透明的,当滚动到底部时 – 顶部是透明的,底部是不透明的

在此处输入图像描述在此处输入图像描述

 - (void)scrollViewDidScroll:(UIScrollView *)scrollView { CGFloat layerWidth = 8.f; CAGradientLayer *gl1 = [CAGradientLayer layer]; id col1 = (id)[[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0]CGColor]; id col2 = (id)[[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0]CGColor]; gl1.colors = @[col2, col1, col1, col2]; if (self.mainTextView.contentOffset.y < 5) gl1.locations = @[@0.0, @0.0, @0.85, @1.0]; if (self.mainTextView.contentOffset.y >=5 && self.mainTextView.contentOffset.y < 20) { CGFloat number = (self.mainTextView.contentOffset.y - 5) * 0.01; gl1.locations = @[@0.0, [NSNumber numberWithFloat:number], @0.85, @1.0]; } if (self.mainTextView.contentOffset.y >= 20 && self.mainTextView.contentOffset.y < self.mainTextView.contentSize.height - self.mainTextView.height - 20) gl1.locations = @[@0.0, @0.15, @0.85, @1.0]; if (self.mainTextView.contentOffset.y >= (self.mainTextView.contentSize.height - self.mainTextView.height - 20) && self.mainTextView.contentOffset.y < (self.mainTextView.contentSize.height - self.mainTextView.height - 5)) { CGFloat number = 1 - (self.mainTextView.contentSize.height - self.mainTextView.contentOffset.y - self.mainTextView.height - 5) * 0.01; gl1.locations = @[@0.0, @0.15, [NSNumber numberWithFloat:number], @1.0]; } if (self.mainTextView.contentOffset.y >= self.mainTextView.contentSize.height - self.mainTextView.height - 5) gl1.locations = @[@0.0, @0.15, @1.0, @1.0]; gl1.frame = CGRectMake(0, 0, self.underTextView.width - layerWidth, self.underTextView.height); CAGradientLayer *gl2 = [CAGradientLayer layer]; gl2.colors = @[col1, col1]; gl2.locations = @[@0.0, @1.0]; gl2.frame = CGRectMake(self.underTextView.width - layerWidth, 0, layerWidth, self.underTextView.height); CAGradientLayer *gl = [CAGradientLayer layer]; [gl addSublayer:gl1]; [gl addSublayer:gl2]; self.underTextView.layer.mask = gl; }