Tomek
Tomek

Reputation: 642

Custom UIView for displaying a label on many ViewControllers?

I'm new to objective-c and it's a a newbie question. I'm writing an app which has this custom looking label on many views. (edit) The label has two parts: main label that acts as a title and a smaller sublabel that has some short text. (/edit) It seems silly to put labels manually on all of the views so I started thinking about making a custom control that would consist of two labels: mainLabel and subLabel. I'm not sure what approach is better here. Should I make a custom UIView or is there a better way of doing it?

If customizing UIView is the right way, how should I handle setting of the labels? Is creating a protocol delegate that would make a ViewController implement label control initialization methods the right way of doing this?

EDIT: How do I reuse the custom UIView so later when the labels style changes, I don't have to change looks on each of them separatly, but to have some sort o template that's going to be used every time the custom UIView is used

I'll put the answer here for anyone who'd have the same question:

You should use a custom UIView to control setting the labels (check the answer below) together with a .xib file which will define a template for the looks of the label. If you're interested about UIView and .xib files check out this: How to use a xib and a UIView subclass together?

Thanks for your help!

Upvotes: 0

Views: 1103

Answers (2)

nhgrif
nhgrif

Reputation: 62062

You don't need a delegate relationship to simply set a pair of text labels. If you want to do a UIView subclass for a view with two labels, and simply add this view to your view controllers, then in your view subclass, right a method that would be similar to UILabel setText: method. It just needs to take two arguments.

So if mainLabel and subLabel are UILabel properties on your UIView subclass's .m file, you might do something like this:

In the UIView subclass's .h file:

@interface DoubleLabelView

@property (nonatomic,strong) NSString *mainText;
@property (nonatomic,strong) NSString *subText;

- (void)setText:(NSString*)mainText subText:(NSString*)subText;

@end

And then in the .m file:

@interface DoubleLabelView()

@property (nonatomic,strong) UILabel *mainLabel;
@property (nonatomic,strong) UILabel *subLabel;

@end

@implementation DoubleLabelView

- (void)setText:(NSString*)mainText subText:(NSString*)subText {
    self.mainText = mainText;
    self.subText = subText;
}

- (void)setMainText:(NSString*)mainText {
    _mainText = mainText;
    self.mainLabel.text = mainText;
}

- (void)setSubText:(NSString*)subText {
    _subText = subText;
    self.subLabel.text = subText;
}

If the positions of the labels on the custom view are constant, I'd recommend just using a .xib file to set up exactly how you want the custom view to look. And I might add a custom factory method that takes the manText and subText arguments and calls setText:subText: right then and there.

If you're subclassing UIView, the more code specific to the view you can contain within the subclass, the better.


In the same way I've added methods to the UIView to allow you to change the mainText, subText, or both at the same time in a single method call, you can continue to add methods such as this to change properties of your UIView.

If you want to reposition your labels, consider adding a pair of CGPoint properties:

@property (nonatomic,assign) CGPoint mainLabelPosition;
@property (nonatomic,assign) CGPoint subLabelPosition;

- (void)setPosition:(CGPoint)mainPosition subPosition:(CGPoint)subPosition;

And write methods for these similar to the ones I showed you for changing the text.

Upvotes: 2

santhu
santhu

Reputation: 4792

Create a subclass of UIView as below,

CustomView.h file

@interface CustomView : UIView
@property UILabel *mainLabel;
@property UILabel *subLabel;

-(void)showMainLabelWithText:(NSString *)text;
-(void)showSubLabel:(NSString *)text;

@end

CustomView.m file

@implementation CustomView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}
-(void)showMainLabelWithText:(NSString *)text{
    self.mainLabel =[[UILabel alloc] init];
    self.mainLabel.text =text;
    [self addSubview:self.mainLabel];
}
-(void)showSubLabel:(NSString *)text{
    self.subLabel =[[UILabel alloc] init];
    self.subLabel.text =text;
    [self addSubview:self.subLabel];
}

Upvotes: 1

Related Questions