自定义视图中的UiLabel文本分配

时间:2014-08-13 20:22:09

标签: ios objective-c

我在其他自定义视图(bage)中创建了自定义视图(自定义视图)。然后在主视图中我添加自定义视图作为子视图。之后,我试图通过属性设置文本,但它不起作用。

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 请帮助我在我的bage中获取文字。

1 个答案:

答案 0 :(得分:1)

不要在drawRect:中将标题添加为子视图。您应该在初始化Bage视图时执行此操作。此外,您正在使用drawRect:中给出的矩形,它不一定是视图的边界。因此,在初始化程序中添加标题