How to style UITextview to like Rounded Rect text field?

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

    this code worked well for me:

        [yourTextView.layer setBackgroundColor: [[UIColor whiteColor] CGColor]];
        [yourTextView.layer setBorderColor: [[UIColor grayColor] CGColor]];
        [yourTextView.layer setBorderWidth: 1.0];
        [yourTextView.layer setCornerRadius:8.0f];
        [yourTextView.layer setMasksToBounds:YES];
    
    0 讨论(0)
  • 2020-11-28 01:11

    One way I found to do it without programming is to make the textfield background transparent, then place a Round Rect Button behind it. Make sure to change the button settings to disable it and uncheck the Disable adjusts image checkbox.

    Tried the Quartzcore code and found it caused lag on my old 3G (I use for testing). Not a big issue but if you want to be as inclusive as possible for different ios and hardware I recommend Andrew_L's answer above - or make your own images and apply accordingly.

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

    One solution is to add a UITextField below the UITextView, make the UITextView background transparent and disable any user interaction on the UITextField. Then in code change the UITextField frame with something like that

    self.textField.frame = CGRectInset(self.textView.frame, 0, -2);
    

    You will have exactly the same look as a text field.

    And as suggested by Jon, you should put this piece of code inside [UIViewController viewDidLayoutSubviews] on iOS 5.0+.

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

    How about just:

    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 20, 280, 32)];
    textField.borderStyle = UITextBorderStyleRoundedRect;
    [self addSubview:textField];
    
    0 讨论(0)
  • 2020-11-28 01:17

    There is no implicit style that you have to choose, it involves writing a bit of code using the QuartzCore framework:

    //first, you
    #import <QuartzCore/QuartzCore.h>
    
    //.....
    
    //Here I add a UITextView in code, it will work if it's added in IB too
    UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(50, 220, 200, 100)];
    
    //To make the border look very close to a UITextField
    [textView.layer setBorderColor:[[[UIColor grayColor] colorWithAlphaComponent:0.5] CGColor]];
    [textView.layer setBorderWidth:2.0];
    
    //The rounded corner part, where you specify your view's corner radius:
    textView.layer.cornerRadius = 5;
    textView.clipsToBounds = YES;
    

    It only works on OS 3.0 and above, but I guess now it's the de facto platform anyway.

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

    One way I found to do it without programming is to make the textfield background transparent, then place a Round Rect Button behind it. Make sure to change the button settings to disable it and uncheck the Disable adjusts image checkbox.

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