Use UILabel Class's method differently for Two Label

In my application i have a scrollview that shows dynamically generated Labels (some are having 70px height and some are having 50px height).

i need to use - (void)drawRect:(CGRect)rect method for both type of label.

for that i created one UILabel controller class and in it there is a method

- (void)drawRect:(CGRect)rect
{
// Drawing code
NSLog(@"draw rect in uilabel class");
UIEdgeInsets insets = {0, 0, -14, 0};
return [super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)]; 
}

i need to set one label's insets = {0, 0, -14, 0}; and another label's {0, 0, -22, 0}; but in a random manner (means for label 1,2,4,6,9 the bottom will be -14 and for 3,5,7,8,10,11 bottom will be -22).

yes I can do this from putting Two UILabel class and getting object from that.

but i need to use this class's methods for two types of labels.

tagging is not useful, i used it.

I would like to do some thing like this

- (void)drawRect:(CGRect)rect
{
// Drawing code
  if(for label one){
    UIEdgeInsets insets = {0, 0, -14, 0};
    return [super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)];
  }
  else if(for label Two){
    UIEdgeInsets insets = {0, 0, -22, 0};
    return [super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)];
  }
} 

any idea, code, link, tutorial will be great Help...

------------ EDIT: -----------------------------------

#import <UIKit/UIKit.h>

@interface LabelInListViewClass : UILabel

@end

and in .m file

#import "LabelInListViewClass.h"

@implementation LabelInListViewClass

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
    // Initialization code

    NSLog(@"in Label Class");

}
return self;
}
- (void)drawRect:(CGRect)rect
{
// Drawing code
UIEdgeInsets insets = {0, 0, -14, 0};
return [super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)];

}

and in ViewController .m file

label1 = [[LabelInListViewClass alloc] initWithFrame:CGRectMake(16,varForHeight,320, 50)];

label2 = [[LabelInListViewClass alloc] initWithFrame:CGRectMake(16,varForHeight,320, 70)];

Answers


Make the edge insets value a property of your label subclass, and set the insets when you create the labels. Use the property when drawing instead of creating a local value.


Need Your Help

read.csv parameter stringsAsFactors not working?

r csv r-factor file-import

Based on the documentation read.csv, the parameter stringsAsFactors, when set, should cause quoted data values to be interpreted as factors. Consider the following data file, which we will call tes...

PriorityBlockingQueue: is it realy blocking

java multithreading collections concurrency blocking

I read about PriorityBlockingQueue, and seems I've got some delusions about it. You can see four statements below. I hope if you disagree with them you'll correct me.