ios 向 UITextView 添加“填充”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7902990/
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
Add "padding" to a UITextView
提问by magtak
as the title says i am trying to add padding-like behavior to a UITextView. The textview is generated when the view is pushed inside my navigation controller and the following code occurs:
正如标题所说,我正在尝试向 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
so, it all works and it's ok, but i want to add some padding on all 4 sides of the UITextView and i've been unable to do this with 3 hours of googling for something that seems rather easy. Any suggestions?
所以,一切正常,没问题,但我想在 UITextView 的所有 4 个边上添加一些填充,我一直无法通过 3 个小时的谷歌搜索来完成这个看起来很容易的事情。有什么建议?
采纳答案by Jamon Holmgren
EDIT 1/16/2015: This was written in 2012 and is no longer accurate. Use -textContainerInset as mentioned above.
编辑 1/16/2015:这是在 2012 年写的,不再准确。使用 -textContainerInset 如上所述。
Original post:
原帖:
Using contentInset won't actually work. You have two reasonable choices: subclass UITextField and override the textRectForBounds: and editingRectForBounds: methods or create your text field transparently over a UIView and style the UIView.
使用 contentInset 实际上不起作用。您有两个合理的选择:子类化 UITextField 并覆盖 textRectForBounds: 和editingRectForBounds: 方法或在 UIView 上透明地创建文本字段并设置 UIView 样式。
Example of subclassing the UITextField:
子类化 UITextField 的示例:
- (CGRect)textRectForBounds:(CGRect)bounds {
return CGRectInset(bounds, 5, 5);
}
- (CGRect)editingRectForBounds:(CGRect)bounds {
return CGRectInset(bounds, 5, 5);
}
Example of wrapping it in a UIView:
将其包装在 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;
回答by Mário Carvalho
Using iOS7 I have done it just with
使用 iOS7 我已经完成了
[self.textView setTextContainerInset:UIEdgeInsetsMake(0, 12, 0, 12)];
Hope this helps, Mário
希望这会有所帮助,马里奥
回答by Sergej Metelin
This answer is totally wrong. Correct answer is simply:
这个答案是完全错误的。正确答案很简单:
uitextview.textContainerInset =
UIEdgeInsetsMake(8,5,8,5); // top, left, bottom, right
(Those values generally match how a UITextField on the same screen looks.)
(这些值通常与同一屏幕上 UITextField 的外观相匹配。)
Use this one:
使用这个:
self.textView.contentInset = UIEdgeInsetsMake(5, 5, 5, 5);
回答by Julian B.
This worked for me using swift 2.0:
这对我使用 swift 2.0 有用:
textView.textContainerInset = UIEdgeInsetsMake(10, 10, 10, 10)
textView.textContainerInset = UIEdgeInsetsMake(10, 10, 10, 10)
回答by Adam Foot
This is working in Swift 5:
这在 Swift 5 中有效:
textView.textContainerInset = UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20)
回答by LAD
For Swift 4.2:
对于 Swift 4.2:
textview.contentInset = UIEdgeInsets(top: 2, left: 10, bottom: 2, right: 10)
回答by Adam Stoller
For Swift 3 - the answer appears to be similar, but slightly different:
对于 Swift 3 - 答案似乎相似,但略有不同:
textview.contentEdgeInsets = UIEdgeInsets(top: 2, left: 10, bottom: 2, right: 10)
(Actually, I used the above for a UIButtonView, but as it is so closed to the answers that were posted for UITextView I'm thinking [hoping] that it applies for that too)
(实际上,我将上面的内容用于 UIButtonView,但由于它与为 UITextView 发布的答案非常接近,我在想 [希望] 它也适用于此)
回答by Chowdhury Md Rajib Sarwar
Simply create an extension
of UITextView
using Container Edge Insetas following:
简单地使用容器边缘插入创建一个extension
,如下所示:UITextView
extension UITextView {
func leftSpace() {
self.textContainerInset = UIEdgeInsets(top: 4, left: 6, bottom: 4, right: 4)
}
}
and use it like:
并使用它:
let textView = UITextView()
textView. leftSpace()