简体   繁体   English

UITextView文本上的底部阴影

[英]UITextView bottom shadow on text

Maybe you know the solution, how to add shadow on textView like: 也许您知道解决方案,如何在textView上添加阴影,例如:

在此处输入图片说明

So I need a transparent textView, where I can see view in the background 所以我需要一个透明的textView,在这里我可以在后台看到视图

在此处输入图片说明

Apple provides CAReplicatorLayer which you can use to do automatic mirroring of a layer (amonst other things). Apple提供了CAReplicatorLayer ,您可以使用它来对图层进行自动镜像(其他操作)。 This even works for video layers, and is very efficient (executing its drawing directly on the GPU). 这甚至适用于视频层,并且非常有效(直接在GPU上执行其绘制)。

The CAReplicatorLayer class creates a specified number of copies of its sublayers (the source layer), each copy potentially having geometric, temporal and color transformations applied to it. CAReplicatorLayer类创建其子层(源层)的指定数量的副本,每个副本都可能对其应用了几何,时间和颜色转换。

There is a WWDC video ( Core Animation Essentials around 51:00) which talks briefly about how to use this, as well as an OSX demo project called ReplicatorDemo . 有一个WWDC视频( Core Animation Essentials在51:00左右),简要介绍了如何使用它,还有一个OSX演示项目ReplicatorDemo

A quick search reveals a blog post that gives an example of reflecting a layer, although I haven't checked the quality of the code/technique. 快速搜索显示了一篇博文 ,该博文提供了一个反映层的示例,尽管我尚未检查代码/技术的质量。

I think you can combine the below methods (it is not tested!): 我认为您可以结合以下方法(未经测试!):

How do I use the NSString draw functionality to create a UIImage from text 如何使用NSString绘制功能从文本创建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

I've done this in the past on a white background by making an white image with a gradient that starts full opaque and fades to clear. 过去,我是在白色背景上通过使白色图像具有逐渐完全不透明并逐渐消失的渐变色来完成的。 I overlaid this on top of my the bottom edge of my table view. 我将其覆盖在表格视图底部的顶部。 It gave the appearance of the table view fading to white at the bottom edge. 它使表格视图的外观在底部边缘逐渐变为白色。

I solved the "bottom" issue by just having the image slide down as I reached the very bottom of the scrolling content of the table so the last bits wouldn't fade. 我解决了“底部”问题,方法是在到达表格滚动内容的最底部时使图像向下滑动,以使最后几位不会消失。

If you have a complicated, moving or varied background, this might not be the best option, but for a simple background is worked well. 如果背景复杂,变化多端或变化多端,这可能不是最佳选择,但是对于简单的背景来说效果很好。

I find the best sulution. 我找到最好的解决方案。 I use mask layer and CAGradientLayer. 我使用遮罩层和CAGradientLayer。 So if we use it for UITextView our layer will be dinamically. 因此,如果我们将其用于UITextView,则该层将是动态的。 I create UIView, add my UITextView on this UIView and use layer for UIView 我创建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;

在此处输入图片说明

So then I change locations of colors in CAGradientLayer in method - (void)scrollViewDidScroll:(UIScrollView *)scrollView for top and bottom gradient. 所以然后我在方法- (void)scrollViewDidScroll:(UIScrollView *)scrollView顶部和底部渐变的颜色在CAGradientLayer中的位置。 So when I open textView I see good text on top and transparent in bottom, when scrolling I have transparent in top and in bottom, when scroll to bottom - transparent in top and opaque in bottom 因此,当我打开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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM