Bordered UITextView

后端 未结 14 2095
一个人的身影
一个人的身影 2020-12-04 10:11

I want to have a thin gray border around a UITextView. I have gone through the Apple documentation but couldn\'t find any property there. Please help.

相关标签:
14条回答
  • 2020-12-04 10:35
    #import <QuartzCore/QuartzCore.h>
    
    ....
    
    // typically inside of the -(void) viewDidLoad method
    self.yourUITextView.layer.borderWidth = 5.0f;
    self.yourUITextView.layer.borderColor = [[UIColor grayColor] CGColor];
    
    0 讨论(0)
  • 2020-12-04 10:39

    Works great, but the color should be a CGColor, not UIColor:

    view.layer.borderWidth = 5.0f;
    view.layer.borderColor = [[UIColor grayColor] CGColor];
    
    0 讨论(0)
  • 2020-12-04 10:40

    you can add border to UITextView from the Storyboard - Identity Inspector - User Defined Runtime Attribute

    0 讨论(0)
  • 2020-12-04 10:40

    As of iOS 8 and Xcode 6, I now find the best solution is to subclass UITextView and mark the subclass as an IB_DESIGNABLE, which will allow you to view the border in storyboard.

    Header:

    #import <UIKit/UIKit.h>
    
    IB_DESIGNABLE
    
    @interface BorderTextView : UITextView
    
    @end
    

    Implementation:

    #import "BorderTextView.h"
    
    @implementation BorderTextView
    
    - (void)drawRect:(CGRect)rect
    {
        self.layer.borderWidth = 1.0;
        self.layer.borderColor = [UIColor blackColor].CGColor;
        self.layer.cornerRadius = 5.0f;
    }
    
    @end
    

    Then just drag out your UITextView in storyboard and set its class to BorderTextView

    0 讨论(0)
  • 2020-12-04 10:41

    Just a small addition. If you make the border a bit wider, it will interfere with the left and right side of text. To avoid that, I added the following line:

    self.someTextView.textContainerInset = UIEdgeInsetsMake(8.0, 8.0, 8.0, 8.0);
    
    0 讨论(0)
  • 2020-12-04 10:47

    Here's the code I used, to add a border around my TextView control named "tbComments" :

    self.tbComments.layer.borderColor = [[UIColor grayColor] CGColor];
    self.tbComments.layer.borderWidth = 1.0;
    self.tbComments.layer.cornerRadius = 8;
    

    And here's what it looks like:

    enter image description here

    Easy peasy.

    0 讨论(0)
提交回复
热议问题