UITextView 在文本上的底部阴影 [英] UITextView bottom shadow on text

查看:35
本文介绍了UITextView 在文本上的底部阴影的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

我找到了最好的解决方案.我使用遮罩层和 CAGradientLayer.因此,如果我们将它用于 UITextView,我们的图层将是动态的.我创建 UIView,在这个 UIView 上添加我的 UITextView 并为 UIView 使用层

I find the best sulution. I use mask layer and CAGradientLayer. So if we use it for UITextView our layer will be dinamically. I create UIView, add my UITextView on this UIView and use layer for 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;

然后我在方法 - (void)scrollViewDidScroll:(UIScrollView *)scrollView 中更改 CAGradientLayer 中颜色的位置,用于顶部和底部渐变.所以当我打开 textView 我看到顶部和底部透明的良好文本,滚动时顶部和底部透明,滚动到底部时 - 顶部透明,底部不透明

So then I change locations of colors in CAGradientLayer in method - (void)scrollViewDidScroll:(UIScrollView *)scrollView for top and bottom gradient. 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

- (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;
}

这篇关于UITextView 在文本上的底部阴影的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆