Bordered UITextView

后端 未结 14 2093
一个人的身影
一个人的身影 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:20

    I add UIImageView as a subview of the UITextView. This matches the native border on a UITextField, including the gradient from top to bottom:

    enter image description here

    textView.backgroundColor = [UIColor clearColor];
    UIImageView *borderView = [[UIImageView alloc] initWithFrame: CGRectMake(0, 0, textView.frame.size.width, textView.frame.size.height)];
    borderView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    UIImage *textFieldImage = [[UIImage imageNamed:@"TextField.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(15, 8, 15, 8)];
    borderView.image = textFieldImage;
    [textField addSubview: borderView];
    [textField sendSubviewToBack: borderView];
    

    These are the png images I use, and a jpg representation:

    @1x

    @2x

    enter image description here

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

    this is as close as I could from an original UITextField

    func updateBodyTextViewUI() {
        let borderColor = UIColor.init(red: 212/255, green: 212/255, blue: 212/255, alpha: 0.5)
    
        self.bodyTextView.layer.borderColor = borderColor.CGColor
        self.bodyTextView.layer.borderWidth = 0.8
        self.bodyTextView.layer.cornerRadius = 5
    }
    
    0 讨论(0)
  • 2020-12-04 10:28

    for Swift Programming, use this

    tv_comment.layer.borderWidth = 2
    tv_comment.layer.borderColor = UIColor(red: 0.2, green: 0.2, blue: 0.2, alpha: 1).CGColor
    
    0 讨论(0)
  • 2020-12-04 10:28

    The thing that made it work (in addition to following the answers here) is adding the borderStyle attribute:

    #import <QuartzCore/QuartzCore.h>
    ..
    
    phoneTextField.layer.borderWidth = 1.0f;
    phoneTextField.layer.borderColor = [[UIColor blueColor] CGColor];
    phoneTextField.borderStyle = UITextBorderStyleNone;
    
    0 讨论(0)
  • 2020-12-04 10:33

    Add the following for rounded corners:

    self.yourUITextview.layer.cornerRadius = 8; 
    
    0 讨论(0)
  • 2020-12-04 10:34

    In Swift 3, you may use the following two lines:

    myText.layer.borderColor = UIColor.lightGray.cgColor
    
    myText.layer.borderWidth = 1.0
    
    0 讨论(0)
提交回复
热议问题