Piscean
Piscean

Reputation: 3079

How to write two or more texts in tableview row and position them differently?

I have a UITableView and i want to right a text in a row which should be right aligned and another text which should be left aligned. And both of those texts should start with some space from border of the row. I don't want them to start with the border of the row. How can i do that?

enter image description here

I want to write text in every row which is left aligned and also a text which is right alighned.

Thanks in advance.

Upvotes: 0

Views: 212

Answers (3)

Novarg
Novarg

Reputation: 7440

In your cellForRowAtIndexPath method just create a cell with 2 UILabels and give them the desired location. Small example:

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];
}    
  UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 50, 20)];
  UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(80, 10, 50, 20)];
  label1.textAlignment = UITextAlignmentLeft;
  label2.textAlignment = UITextAlignmentRight;
  //other labels customization and add them to your cell
  return cell;
}

did it without SDK nearby, so there might be some mistakes.

Hope it helps

Upvotes: 2

glogic
glogic

Reputation: 4092

what you could do is subclass the label and over write its drawtextinrect function like so

@interface UILabelCustom : UILabel 

- (void)drawTextInRect:(CGRect)rect;
@end


@implementation UILabelCustom

-(void)drawTextInRect:(CGRect)rect{
    UIEdgeInsets insets = {5, 5, 5, 5};
    return [super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)];
}

@end

this would allow you to give a margin around the edges of the label also you could create a uiview that contains 2 of this custom labels and left and right align them accordingly. using the uvview as a container for the 2 labels to position them as needed. i reckon that could create the effect you looking for

edit: just seen your edit was for table cell. you would need to create a custom cell as well as custom uilabel

@interface CustomCell : UITableViewCell {

}


@property (retain, nonatomic) UILabelCustom *label1,*label2;


@end

@implementation CustomCell

@synthesize label1, label2;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)dealloc
{

    [label1 release];
    [label2 release];


    [super dealloc];
}

@end

Upvotes: 1

nduplessis
nduplessis

Reputation: 12436

UILabel is very limited in terms of styling and layout. Considering either using multiple layouts or CoreText.

Upvotes: 1

Related Questions