【问题标题】:Add "padding" to a UITextView向 UITextView 添加“填充”
【发布时间】:2011-12-15 17:20:57
【问题描述】:

正如标题所说,我正在尝试向 UITextView 添加类似填充的行为。当视图被推入我的导航控制器并发生以下代码时,会生成文本视图:

 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 的所有 4 个侧面添加一些填充,我无法通过 3 小时的谷歌搜索来完成此操作,这似乎很容易。有什么建议吗?

【问题讨论】:

  • 这只是.. uitextview.textContainerInset = UIEdgeInsetsMake(8,5,8,5); // 上、左、下、右 ... 就这么简单

标签: ios uitextview padding


【解决方案1】:

使用 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建议)here:

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

}

然后使用

let textView = UITextView()
textView.leftSpace() 

希望这会有所帮助, 马里奥

【讨论】:

  • 这在 iOS 7 中对我有用。事实上,这是唯一有效的建议答案之一。
  • Mário Carvalho iOS 6 中“setTextContainerInset”的任何可能替代方案?
  • @Mário Carvalho 问题是,我需要同时支持 iOS 6 和 7,因此,这个答案不可行。我发现这个answer 更有意义,但它仍然没有解决我的问题。我最终为我的 UITextView 使用了一个容器视图。不过感谢您的帮助!
  • 在 Swift 中我使用了 self.textView.textContainerInset = UIEdgeInsetsMake(50, 50, 50, 50);
  • @KayanAlmeida 是的!检查情节提要选项的插图
【解决方案2】:

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

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

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


使用这个:

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

【讨论】:

  • 如果您以编程方式绕过 textview 的角,则 puking textContainerInset 不起作用,但 contetInset 完全没有问题。谢谢!
【解决方案3】:

编辑 2015 年 1 月 16 日:这是 2012 年编写的,不再准确。如上所述使用 -textContainerInset。

原帖:

使用 contentInset 实际上不起作用。您有两个合理的选择:继承 UITextField 并覆盖 textRectForBounds: 和编辑RectForBounds: 方法,或者在 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;

【讨论】:

  • 这应该是公认的答案——当前的答案实际上不起作用。
  • 这比添加空leftView要好。使用 leftView 有时 UITextField 在触摸占位符文本区域时不会成为第一响应者(在 iOS 5 上)。
  • 我不明白这是如何被接受为正确答案的。问题是针对 UITextView,而不是针对 UITextField。 UITextView 没有这样的方法,所以至少答案的第一部分是不正确的。第二部分有问题,因为滚动指示器也会缩进显示。
【解决方案4】:

这适用于 Swift 5:

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

【讨论】:

    【解决方案5】:

    这对我使用 swift 2.0 有效:

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

    【讨论】:

    • @JulianB。 UITextField 有没有类似的东西
    【解决方案6】:

    对于 Swift 4.2:

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

    【讨论】:

      【解决方案7】:

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

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

      (实际上,我将上述内容用于 UIButtonView,但由于它与为 UITextView 发布的答案非常接近,我想 [希望] 它也适用于此)

      【讨论】:

      • 很抱歉投反对票,但实际上它不适用于这里。在 UITextView 它被称为 textView.textContainerInset
      • Swift 4 版本相同:textView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 50)
      【解决方案8】:

      使用 Container Edge Inset 简单地创建 UITextView 的 extension,如下所示:

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

      并像这样使用它:

      let textView = UITextView()
      textView. leftSpace() 
      

      【讨论】:

        猜你喜欢
        • 2013-01-29
        • 2019-07-31
        • 1970-01-01
        • 1970-01-01
        • 2020-11-20
        • 2012-02-06
        • 2015-02-12
        • 2015-08-14
        • 2013-12-13
        相关资源
        最近更新 更多