How to style UITextview to like Rounded Rect text field?

后端 未结 20 2249
深忆病人
深忆病人 2020-11-28 00:36

I am using a text view as a comment composer.

In the properties inspector I can\'t find anything like a border style property so that I can make use a rounded rect,

相关标签:
20条回答
  • 2020-11-28 01:25

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

    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 images I use:

    enter image description here enter image description here

    0 讨论(0)
  • 2020-11-28 01:25

    If you want to keep your controller code clean, you can subclass UITextView like below, and change the class name in the Interface Builder.

    RoundTextView.h

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

    RoundTextView.m

    #import "RoundTextView.h"
    #import <QuartzCore/QuartzCore.h>
    @implementation RoundTextView
    -(id) initWithCoder:(NSCoder *)aDecoder {
        if (self = [super initWithCoder:aDecoder]) {
            [self.layer setBorderColor:[[[UIColor grayColor] colorWithAlphaComponent:0.333] CGColor]];
            [self.layer setBorderWidth:1.0];
            self.layer.cornerRadius = 5;
            self.clipsToBounds = YES;
        }
        return self;
    }
    @end
    
    0 讨论(0)
  • 2020-11-28 01:30

    For the best effect you have to use a custom (stretchable) background image. This is also how the UITextField's rounded border is drawn.

    0 讨论(0)
  • 2020-11-28 01:31

    Swift 3 Version

    After setting up the text view in interface builder.

    @IBOutlet weak var textView: UITextView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        textView.layer.cornerRadius = 5     
        textView.layer.borderColor = UIColor.gray.withAlphaComponent(0.5).cgColor
        textView.layer.borderWidth = 0.5  
        textView.clipsToBounds = true
    }
    

    Swift 2.2 Version

    @IBOutlet weak var textView: UITextView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        textView.layer.cornerRadius = 5     
        textView.layer.borderColor = UIColor.grayColor().colorWithAlphaComponent(0.5).CGColor
        textView.layer.borderWidth = 0.5  
        textView.clipsToBounds = true
    }
    
    0 讨论(0)
  • 2020-11-28 01:32

    You can create a Text Field that doesn't accept any events on top of a Text View like this:

    CGRect frameRect = descriptionTextField.frame;
    frameRect.size.height = 50;
    descriptionTextField.frame = frameRect;
    descriptionTextView.frame = frameRect;
    descriptionTextField.backgroundColor = [UIColor clearColor];
    descriptionTextField.enabled = NO;
    descriptionTextView.layer.cornerRadius = 5;
    descriptionTextView.clipsToBounds = YES;
    
    0 讨论(0)
  • 2020-11-28 01:34

    I know there are already a lot of answers to this one, but I didn't really find any of them sufficient (at least in Swift). I wanted a solution that provided the same exact border as a UITextField (not an approximated one that looks sort of like it looks right now, but one that looks exactly like it and will always look exactly like it). I needed to use a UITextField to back the UITextView for the background, but didn't want to have to create that separately every time.

    The solution below is a UITextView that supplies it's own UITextField for the border. This is a trimmed down version of my full solution (which adds "placeholder" support to the UITextView in a similar way) and was posted here: https://stackoverflow.com/a/36561236/1227119

    // This class implements a UITextView that has a UITextField behind it, where the 
    // UITextField provides the border.
    //
    class TextView : UITextView, UITextViewDelegate
    {
        var textField = TextField();
    
        required init?(coder: NSCoder)
        {
            fatalError("This class doesn't support NSCoding.")
        }
    
        override init(frame: CGRect, textContainer: NSTextContainer?)
        {
            super.init(frame: frame, textContainer: textContainer);
    
            self.delegate = self;
    
            // Create a background TextField with clear (invisible) text and disabled
            self.textField.borderStyle = UITextBorderStyle.RoundedRect;
            self.textField.textColor = UIColor.clearColor();
            self.textField.userInteractionEnabled = false;
    
            self.addSubview(textField);
            self.sendSubviewToBack(textField);
        }
    
        convenience init()
        {
            self.init(frame: CGRectZero, textContainer: nil)
        }
    
        override func layoutSubviews()
        {
            super.layoutSubviews()
            // Do not scroll the background textView
            self.textField.frame = CGRectMake(0, self.contentOffset.y, self.frame.width, self.frame.height);
        }
    
        // UITextViewDelegate - Note: If you replace delegate, your delegate must call this
        func scrollViewDidScroll(scrollView: UIScrollView)
        {
            // Do not scroll the background textView
            self.textField.frame = CGRectMake(0, self.contentOffset.y, self.frame.width, self.frame.height);
        }        
    }
    
    0 讨论(0)
提交回复
热议问题