How to set content inset for UITextView in ios

前端 未结 2 1975
广开言路
广开言路 2020-12-15 15:21

I\'m having a UITextView and set content inset as

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

This code working in iOS 6.1 and

2条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-15 16:21

    An alternative and maybe a little more convenient way to do it, by subclassing the UITextField and add an @IBInspectable inset property.

    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)
        }
    
    }
    

提交回复
热议问题