Question

I can't intercept the init function that's getting called when it's getting created inside of the xib file.

I want to add borderline to it when it gets created so that I won't need to add it manually.

.h:

#import <UIKit/UIKit.h>

@interface UITextView (FITAddBorderline)
- (id) init;
- (id) initWithFrame:(CGRect)frame;
@end

.m:

#import "UITextView+FITAddBorderline.h"

@implementation UITextView (FITAddBorderline)

- (id) init
{
    self = [super init];
    if (self) {
        [self addBorderline];
    }
    return self;
}

- (id) initWithFrame:(CGRect)frame {

    self = [super init];
    if (self) {
        self.frame = frame;
        [self addBorderline];
    }
    return self;
}

- (void) addBorderline {

    //To make the border look very close to a UITextField
    [self.layer setBorderColor:[[[UIColor grayColor] colorWithAlphaComponent:0.5] CGColor]];
    [self.layer setBorderWidth:2.0];

    //The rounded corner part, where you specify your view's corner radius:
    self.layer.cornerRadius = 5;
    self.clipsToBounds = YES;
}

@end
Was it helpful?

Solution

Views that come from NIBs are initialized with initWithCoder:

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

    if (self)
    {
        [self addBorderline];
    }

    return self;
}

As a side note, I would recommend changing what you are doing and use a subclass instead of a category. You can get yourself into some trouble overriding methods in a category. See more info here.

OTHER TIPS

You just need to implement the awakeFromNib method:

-(void)awakeFromNib

{
    [super awakeFromNib];
    [self addBorderline];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top