如何在iOS UITextView
中获得相同的效果,如下图所示。
来自UITextView
提前致谢:)
答案 0 :(得分:2)
尝试以下操作:我尝试了以下图片但不是UITextView
UIView *vwTest = [[UIView alloc] initWithFrame:_textView.frame];
[vwTest setBackgroundColor:[UIColor whiteColor]];
[vwTest setUserInteractionEnabled:NO];
[self.view addSubview:vwTest];
NSArray *colors = [NSArray arrayWithObjects:
(id)[[UIColor colorWithWhite:0 alpha:1] CGColor],
(id)[[UIColor colorWithWhite:0 alpha:0] CGColor],
nil];
CAGradientLayer *layer = [CAGradientLayer layer];
[layer setFrame:vwTest.bounds];
[layer setColors:colors];
[layer setStartPoint:CGPointMake(0.0f, 1.0f)];
[layer setEndPoint:CGPointMake(0.0f, 0.6f)];
[vwTest.layer setMask:layer];
答案 1 :(得分:0)
只需在文本视图底部放置一个UIImageView,其中包含从透明到白色的渐变图像。这是我猜的最简单的方式。
答案 2 :(得分:0)
let gradientLayer: CAGradientLayer = CAGradientLayer()
gradientLayer.frame = CGRectMake(0.0, 0.0, CGRectGetWidth(textView.frame), CGRectGetHeight(textView.frame))
gradientLayer.colors = [UIColor.init(white: 1, alpha: 0).CGColor, UIColor.whiteColor().CGColor]
gradientLayer.startPoint = CGPointMake(0, 0.6)
gradientLayer.endPoint = CGPointMake(0.0, 1)
self.textview.layer.addSublayer(gradientLayer)
答案 3 :(得分:0)
上面@Charlie的答案在Swift 5.x上对我来说不起作用,我必须进行一些更改。
首先添加一个带有UITextView的UIView。然后我确实像这样应用了calare:
let gradientLayer: CAGradientLayer = CAGradientLayer()
gradientLayer.frame = CGRect.init(x: 0.0, y: 0.0, width: textView.frame.width, height: textView.frame.height)
gradientLayer.colors = [UIColor.init(white: 1, alpha: 0).cgColor, UIColor.white.cgColor]
gradientLayer.startPoint = CGPoint.init(x: 0.0, y: 0.6)
gradientLayer.endPoint = CGPoint.init(x: 0.0, y: 1.0)
self.contentTextView.layer.addSublayer(gradientLayer)