美文网首页移动端设计研发
设置UITextView UITextField insets

设置UITextView UITextField insets

作者: Show_Perry | 来源:发表于2018-05-30 18:40 被阅读5次

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

    • UITextView
    UIEdgeInsets insets = textView.contentInset;
    textView.contentInset = UIEdgeInsetsMake(insets.top, 10.0, insets.bottom, insets.right);
    
    • UITextField 没有contentInset属性,不过有几种方法可以实现该效果。

    方法一重写 -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;
    

    相关文章

      网友评论

        本文标题:设置UITextView UITextField insets

        本文链接:https://www.haomeiwen.com/subject/fjyxsftx.html