如何在 ios 中为 UITextView 设置内容插入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19065693/
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
How to set content inset for UITextView in ios
提问by Muruganandham K
I'm having a UITextView and set content inset as
我有一个 UITextView 并将内容插入设置为
[atextView setContentInset:UIEdgeInsetsMake(0, 10, 0, 0)];
[atextView setContentInset:UIEdgeInsetsMake(0, 10, 0, 0)];
This code working in iOS 6.1 and below, but nothing happens in iOS 7.0.
此代码适用于 iOS 6.1 及更低版本,但在 iOS 7.0 中没有任何反应。
回答by Muruganandham K
got answer:
得到答案:
[atextView setTextContainerInset:UIEdgeInsetsMake(7, 7, 0, 0)];
fixed my problem.
解决了我的问题。
And in Swift...
而在斯威夫特...
@IBOutlet var tv:UITextView!
override func awakeFromNib() {
super.awakeFromNib()
tv.textContainerInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
}
UPDATE: another option to do that.
更新:另一种选择。
UIView *spacerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
[msgtext setLeftViewMode:UITextFieldViewModeAlways];
[msgtext setLeftView:spacerView];
回答by Bence Pattogato
An alternative and maybe a little more convenient way to do it, by subclassing the UITextField and add an @IBInspectable inset property.
通过继承 UITextField 并添加 @IBInspectable inset 属性,另一种方法可能更方便一些。
import UIKit
class UITextfieldWithInset: UITextField {
@IBInspectable
var textfieldInset: CGPoint = CGPointMake(0, 0)
override func textRectForBounds(bounds: CGRect) -> CGRect {
return CGRectInset(bounds, textfieldInset.x, textfieldInset.y)
}
override func editingRectForBounds(bounds: CGRect) -> CGRect {
return CGRectInset(bounds, textfieldInset.x, textfieldInset.y)
}
override func placeholderRectForBounds(bounds: CGRect) -> CGRect {
return CGRectInset(bounds, textfieldInset.x, textfieldInset.y)
}
}