Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UILabel text alignment right

I want that my text should be align right.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"lisn"];
cell=[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"lisn"] autorelease];
CGSize  textSize = { 210.0, 10000.0 };
CGSize size = [[gMessageArray objectAtIndex:indexPath.row] sizeWithFont:[UIFont systemFontOfSize:12] constrainedToSize:textSize lineBreakMode:UILineBreakModeWordWrap];

UILabel *lisnerMessage=[[[UILabel alloc] init] autorelease];
lisnerMessage.backgroundColor = [UIColor clearColor];
[lisnerMessage setFrame:CGRectMake(75 ,20,size.width + 5,size.height+2)];
lisnerMessage.numberOfLines=0;
lisnerMessage.textAlignment=UITextAlignmentRight;
lisnerMessage.text=[gMessageArray objectAtIndex:indexPath.row];
[cell.contentView addSubview:lisnerMessage];
return cell
}

but my text is not align right Please Help

like image 729
Alok Srivastava Avatar asked Jun 27 '12 14:06

Alok Srivastava


People also ask

How do I align text in Swiftui?

To align a text view along the horizontal axis, you need to use . frame() modifier with maxWidth set to . infinity and alignment to the alignment you want.

What is UILabel?

A view that displays one or more lines of informational text.

How do you use TextAlign in flutter?

TextAlign. start places the text in the leading end of the parent widget's boundaries. The text is placed either left or right according to the textDirection property. If the textDirection is ltr , the text will be aligned left.


2 Answers

Because you are using sizeWithFont and then setting your frame to that size, your text is aligned right. Try added a background color of light gray to your label to see what I'm talking about. Your label should be set to the same size as your table cell and allow the text to flow inside it. Then it will align to the right.

Update with sample

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"lisn"];
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"lisn"];

    UILabel *lisnerMessage = [[UILabel alloc] init];
    lisnerMessage.backgroundColor = [UIColor clearColor];
    [lisnerMessage setFrame:cell.frame];
    lisnerMessage.numberOfLines = 0;
    lisnerMessage.textAlignment = NSTextAlignmentRight;
    lisnerMessage.text = [gMessageArray objectAtIndex:indexPath.row];
    [cell.contentView addSubview:lisnerMessage];

    return cell
}
like image 163
Ryan Poolos Avatar answered Sep 23 '22 07:09

Ryan Poolos


Right alignment for label

yourLabel.textAlignment = NSTextAlignmentRight;
like image 39
Vineesh TP Avatar answered Sep 24 '22 07:09

Vineesh TP