简体   繁体   中英

custom UILabel class iOS

In my iOS application I want UILabel which can be used on most of the screen with dynamic text.

So I have written method in my Utility class, which creates UILabel with associated UIView and I'm also setting text to UILabel which I want. And I use this Utility class method to create UILabel where I want

But, the problem which I'm facing is I am not able hide or remove this UILabel from view.

So please give me your valuable suggestions...

Thanks in advance.....

+(void)drawLabel:(UIView *)view message:(NSString *)message
{
    UILabel *messageLabel = [[UILabel alloc]init];
    messageLabel.frame = CGRectMake(0, 0, 320, 30);
    [messageLabel setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin];
    messageLabel.text = message;
    [messageLabel setCenter:view.center];
    [messageLabel setTextAlignment:NSTextAlignmentCenter];
    [view addSubview:messageLabel];
}

Here is the method which I have written in Utility class for creating UILabel.

[CACCustomMessageLabel drawLabel:self.view message:@"MY LABEL2"];

And I'm using this like as above.

You can create method like this in your utility class.

+(void)drawLabel:(UIView *)view message:(NSString *)message isHide:(BOOL)isHide
{
    UILabel *messageLabel = [[UILabel alloc]init];
    messageLabel.frame = CGRectMake(0, 0, 320, 30);
    [messageLabel setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin];
    messageLabel.text = message;
    [messageLabel setCenter:view.center];
    [messageLabel setTextAlignment:NSTextAlignmentCenter];
    [view addSubview:messageLabel];
    messageLabel.hidden = isHide;
}

Here is the method which I have written in Utility class for creating UILabel.Here is the isHide parameter for hide and unhide label from your view.

[uilabelUtility drawLabel:self.view message:@"Jaydip Godhani" isHide:YES];

Here YES is to hide label from you view.

+(void)drawLabel:(UIView *)view message:(NSString *)message isHide:(BOOL)isHide
{
    UILabel *messageLabel = [[UILabel alloc]init];
    messageLabel.frame = CGRectMake(0, 0, 320, 30);
    [messageLabel setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin];
    messageLabel.text = message;
    [messageLabel setCenter:view.center];
    [messageLabel setTextAlignment:NSTextAlignmentCenter];
    [view addSubview:messageLabel];
    messageLabel.hidden = isHide;
}

working perfectly......

Create using tag of the UILabel.

+(void)drawLabel:(UIView *)view message:(NSString *)message tag:(int)tagValue
{
    UILabel *messageLabel = [[UILabel alloc]init];
    messageLabel.tag = tagValue;
    messageLabel.frame = CGRectMake(0, 0, 320, 30);
    [messageLabel setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin];
    messageLabel.text = message;
    [messageLabel setCenter:view.center];
    [messageLabel setTextAlignment:NSTextAlignmentCenter];
    [view addSubview:messageLabel];
}

And Call like this.

[CACCustomMessageLabel drawLabel:self.view message:@"MY LABEL2" tag:100];

In the View get in the UILabel using given tag of the UIlabel.

UILabel *yourLbl = (UILabel)[self.view viewWithTag:123];
yourLbl.hidden = YES;
[yourLbl removeFromSuperview];

You can't hide or remove the label later because you don't have a reference to it. Your drawLabel method should return an instance of the Label it creates, save it a property and later when you want to modify it you can access it through the property.

In your ViewController you have a label property:

@property (nonatomic, strong) UILabel *label

In your custom class you can have a factory method that creates an UILabel instance. Although I would change the name something more clear. You shouldn't add the label to the view in this method that is the job of the viewController:

+(UILabel *)labelWithMessage:(NSString *)message
{
   UILabel *messageLabel = [[UILabel alloc]init];
   messageLabel.frame = CGRectMake(0, 0, 320, 30);
   [messageLabel setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin];
   messageLabel.text = message;
   [messageLabel setTextAlignment:NSTextAlignmentCenter];
   return messageLabel;
}

Call your method like this in your ViewController:

self.label = [CACCustomMessageLabel drawLabel:self.view message:@"MY LABEL2"];
[self.view addSubview:self.label]
self.label.center = self.view.center;

Now you have a reference to your label in your ViewController, and you can hide or remove it from it's superview:

[self.label removeFromSuperview]

Additional info

Please try to avoid using UIView tags as much as possible. Follow these rules:

  1. Don't store data in a view's tag. Prefer subclassing and Objective-C associated references to associate additional information with a view.
  2. Prefer real properties and XIB outlets over tags for getting references to subviews.
  3. If you do use tags, do not use magic numbers. Use named constants.

More info about why you shouldn't overuse the tag property can be found here

+(void)drawLabel:(UIView *)view message:(NSString *)message tag:(int)tagValue
{
    UILabel *messageLabel = [[UILabel alloc]init];
    messageLabel.tag = tagValue;
    messageLabel.frame = CGRectMake(0, 0, 320, 30);
    [messageLabel setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin];
    messageLabel.text = message;
    [messageLabel setCenter:view.center];
    [messageLabel setTextAlignment:NSTextAlignmentCenter];
    [view addSubview:messageLabel];
}

working Perfect..Try This

Just set the tag value for your label

+(void)drawLabel:(UIView *)view message:(NSString *)message tag:(int)tagValue
{
    UILabel *messageLabel = [[UILabel alloc]init];
    messageLabel.tag = tagValue;
    messageLabel.frame = CGRectMake(0, 0, 320, 30);
    [messageLabel setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin];
    messageLabel.text = message;
    [messageLabel setCenter:view.center];
    [messageLabel setTextAlignment:NSTextAlignmentCenter];
    [view addSubview:messageLabel];
}

create label like

[CACCustomMessageLabel drawLabel:self.view message:@"MY LABEL2" tag:123];

then access it where ever you want inside the VC and do hide or remove as follows,

UILabel *label = (UILabel)[self.view viewWithTag:123];
label.hidden = YES;
[label removeFromSuperview];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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