IOS - 从 UITextView 中删除所有填充

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15823991/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:02:37  来源:igfitidea点击:

IOS - remove ALL padding from UITextView

iosuitextviewpadding

提问by mattyd

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

SO 上有很多很好的例子来删除 UITextView 的左填充。

How to lose margin/padding in UITextView?

如何在 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"

Textview 中的以下文本显示“00”

enter image description here

在此处输入图片说明

回答by dbart

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

虽然它只是 iOS 7,但一个非常干净的解决方案是将 textView 的 textContainerInsets 设置为这样:

[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.

这将有效地删除文本视图内文本周围的所有填充(插图)。如果您的部署目标是 iOS 7+,那么这是迄今为止最好的解决方案。

回答by samwize

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

要完全删除所有填充,lineFragmentPadding必须考虑。

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

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

lineFragmentPadding默认值是5,并且在片段矩形的开始和结束。

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

一些答案建议设置lineFragmentPadding为 0。但是,根据文档中的讨论,它不是用来表达文本边距的。所以不要设置为0。

回答by Mohamed A.Karim

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

请尝试对 UITextView 进行子类化并覆盖以下方法:

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

显然; 你可以调整 margin-left、margin-top 和任何你想要的 ;)

回答by Abhishek Biswas

Swift 4 version for the OA

面向 OA 的 Swift 4 版本

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

回答by 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

有关更多信息,请参阅http://foobarpig.com/iphone/get-rid-of-uitextview-padding.html