简体   繁体   English

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

[英]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: Bage.h:

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

Bage.m: 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 CustomView.h

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


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

CustomView.m 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 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

在此处输入图片说明 Please help me to get text in my bage. 请帮助我在我的行李中输入文字。

Do not add the title as a subview in drawRect:. 不要在drawRect:中将标题添加为子视图。 You should do this when the Bage view is initialized. 初始化Bage视图时,应执行此操作。 Also, you are using the rect given to you in drawRect:, which will not always be the bounds of the view. 另外,您使用的是drawRect:中赋予的rect,它并不总是视图的边界。 So, add the title in your initializer 因此,在您的初始值设定项中添加标题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM