Gradient effect in UITextView iPhone?

佐手、 提交于 2021-02-07 10:31:13

问题


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

enter image description here


回答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. enter image description here




回答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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!