如何在ios中为UITextView设置内容embedded

我有一个UITextView和设置内容插入为

[atextView setContentInset:UIEdgeInsetsMake(0, 10, 0, 0)];

此代码在iOS 6.1及以下版本中工作,但在iOS 7.0中没有任何反应。

得到的答案:

 [atextView setTextContainerInset:UIEdgeInsetsMake(7, 7, 0, 0)]; 

解决了我的问题。

而在Swift中

 @IBOutlet var tv:UITextView! override func awakeFromNib() { super.awakeFromNib() tv.textContainerInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0); } 

更新:另一个选项来做到这一点。

 UIView *spacerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)]; [msgtext setLeftViewMode:UITextFieldViewModeAlways]; [msgtext setLeftView:spacerView]; 

通过inheritanceUITextField并添加一个@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) } }