I have a UITextView
in my iOS Application, which displays a large amount of text. I am then paging this text by using the offset margin parameter of the UITextView
. My problem is that the padding of the UITextView
is confusing my calculations as it seems to be different depending on the font size and typeface that I use.
Therefore, I pose the question: Is it possible to remove the padding surrounding the content of the UITextView
?
Look forward to hearing your responses!
I would definitely avoid any answers involving hard-coded values, as the actual margins may change with user font-size settings, etc.
Here is @user1687195's answer, written without modifying the
textContainer.lineFragmentPadding
(because the docs state this is not the intended usage).This works great for iOS 7 and later.
This is effectively the same outcome, just a bit cleaner in that it doesn't misuse the lineFragmentPadding property.
you can use
textContainerInset
property ofUITextView
:textView.textContainerInset = UIEdgeInsetsMake(10, 10, 10, 10);
(top, left, bottom, right)
Here's an easy little extension that will remove Apple's default margin from every text view in your app.
Note: Interface Builder will still show the old margin, but your app will work as expected.
Building off some of the good answers already given, here is a purely Interface Builder-based solution that makes use of User Defined Runtime Attributes and works in iOS 7.0+:
For me (iOS 11 & Xcode 9.4.1) what worked magically was setting up textView.font property to
UIFont.preferred(forTextStyle:UIFontTextStyle)
style and also the first answer as mentioned by @Fattie. But the @Fattie answer did not work till I set the textView.font property else UITextView keeps behaving erratically.All these answers address the title question, but I wanted to propose some solutions for the problems presented in the body of the OP's question.
Size of Text Content
A quick way to calculate the size of the text inside the
UITextView
is to use theNSLayoutManager
:This gives the total scrollable content, which may be bigger than the
UITextView
's frame. I found this to be much more accurate thantextView.contentSize
since it actually calculates how much space the text takes up. For example, given an emptyUITextView
:Line Height
UIFont
has a property that quickly allows you to get the line height for the given font. So you can quickly find the line height of the text in yourUITextView
with:Calculating Visible Text Size
Determining the amount of text that is actually visible is important for handling a "paging" effect.
UITextView
has a property calledtextContainerInset
which actually is a margin between the actualUITextView.frame
and the text itself. To calculate the real height of the visible frame you can perform the following calculations:Determining Paging Size
Lastly, now that you have the visible text size and the content, you can quickly determine what your offsets should be by subtracting the
textHeight
from thetextSize
:Using all of these methods, I didn't touch my insets and I was able to go to the caret or wherever in the text that I want.