我有一个UITextView,并设置了内容插图:
[atextView setContentInset:UIEdgeInsetsMake(0, 10, 0, 0)];
这段代码在iOS 6.1及以下版本中有效,但在iOS 7.0中没有效果。
我有一个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];
setTextContainerInset
代替setContentInset
。如果您想提供iOS的先前版本,则请检查当前设备系统版本的条件。 - Muruganandham K一种替代而且可能更加方便的方法是,通过子类化UITextField并添加@IBInspectable插入属性。
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)
}
}
UITextField
的不错答案,但问题是关于 UITextView
的。 - dulgan