Add “padding” to a UITextView

前端 未结 8 522
渐次进展
渐次进展 2021-01-31 14:19

as the title says i am trying to add padding-like behavior to a UITextView. The textview is generated when the view is pushed inside my navigation controller and the following c

相关标签:
8条回答
  • 2021-01-31 14:55

    Using iOS7 I have done it just with

    [self.textView setTextContainerInset:UIEdgeInsetsMake(0, 12, 0, 12)];
    

    Hope this helps, Mário

    0 讨论(0)
  • 2021-01-31 15:04

    EDIT 1/16/2015: This was written in 2012 and is no longer accurate. Use -textContainerInset as mentioned above.

    Original post:

    Using contentInset won't actually work. You have two reasonable choices: subclass UITextField and override the textRectForBounds: and editingRectForBounds: methods or create your text field transparently over a UIView and style the UIView.

    Example of subclassing the UITextField:

    - (CGRect)textRectForBounds:(CGRect)bounds {
         return CGRectInset(bounds, 5, 5);
    }
    
    - (CGRect)editingRectForBounds:(CGRect)bounds {
         return CGRectInset(bounds, 5, 5);
    }
    

    Example of wrapping it in a UIView:

    UIView *wrapView = [[UIView alloc] initWithFrame: CGRectMake(10, 10, 200, 30)];
    [wrapView addSubview:textView];
    wrapView.layer.borderColor = [UIColor darkGrayColor].CGColor;
    wrapView.layer.borderWidth = 2.0;
    wrapView.layer.cornerRadius = 5.0;
    
    0 讨论(0)
提交回复
热议问题