How to style UITextview to like Rounded Rect text field?

后端 未结 20 2248
深忆病人
深忆病人 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:19

    You may want to check out my library called DCKit.

    You'd be able to make a rounded corner text view (as well as text field/button/plain UIView) from the Interface Builder directly:

    It also has many other useful features, such as text fields with validation, controls with borders, dashed borders, circle and hairline views etc.

    0 讨论(0)
  • 2020-11-28 01:20
    #import <QuartzCore/QuartzCore.h>
    - (void)viewDidLoad{
      UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(50, 220, 200, 100)];
      textView.layer.cornerRadius = 5;
      textView.clipsToBounds = YES;
      [textView.layer setBackgroundColor: [[UIColor whiteColor] CGColor]];
      [textView.layer setBorderColor: [[UIColor grayColor] CGColor]];
      [textView.layer setBorderWidth: 1.0];
      [textView.layer setCornerRadius:8.0f];
      [textView.layer setMasksToBounds:YES];
      [self.view addSubView:textview];
    }
    
    0 讨论(0)
  • 2020-11-28 01:20

    I don't think that it is possible. but you can do UITableView(grouped) with 1 section and 1 empty cell and use it as a container for your UITextView.

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

    There is a great background image that is identical to the UITextView used for sending text messages in iPhone's Messages app. You'll need Adobe Illustrator to get & modify it. iphone ui vector elements

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

    Edit: You have to import

    #import <QuartzCore/QuartzCore.h>
    

    for using corner radius.

    Try this it will work for sure

    UITextView* txtView = [[UITextView alloc] initWithFrame:CGRectMake(50, 50, 300, 100)];
    txtView.layer.cornerRadius = 5.0;
    txtView.clipsToBounds = YES;
    

    As Rob figured it out setting the if you want the border color to be similar as UITextField then you need to change the border width to 2.0 and color to gray by adding the following line

    [textView.layer setBorderColor:[[[UIColor grayColor] colorWithAlphaComponent:0.5] CGColor]]; 
    [textView.layer setBorderWidth:2.0];
    
    0 讨论(0)
  • 2020-11-28 01:23

    Here is my solution:

    - (void)viewDidLoad {
        [super viewDidLoad];
    
    
        self.textView.text = self.messagePlaceholderText;
        self.textView.layer.backgroundColor = [[UIColor whiteColor] CGColor];
        self.textView.layer.borderColor = [[[UIColor grayColor] colorWithAlphaComponent:0.3] CGColor];
        self.textView.layer.borderWidth = 0.5;
        self.textView.layer.cornerRadius = 5.5f;
        self.textView.layer.masksToBounds = YES;
        self.textView.textColor = [[UIColor grayColor] colorWithAlphaComponent:0.4];
    }
    
    - (void)textViewDidBeginEditing:(UITextView *)textView {
        if (textView == self.tvMessage) {
            // Delete placeholder text
            if ([self.textView.text isEqualToString:self.messagePlaceholderText]) {
                self.textView.text = @"";
                self.textView.textColor = [UIColor blackColor];
            }
        }
    }
    
    - (void)textViewDidEndEditing:(UITextView *)textView {
        if (textView == self.tvMessage) {
            // Write placeholder text
            if (self.textView.text.length == 0) {
                self.textView.text = self.messagePlaceholderText;
                self.textView.textColor = [[UIColor grayColor] colorWithAlphaComponent:0.4];
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题