在UITextView中添加“padding”

65

如标题所述,我正在尝试为UITextView添加类似于padding的行为。当视图被推入我的导航控制器时生成文本视图,并出现以下代码:

 self.textView.layer.cornerRadius = 7;
 //pretty stuff is pretty

 NSString *description = appDelegate.productInDisplay.description;
 [self.textView setText:description];
 //pretty stuff has content now


 CGRect frame = textView.frame;
 frame.size.height = textView.contentSize.height;
 textView.frame = frame;
 //set the UITextView to the size of it's containing text.

 self.textView.editable = NO;

  self.theScrollView.contentSize = CGSizeMake(320, 250 + textView.frame.size.height);
  //set the parent scrollView's height to fit some other elements with fixed size (250)
  //and the pre-mentioned UITextView

所以,一切都正常工作,但是我想在UITextView的所有四个边缘上添加一些填充,并且在谷歌搜索了三个小时后,我无法做到这一点,尽管这似乎相当容易。有什么建议吗?


31
只需要这样... uitextview.textContainerInset = UIEdgeInsetsMake(8,5,8,5); // 上边距,左边距,下边距,右边距... 就是这么简单 - Fattie
请注意,他们已经删除了“make”版本:现在它只是一个简单的文本容器插图,其代码为**textContainerInset = UIEdgeInsets(top: 8, left: 0, bottom: 8, right: 32)**。 - Fattie
8个回答

107

使用 Objective-C,我只是这样做的

[self.textView setTextContainerInset:UIEdgeInsetsMake(0, 12, 0, 12)];

对于 Swift,您可以使用:

textview.contentInset = UIEdgeInsets(top: 0, left: 12, bottom: 0, right: 12)

您还可以创建一个 Swift 扩展(由 chowdhury-md-rajib-sarwar 提议)这里

extension UITextView {
func leftSpace() {
    self.textContainerInset = UIEdgeInsets(top: 4, left: 6, bottom: 4, right: 4)
}

然后使用

let textView = UITextView()
textView.leftSpace() 

1
这在iOS 7中对我有效。实际上,这是建议答案中唯一有效的一个。 - Trevor Gehman
Mário Carvalho 在 iOS 6 中有没有“setTextContainerInset”的任何可能替代方案? - GoGreen
1
@Mário Carvalho,问题是我需要支持iOS 6和7,所以那个答案不可行。我发现这个答案更有意义,但它仍然没有解决我的问题。最终,我使用了一个容器视图来处理UITextView。感谢您的帮助! - GoGreen
3
在Swift中,我使用了self.textView.textContainerInset = UIEdgeInsetsMake(50, 50, 50, 50)。 - FredL
1
@KayanAlmeida 是的!在Storyboard选项中检查插入。 - Mário Carvalho
显示剩余2条评论

69

这个答案是完全错误的。正确答案很简单:

uitextview.textContainerInset =
       UIEdgeInsetsMake(8,5,8,5); // top, left, bottom, right

(这些值通常与同一屏幕上的UITextField外观相匹配。)


使用这个:

self.textView.contentInset = UIEdgeInsetsMake(5, 5, 5, 5); 

1
如果您通过编程方式将文本视图的角落圆形化,那么该文本容器插入就无法正常工作,但是contetInset则没有任何问题。谢谢! - ACAkgul

31

以下是适用于 Swift 5 的代码:

textView.textContainerInset = UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20)

26

编辑于2015年1月16日:本文于2012年撰写,已经不再准确。请按照上面提到的使用-textContainerInset。

原始文章:

使用contentInset不会真正起作用。你有两种合理的选择:子类化UITextField并覆盖textRectForBounds:和editingRectForBounds:方法,或者在UIView上透明地创建文本字段并对UIView进行样式设置。

UITextField子类化示例:

- (CGRect)textRectForBounds:(CGRect)bounds {
     return CGRectInset(bounds, 5, 5);
}

- (CGRect)editingRectForBounds:(CGRect)bounds {
     return CGRectInset(bounds, 5, 5);
}

将其包装在UIView中的示例:

UIView *wrapView = [[UIView alloc] initWithFrame: CGRectMake(10, 10, 200, 30)];
[wrapView addSubview:textView];
wrapView.layer.borderColor = [UIColor darkGrayColor].CGColor;
wrapView.layer.borderWidth = 2.0;
wrapView.layer.cornerRadius = 5.0;

这应该是被接受的答案——当前的答案实际上并不起作用。 - Nick Baicoianu
这比添加空的leftView更好。有时候在iOS 5上,当触摸占位符文本区域时,UITextField并不会成为第一个响应者。 - Haitao Li
22
我不理解为什么这被接受为正确答案。问题是关于UITextView的,而非UITextField。UITextView没有这样的方法,所以至少答案的第一部分是不正确的。第二部分也有问题,因为滚动指示器也会显示缩进。 - José Manuel Sánchez

12

我在使用 Swift 2.0 时,以下方法可行:

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


@JulianB. UITextField有类似的东西吗? - Shivam Pokhriyal

6

对于Swift 4.2:

textview.contentInset = UIEdgeInsets(top: 2, left: 10, bottom: 2, right: 10)

4

对于Swift 3-答案似乎类似,但略有不同:

textview.contentEdgeInsets = UIEdgeInsets(top: 2, left: 10, bottom: 2, right: 10)

实际上,我在UIButtonView中使用了上述内容,但是由于它与为UITextView发布的答案非常接近,所以我认为[希望]它也适用于那个。


4
抱歉给您点了负评,但实际上这与此处无关。在UITextView上,它被称为textView.textContainerInset。 - Alex Wally
2
同样的代码在 Swift 4 中的版本为:textView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 50) - Codetard

2

只需创建一个UITextViewextension,使用容器边缘插图如下所示:

extension UITextView {
    func leftSpace() {
        self.textContainerInset = UIEdgeInsets(top: 4, left: 6, bottom: 4, right: 4)
    }
}

and use it like:

let textView = UITextView()
textView. leftSpace() 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接