0
votes

I want to set the custom view's frame as my subview's frame, which is a UILabel. but when I use self.frame the UILabel shows empty text. I need to hardcode the parameters in CGRectMake in order to see my UILabel. How do I set the UILabel's frame to be my custom view's frame?

- (id)initWithFrame:(CGRect)frame withText:(NSString *)text
{
    self = [super initWithFrame:frame];

    if (self) {
        self.backgroundColor = [UIColor grayColor];
        self.layer.cornerRadius = 3;
        self.layer.borderWidth = 1;
        self.layer.borderColor = [UIColor blackColor].CGColor;
        _text = text;
    }

    return self;
}

- (void)layoutSubviews
{
    // This works
    // UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 40)];
    // This doesn't work
    UILabel *label = [[UILabel alloc] initWithFrame:self.frame];
    label.text = @"";
    if (![self.text isEqualToString:@"?"]) {
        label.text = self.text;
        label.textAlignment = NSTextAlignmentCenter;
    }

    [self addSubview:label];
}
4

4 Answers

2
votes

layoutSubviews will be invoked many times, so you should not addSubview like this. Better style is to set a property of UILabel, like your text property:

@property (nonatomic, strong) UILabel * label;

then change your layoutSubviews code:

- (void)layoutSubviews
{
    if(!self.label){
         self.label = [[UILabel alloc] initWithFrame:self.frame]; //Use ARC
         self.label.textAlignment = NSTextAlignmentCenter;
         [self addSubview:self.label];
    }

    self.label.frame = self.bounds;
    if (![self.text isEqualToString:@"?"]) {
        self.label.text = self.text;
    }
}
0
votes

Try with following code.

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 70, 40)]; // set frame as you want
label.backgroundColor = [UIColor yellowColor]; // set color as you want
label.text = @"Hellow";
[yourParentView addSubview:label];
0
votes

use this code

- (id)initWithFrame:(CGRect)frame withText:(NSString *)text
{
    self = [super initWithFrame:frame];

    if (self) {
        self.backgroundColor = [UIColor grayColor];
        self.layer.cornerRadius = 3;
        self.layer.borderWidth = 1;
        self.layer.borderColor = [UIColor blackColor].CGColor;
        _text = text;

     [self layoutSubviews:frame];
    }

    return self;
}

- (void)layoutSubviews:(CGRect)myFrame
{
    // This works
    // UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 40)];
    // This doesn't work
    UILabel *label = [[UILabel alloc] initWithFrame:myFrame];
    label.text = @"";
    if (![self.text isEqualToString:@"?"]) {
        label.text = self.text;
        label.textAlignment = NSTextAlignmentCenter;
    }

    [self addSubview:label];
}
0
votes

You do not want to create views inside the layoutSubviews method. That method can be called multiple times and you'll create each view multiple times (having them overlap each other).

Instead create the views you need in the initWithFrame method.

For the answer to your actual question, you probably need to set the frame of the new label to the bounds of the custom view. The bounds will have an origin of 0,0 and so it will work no mater where the custom view is placed in it's superview. If you use the frame, the label will be offset by however much the custom view is offset.

You can probably completely remove your layoutSubviews method as that's not the place to set the label text either. Then add this:

- (instancetype)initWithFrame:(CGRect)frame withText:(NSString *)text
{
    self = [super initWithFrame:frame];
    if (!self) return nil;

    self.backgroundColor = [UIColor grayColor];
    self.layer.cornerRadius = 3;
    self.layer.borderWidth = 1;
    self.layer.borderColor = [UIColor blackColor].CGColor;
    _text = text;

    UILabel *label = [[UILabel alloc] initWithFrame:self.bounds];
    label.text = @"";
    if (![self.text isEqualToString:@"?"]) {
        label.text = self.text;
        label.textAlignment = NSTextAlignmentCenter;
    }

    [self addSubview:label];

    return self;
}

If you want the label to resize when the custom view resizes, you could do that inside the layoutSubviews by setting the label.frame = self.bounds;, but you'd be better off using autoresizing masks or auto layout.