Alo*_*ava 8 xcode objective-c ios ios5
我希望我的文字应该正确对齐.
- (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
}
Run Code Online (Sandbox Code Playgroud)
但我的文字不对齐请帮助
Rya*_*los 13
因为您正在使用sizeWithFont然后将框架设置为该大小,所以您的文本是正确对齐的.尝试在标签上添加浅灰色背景颜色,看看我在说什么.您的标签应设置为与表格单元格相同的大小,并允许文本在其中流动.然后它会向右对齐.
更新样本
- (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
}
Run Code Online (Sandbox Code Playgroud)