【问题标题】:UITextView bottom shadow on textUITextView 在文本上的底部阴影
【发布时间】:2013-04-08 08:14:00
【问题描述】:

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

所以我需要一个透明的 textView,我可以在其中看到背景中的视图

【问题讨论】:

  • 什么样的决定?

标签: ios objective-c uitextview


【解决方案1】:

Apple 提供了CAReplicatorLayer,您可以使用它来自动镜像图层(除其他外)。这甚至适用于视频层,并且非常高效(直接在 GPU 上执行其绘制)。

CAReplicatorLayer 类创建指定数量的 它的子层(源层),每个副本可能具有 对其应用几何、时间和颜色变换。

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

快速搜索发现blog post,它提供了一个反射层的示例,尽管我没有检查代码/技术的质量。

【讨论】:

    【解决方案2】:

    我认为您可以结合以下方法(未经测试!):

    How do I use the NSString draw functionality to create a UIImage from text

    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
    

    【讨论】:

      【解决方案3】:

      我过去曾在白色背景上完成此操作,方法是制作具有渐变的白色图像,该渐变开始完全不透明,然后逐渐变清晰。我将其覆盖在表格视图的底部边缘之上。它使表格视图的外观在底部边缘淡化为白色。

      当我到达表格滚动内容的最底部时,我通过让图像向下滑动解决了“底部”问题,这样最后几位就不会消失。

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

      【讨论】:

        【解决方案4】:

        我找到了最好的解决方案。我使用遮罩层和 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;
        

        然后我在方法- (void)scrollViewDidScroll:(UIScrollView *)scrollView 中更改CAGradientLayer 中颜色的位置以进行顶部和底部渐变。所以当我 打开 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;
        }
        

        【讨论】:

          猜你喜欢
          • 2015-01-20
          • 2017-08-23
          • 2023-03-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多