将"填充"添加到UITextView

mag*_*tak 36 padding uitextview ios

正如标题所说我试图向UITextView添加类似填充的行为.在我的导航控制器中按下视图时会生成textview,并出现以下代码:

 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
Run Code Online (Sandbox Code Playgroud)

所以,它一切正常,没关系,但我想在UITextView的所有4个方面添加一些填充,我已经无法用3小时的谷歌搜索看起来相当容易的东西.有什么建议?

Már*_*lho 76

使用iOS7我已经完成了它

[self.textView setTextContainerInset:UIEdgeInsetsMake(0, 12, 0, 12)];
Run Code Online (Sandbox Code Playgroud)

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

  • 在Swift中我使用了self.textView.textContainerInset = UIEdgeInsetsMake(50,50,50,50); (3认同)

小智 54

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

uitextview.textContainerInset =
       UIEdgeInsetsMake(8,5,8,5); // top, left, bottom, right
Run Code Online (Sandbox Code Playgroud)

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


使用这个:

self.textView.contentInset = UIEdgeInsetsMake(5, 5, 5, 5); 
Run Code Online (Sandbox Code Playgroud)


ada*_*dev 25

这在 Swift 5 中有效:

textView.textContainerInset = UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20)
Run Code Online (Sandbox Code Playgroud)


Jam*_*ren 24

编辑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);
}
Run Code Online (Sandbox Code Playgroud)

将它包装在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;
Run Code Online (Sandbox Code Playgroud)

  • 我不明白这是如何被接受为正确的答案.问题是UITextView,而不是UITextField.UITextView没有这样的方法,所以至少答案的第一部分是不正确的.第二部分有一个问题,因为滚动指示器也将以缩进方式显示. (19认同)

Jul*_* B. 11

这对我来说使用swift 2.0:

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


LAD*_*LAD 5

对于Swift 4.2:

textview.contentInset = UIEdgeInsets(top: 2, left: 10, bottom: 2, right: 10)
Run Code Online (Sandbox Code Playgroud)