问题
I am working on a project where I need to implement center gradient in UITextView. It must be something like attached image. I cannot use any image at upper of UITextView because I want user interaction in UITextView. Please provide your suggestions. Thanks
回答1:
If you apply CAGRradient Layer on UITextView and set [txtView.layer setMask:gradient] this will create problem while scrolling means the gradient layer will scroll. To overcome from this problem you must use a UiView working as a container view for your textView. first of all add your container view and then add your text view as subview in your container view. and then use this code which save my life.
CAGradientLayer gradient = [CAGradientLayer layer];
gradient.frame = view_bottomMenu.bounds;
gradient.colors = [NSArray arrayWithObjects:(id)[[[UIColor blackColor colorWithAlphaComponent:0.2] CGColor],(id)[[[UIColor blackColor] colorWithAlphaComponent:0.3] CGColor],(id)[[[UIColor blackColor] colorWithAlphaComponent:0.3] CGColor],(id)[[[UIColor blackColor] colorWithAlphaComponent:0.3] CGColor], (id)[[[UIColor clearColor] colorWithAlphaComponent:1.0] CGColor], (id)[[[UIColor blackColor] colorWithAlphaComponent:0.3] CGColor],(id)[[[UIColor blackColor] colorWithAlphaComponent:0.3] CGColor],(id)[[[UIColor blackColor] colorWithAlphaComponent:0.3] CGColor],
(id)[[[UIColor blackColor] colorWithAlphaComponent:0.2] CGColor], nil];
gradient.startPoint = CGPointMake(0.0f,0.0f);
gradient.endPoint = CGPointMake(0.0f, 1.0f);
[view_bottomMenu.layer setMask:gradient];
where view_bottomMenu is UIView working as container view.here is my output.
回答2:
For achieving this kind of effect you would have to implement masking on the UItextView
. Please check
Applying CAGradient mask layer to UITextView .
This shows how to change the color and fade certain parts of the UITextView
.
来源:https://stackoverflow.com/questions/18355106/gradient-effect-in-uitextview-iphone