UserNil
UserNil

Reputation: 129

UITextView to Mimic UITextField [for basic round corner text field]

I want to have UITextField with multiple lines, after a quick google on this issue I found that I should use TextView so I did switch my code to use UITextView when I want multiple lines. My View still have other one line textField that I want to keep.

To make my TextView looks like TextField, I had to add code to set border and radius, but they look a little bit different on iOS7. Does anyone know:

If there is away to keep using textfield for multiline text, I am all ears and i switch to use it.

Best Regards,

enter image description here

Upvotes: 5

Views: 1863

Answers (4)

Mohammad Faisal
Mohammad Faisal

Reputation: 2006

This is the closest I got with enabled UITextView

[yourTextView.layer setBorderColor:[[[UIColor lightGrayColor] colorWithAlphaComponent:0.2] CGColor]];
[yourTextView.layer setBorderWidth:2.0];
yourTextView.layer.cornerRadius = 5;
yourTextView.clipsToBounds = YES;
yourTextView.textColor = [UIColor lightGrayColor];

Upvotes: 0

Hampotato
Hampotato

Reputation: 267

I use this:

textView.layer.borderColor = [[UIColor colorWithRed:215.0 / 255.0 green:215.0 / 255.0 blue:215.0 / 255.0 alpha:1] CGColor];
textView.layer.borderWidth = 0.6f;
textView.layer.cornerRadius = 6.0f;

little differences in the params make it looks more like UITextField(I hope).

Upvotes: 7

Javi Campaña
Javi Campaña

Reputation: 1481

I use this:

#import <QuartzCore/QuartzCore.h>

-(void) textViewLikeTextField:(UITextView*)textView
{
    [textView.layer setBorderColor:[[UIColor colorWithRed:212.0/255.0
                                                                       green:212.0/255.0
                                                                        blue:212.0/255.0
                                                                       alpha:1] CGColor]];
    [textView.layer setBorderWidth:1.0f];
    [textView.layer setCornerRadius:7.0f];
    [textView.layer setMasksToBounds:YES];
}

and get a good resut.

Upvotes: 3

PatrickV
PatrickV

Reputation: 1090

I have a small subclass of UITextView that gives in iOS 7 the same look as the UITextField

The interface is empty:

@interface MyTextView : UITextView
@end

The implementation overwrites the initialization and the setter of the 'editable' property:

@implementation MyTextView

//================================================================================

- (id) initWithFrame: (CGRect) frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        [self commonInit];
    }
    return self;
}

//================================================================================

- (id) initWithCoder: (NSCoder *) aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self)
    {
        [self commonInit];
    }
    return self;
}

//================================================================================

- (void) commonInit
{
    self.layer.borderWidth  = 1.0f;
    self.layer.borderColor  = [[UIColor colorWithRed:232.0/255.0
                       green:232.0/255.0 blue:232.0/255.0 alpha:1] CGColor];
    self.layer.cornerRadius = 6;
}

//================================================================================

- (void) setEditable: (BOOL) editable
{
    [super setEditable:editable];

    if (editable)
    {
        self.backgroundColor = [UIColor whiteColor];
    }
    else
    {
        self.backgroundColor = [UIColor colorWithRed:250.0/255.0
                                green:250.0/255.0 blue:250.0/255.0 alpha:1];
    }
}

//================================================================================

@end

Upvotes: 0

Related Questions