How to lose margin/padding in UITextView?

0 votes
asked Apr 14, 2009 by sniurkst

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!

14 Answers

0 votes
answered Apr 26, 2009 by michael

This workaround was written in 2009 when IOS 3.0 was released. It no longer applies.

I ran into the exact same problem, in the end I had to wind up using

nameField.contentInset = UIEdgeInsetsMake(-4,-8,0,0);

where nameField is a UITextView. The font I happened to be using was Helvetica 16 point. Its only a custom solution for the particular field size I was drawing. This makes the left offset flush with the left side, and the top offset where I want it for the box its draw in.

In addition, this only seems to apply to UITextViews where you are using the default aligment, ie.

nameField.textAlignment = NSTextAlignmentLeft;

Align to the right for example and the UIEdgeInsetsMake seems to have no impact on the right edge at all.

At very least, using the .contentInset property allows you to place your fields with the "correct" positions, and accommodate the deviations without offsetting your UITextViews.

0 votes
answered Jan 11, 2011 by to0nice4eva
[firstNameTextField setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
0 votes
answered Jan 31, 2011 by rp90

Doing the inset solution I still had padding on the right side and the bottom. Also text alignment was causing issues. The only sure fire way I found was to put the text view inside another view that is clipped to bounds.

0 votes
answered Apr 8, 2012 by engin-kurutepe

On iOS 5 UIEdgeInsetsMake(-8,-8,-8,-8); seems to work great.

0 votes
answered Jan 28, 2013 by nikita

I have found one more approach, getting view with text from UITextView's subviews and setting it up in layoutSubview method of a subclass:

- (void)layoutSubviews {
    [super layoutSubviews];

    const int textViewIndex = 1;
    UIView *textView = [self.subviews objectAtIndex:textViewIndex];
    textView.frame = CGRectMake(
                                 self.bounds.size.width - (2.0f * kStatusViewContentOffset),
                                 self.bounds.size.height - kStatusViewContentOffset);
0 votes
answered Apr 5, 2013 by uladzimir

Storyboard or Interface Builder solution, using User defined runtime attributes. Update. Added iOS7.1&iOS6.1 screenshots with contentInset = {{-10, -5}, {0, 0}} enter image description here enter image description here

0 votes
answered Apr 24, 2013 by user1687195

For iOS 7.0, I've found that the contentInset trick no longer works. This is the code I used to get rid of the margin/padding in iOS 7.

This brings the left edge of the text to the left edge of the container:

self.textView.textContainer.lineFragmentPadding = 0

This causes the top of the text to align with the top of the container

self.textView.textContainerInset = .zero

Both Lines are needed to completely remove the margin/padding.

0 votes
answered Apr 4, 2015 by azdev

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+:

enter image description here

0 votes
answered Apr 5, 2015 by himanshu-padia

you can use textContainerInset property of UITextView:

textView.textContainerInset = UIEdgeInsetsMake(10, 10, 10, 10);

(top, left, bottom, right)

0 votes
answered Apr 26, 2015 by mikeho

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 the NSLayoutManager:

UITextView *textView;
CGSize textSize = [textView usedRectForTextContainer:textView.textContainer].size;

This gives the total scrollable content, which may be bigger than the UITextView's frame. I found this to be much more accurate than textView.contentSize since it actually calculates how much space the text takes up. For example, given an empty UITextView:

textView.frame.size = (width=246, height=50)
textSize = (width=10, height=16.701999999999998)
textView.contentSize = (width=246, height=33)
textView.textContainerInset = (top=8, left=0, bottom=8, right=0)

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 your UITextView with:

UITextView *textView;
CGFloat lineHeight = textView.font.lineHeight;

Calculating Visible Text Size

Determining the amount of text that is actually visible is important for handling a "paging" effect. UITextView has a property called textContainerInset which actually is a margin between the actual UITextView.frame and the text itself. To calculate the real height of the visible frame you can perform the following calculations:

UITextView *textView;
CGFloat textViewHeight = textView.frame.size.height;
UIEdgeInsets textInsets = textView.textContainerInset;
CGFloat textHeight = textViewHeight - - textInsets.bottom;

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 the textSize:

// where n is the page number you want
CGFloat pageOffsetY = textSize - textHeight * (n - 1);
textView.contentOffset = CGPointMake(textView.contentOffset.x, pageOffsetY);

// examples
CGFloat page1Offset = 0;
CGFloat page2Offset = textSize - textHeight
CGFloat page3Offset = textSize - textHeight * 2

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.

Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter