Reputation: 111
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.
Upvotes: 0
Views: 786
Reputation: 1627
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:
More info about why you shouldn't overuse the tag property can be found here
Upvotes: 1
Reputation: 184
+(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
Upvotes: 1
Reputation: 185
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];
Upvotes: 1
Reputation: 184
+(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......
Upvotes: 1
Reputation: 5955
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];
Upvotes: 0
Reputation: 185
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.
Upvotes: 1