mattyd
mattyd

Reputation: 1683

IOS - remove ALL padding from UITextView

There are many great examples on SO to remove the left padding of a UITextView.

How to lose margin/padding in UITextView?

However, I need to remove the right padding too.

I have tried...

[tv setContentInset: UIEdgeInsetsMake(-4,-8,-8,-X)];//where X is any integer

and just about every other permutation of the last two values to remove the padding and nothing seems to work. Have also tried

[tv sizeToFit];
[tv setTextAlignment:[NSTextAlignmentRight]];

The following Text in the Textview says "00"

enter image description here

Upvotes: 95

Views: 51345

Answers (6)

Irtaza fayaz
Irtaza fayaz

Reputation: 468

For iOS 11.7

Following code worked for me:

textView.textContainer.lineFragmentPadding = 0
textView.textContainerInset = .zero
textView.contentInset = .zero

From the Official Documentation:

textContainerInset : It is possible to set the text container and view sizes and resizing behavior so that the inset cannot be maintained exactly, although the text system tries to maintain the inset wherever possible.

In any case, the textContainerOrigin and size of the text container are authoritative as to the location of the text container within the view. The text itself can have an additional inset, inside the text container, specified by the lineFragmentPadding method of NSTextContainer.

Upvotes: 20

samwize
samwize

Reputation: 27353

To completely remove all padding, the lineFragmentPadding must be taken into account.

let padding = textView.textContainer.lineFragmentPadding
textView.textContainerInset =  UIEdgeInsets(top: 0, left: -padding, bottom: 0, right: -padding)

The lineFragmentPadding default value is 5, and is at the beginning and end of fragment rectangle.

Some answers suggested setting lineFragmentPadding to 0. However, as per discussed in the doc, it is not designed to express text margins. So do not set it to 0.

Upvotes: 55

Abhishek Biswas
Abhishek Biswas

Reputation: 1243

Swift 4 version for the OA

self.tDescription.textContainerInset = UIEdgeInsets.zero
self.tDescription.textContainer.lineFragmentPadding = 0

Upvotes: 28

dbart
dbart

Reputation: 5566

Although it is iOS 7 only, an extremely clean solution is to set the textView's textContainerInsets as such:

[textView setTextContainerInset:UIEdgeInsetsZero];
textView.textContainer.lineFragmentPadding = 0; // to remove left padding

This will effectively remove all padding (insets) around the text inside the text view. If your deployment target is iOS 7+ then this is the best solution thus far.

Upvotes: 256

user3106522
user3106522

Reputation: 45

My problem is solved this way

if([Utils isiOS7orHigher]){
    commentView.textContainerInset = UIEdgeInsetsZero;
}else {
    commentView.contentInset = UIEdgeInsetsMake(-11,-8,0,0);
}

for more see http://foobarpig.com/iphone/get-rid-of-uitextview-padding.html

Upvotes: -4

Mohamed A.Karim
Mohamed A.Karim

Reputation: 404

Please try sub-classing the UITextView and overriding the following method:

    - (id)styleString
{
    return [[super styleString] stringByAppendingString:@"; line-height: 1.6em;margin-right: 30px; margin-left: 0px; margin-top: 0px;"];
}

Apparently; you can tweak the margin-left, margin-top & whatever you want ;)

Upvotes: 7

Related Questions