How to make UITextView background transparent?

2019-09-02 20:57发布

In my application, I have a large text field that allows the user to input notes, but the only issue is that it does not blend into the background image that I have set. With the UITextField, you can set the border in the Interface Builder so that it does not have a white background, it has a transparent background making it show the background I have set behind it. I did not see this Border option with the UITextView, does anyone know how I can achieve the same effect? (I'm using Xcode 4.2 by the way).

Thank you!

3条回答
可以哭但决不认输i
2楼-- · 2019-09-02 21:28

To set the background to be transparent on a UITextView you can use

UITextView* myTextView = [[UITextView alloc] initWithFrame:CGRectMake(0,0,280,180)];
[myTextView setBackgroundColor:[UIColor clearColor]];

If you want to add a border to the UITextView then you would need to add that to the layer of the textView which you could do in the following way

[[myTextView layer] setCornerRadius:15.0f]; //You don't need to set this but it will give the view rounded corners.
[[myTextView layer] setBorderWidth:1.0f]; //again you pick the value 1 is quite a thin border
[[myTextView layer] setBorderColor:[[UIColor blackColor] CGColor]]; //again you can choose the color.

That should answer both your questions I think.

查看更多
女痞
3楼-- · 2019-09-02 21:39

You can change the opacity of the background in Interface Builder.

Text View with background

Select it and change the background colour

Slide the opacity slider to make the background transparent.

查看更多
一纸荒年 Trace。
4楼-- · 2019-09-02 21:43

You can use following code-

#import <QuartzCore/QuartzCore.h>

....


textView.layer.borderWidth = 5.0f;
textView.layer.borderColor = [[UIColor grayColor] CGColor];

decide color and border width as per your need.

查看更多
登录 后发表回答