移动端设计研发

设置UITextView UITextField insets

2018-05-30  本文已影响5人  Show_Perry

在实际开发中如果UITextView UITextField靠屏幕边界,会影响美观和用户交互体验,所以常常需要设置内容的insets,虽然老B都有所了解如何设置,不过今天在群里有人问及,所以总结了下。

UIEdgeInsets insets = textView.contentInset;
textView.contentInset = UIEdgeInsetsMake(insets.top, 10.0, insets.bottom, insets.right);

方法一重写 -textRectForBounds-editingRectForBounds

// placeholder position
- (CGRect)textRectForBounds:(CGRect)bounds {
     return CGRectInset(bounds, 10.0, 10.0);
}

// text position
- (CGRect)editingRectForBounds:(CGRect)bounds {
     return CGRectInset(bounds, 10.0, 10.0);
}

方法二

textField.layer.sublayerTransform = CATransform3DMakeTranslation(10, 0, 0);

方法三

UItextField *textField = [[UITextField alloc] initWithFrame:...];
UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, textField.frame.size.height)];
leftView.backgroundColor = textField.backgroundColor;
textField.leftView = leftView;
textField.leftViewMode = UITextFieldViewModeAlways;
上一篇下一篇

猜你喜欢

热点阅读