Remove UITextView extra border

2019-02-28 21:00发布

I've made a UITextView yet there's some unwanted spacing:

black border

(I'm drawing the white box separately). I'm fairly certain I got the coordinates right since if I scroll it gets cut off at the right places:

scrolled to show bounds

How can I make the text be drawn further to the left, and also to have the top-most position be further on the top? That is, I'd like it to look like this:

desired outcome

I've tried [[textView layer] setBorderWidth:0.0f], but to no avail - it already doesn't have a border. This is what it looks like if I set the border width to 2 and to the color red:

with layer red border

I'm curious where the extra spacing is coming from and how to control it.

2条回答
兄弟一词,经得起流年.
2楼-- · 2019-02-28 21:15

Have you tried experimenting with textContainerInset?

According to the docs at developer.apple.com, "This property provides text margins for text laid out in the text view." This is new in iOS 7.

查看更多
Anthone
3楼-- · 2019-02-28 21:34

It seems that the following does the trick:

UITextView *textView = ...;
[textView setContentInset:UIEdgeInsetsMake(-10, -8, 0, 0)];

The result is exactly as I wanted it to be. This isn't too satisfying as it uses magic numbers, so if anyone has a better answer, feel free to comment or answer.

查看更多
登录 后发表回答