v_semenov
v_semenov

Reputation: 111

UiLabel text assignment in custom view

I have created a custom view (custom view) inside other custom view(bage). Then in main view I add custom view as a subview. After that I am trying to set text via property and it does not work.

Bage.h:

#import <UIKit/UIKit.h>
@interface Bage : UIView
@property (nonatomic,strong) UILabel *title;
@end

Bage.m:

#import "Bage.h"

@implementation Bage
@synthesize title;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:CGRectMake(0, 0, 45, 25)];
    if (self) {
        // Initialization code
    }

    return self;
}


- (void)drawRect:(CGRect)rect
{

        //// General Declarations
        CGContextRef context = UIGraphicsGetCurrentContext();


        //// Shadow Declarations
        UIColor* shadow = UIColor.blackColor;
        CGSize shadowOffset = CGSizeMake(3.1, 3.1);
        CGFloat shadowBlurRadius = 3;

        //// Variable Declarations
        CGFloat buttonWidth = rect.size.width - 2 - 4;
        CGFloat buttonHeigh = rect.size.height - 2 - 4;

        //// Frames
//        CGRect frame = CGRectMake(frSize.origin.x, frSize.origin.y, frSize.size.width, frSize.size.height);


        //// Rectangle Drawing
        UIBezierPath* rectanglePath = [UIBezierPath bezierPathWithRoundedRect: CGRectMake(1, 1, buttonWidth, buttonHeigh) cornerRadius: 8];
        CGContextSaveGState(context);
        CGContextSetShadowWithColor(context, shadowOffset, shadowBlurRadius, [shadow CGColor]);
        [UIColor.whiteColor setFill];
        [rectanglePath fill];
        CGContextRestoreGState(context);

        [[UIColor greenColor] setStroke];
        rectanglePath.lineWidth = 1;
        [rectanglePath stroke];

    title = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(rect) + 1, CGRectGetMinY(rect) + 1, CGRectGetWidth(rect) - 6, CGRectGetHeight(rect) - 6)];
    title.textColor = [UIColor blackColor];
    title.textAlignment = NSTextAlignmentCenter;
    [self addSubview:title];

}
@end

CustomView.h

#import <UIKit/UIKit.h>
#import "Bage.h"


@interface CustomView : UIView
@property (nonatomic,strong) Bage *myBage;
@end

CustomView.m

#import "CustomView.h"

@implementation CustomView
@synthesize myBage;


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        myBage = [[Bage alloc] init];
        self.backgroundColor = [UIColor greenColor];
        myBage.title.text = @"Class text";

        myBage.center = self.center;
        [self addSubview:myBage];

    }
    return self;
}

@end

ViewController.m

#import "ViewController.h"
#import "CustomView.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor blueColor];

    CustomView *cView = [[CustomView alloc] initWithFrame:CGRectMake(30, 30, 200, 200)];
    [self.view addSubview:cView];
    cView.myBage.backgroundColor = [UIColor clearColor];
    cView.myBage.title.text = @"Text";
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end

enter image description here Please help me to get text in my bage.

Upvotes: 1

Views: 63

Answers (1)

Daniel Shteremberg
Daniel Shteremberg

Reputation: 214

Do not add the title as a subview in drawRect:. You should do this when the Bage view is initialized. Also, you are using the rect given to you in drawRect:, which will not always be the bounds of the view. So, add the title in your initializer

Upvotes: 1

Related Questions