0

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!

Community
  • 1
  • 1
Tomek
  • 607
  • 9
  • 16
  • More detail is needed. What's so custom about the label? – nhgrif Jan 26 '14 at 17:13
  • Nothing really that custom. It always has two normal labels with a certain position and looks. I'm just not sure if it's not going to change globally. Changing each view by hand would be a pain... – Tomek Jan 26 '14 at 17:16
  • Your question is still far from clear. Besides the text on the label, what, specifically do you want to change about these labels? – nhgrif Jan 26 '14 at 17:26
  • The positioning of the labels on each of the custom UIViews, for example – Tomek Jan 26 '14 at 17:29
  • Ok, I think the .xib file is what I needed. Thanks nhgrif – Tomek Jan 26 '14 at 17:31

2 Answers2

2

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.

nhgrif
  • 61,578
  • 25
  • 134
  • 173
  • Ok, I get it, but how do I put this thing on all viewcontrollers in such way that I don't have to change each of them later when style changes? – Tomek Jan 26 '14 at 17:21
  • Change what? Again, as I commented to the original question, I need a whole lot more detail. You're being extremely vague. – nhgrif Jan 26 '14 at 17:21
  • I asked about this in a comment to your comment :) I'll edit the question – Tomek Jan 26 '14 at 17:24
  • You've never specified exactly what you want to change. A label is a UI element that is only on a single view. It shouldn't be changed by anything other than the view controller controlling the view it is on. There should be nothing global about a UI element. – nhgrif Jan 26 '14 at 17:24
1

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];
}
santhu
  • 4,796
  • 1
  • 21
  • 29
  • This will create two UILabels to use, that's cool, but what about layout? How can I automate this? – Tomek Jan 26 '14 at 17:27
  • mainLabel and subLabel are public properties, just set their frame like instance.mainLabel.frame =someFrame; – santhu Jan 26 '14 at 17:50