ChatGPT解决这个技术问题 Extra ChatGPT

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"

https://i.stack.imgur.com/3kOp8.png

I tried replacing an empty string with an empty string as you said. But if I comment out the inset now I have top and left padding again.
Not an empty with empty. Replace " " (space) with "" (empty). Is it possible that you replace the UITextView with a custom UILabel?
I set the String to "00", there are no spaces. I'm not sure if its possible, but I created a UIview for the navigation.rightbarbuttonitem, then init'd with custom view. I added an image view and a textview to the view, I did't think I could subview a label
UILabel is wrong, I meant custom UITextField. UITextView inherits from UIScrollView with a lot of things you don't need in that short field.
But is text field going to have the same padding issues? I disabled most of the scrollview features like editable, scrollenabled along with some others.

d
dbart

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.


Works exactly as stated. UITextView textContainerInset is made specifically for this purpose and because it doesn't require subclass, it is the best answer for systems running iOS7 and above.
Add this line too to remove left padding self.textView.textContainer.lineFragmentPadding = 0;
THIS should be marked as the right answer. Thank you sir!
What about the vertical padding ? Should it has to be unpadded with some negative vertical edge inset ? this thing feels like a joke (:. Apple, why ?
Apple Docs have a nice visual description about that
A
App Dev Guy

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.


This is the best solution because it uses the default lineFragmentPadding value rather than hardcoding numbers in. For newer swift use textContainerInset = UIEdgeInsets(top: 0, left: -padding, bottom: 0, right: -padding)
A
Abhishek Biswas

Swift 4 version for the OA

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

S
Sunil Chauhan

For iOS 11.7

Following code worked for me:

    textView.textContainer.lineFragmentPadding = CGFloat(0.0)
    textView.textContainerInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
    textView.contentInset = UIEdgeInsets(top: 0,left: 0,bottom: 0,right: 0)

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.


M
Mohamed A.Karim

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 ;)


It is the best solution for iOS6. But it need forward declare like this: @interface UITextView (Stylesheet) -(id)styleString; @end
This answer from the old days is still utterly brilliant, but these days the answer above in Swift (dbart/mattsven etc) is the Swift answer!
u
user3106522

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


In general, it's not a great idea to hard code values as your doing in your else statement. You never know how this will change in the future or be displayed across devices and whatnot.
As said above, this isn't a good solution. It's never great to "guesstimate" values like this - you should find out what the inset is, by calculating it or simply just eliminate it with the proper features of the item you're working on. The textView.textContainerInset = UIEdgeInsets.zero and textView.textContainer.lineFragmentPadding = 0 works like a charm in this case..