我正在尝试使单元格详细信息文本居中。
我读过关于这方面的各种帖子,但似乎都在谈论旧版本的IOS。我想我尝试了所有的帖子组合,但都没有成功。
[[cell detailTextLabel] setTextAlignment:UITextAlignmentCenter];我在willDisplayCell和下面的代码中尝试过,两者都不起作用。注意2种我在两种方法中都尝试过的方法。
有没有人知道这是否有效,或者我是否应该创建自己的中心函数(方法)?
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:18.0];
cell.detailTextLabel.font = [UIFont systemFontOfSize:16];
NSMutableDictionary *curRow = [myData objectAtIndex:indexPath.row];
cell.textLabel.text = [curRow objectForKey:@"Description"];
cell.detailTextLabel.text = [curRow objectForKey:@"Stats"];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.detailTextLabel.textAlignment = UITextAlignmentCenter;发布于 2011-07-06 12:37:12
如果对齐有问题,您可以创建自定义标签并向单元格添加子视图。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
UILabel *label;
UILabel *detailLabel;
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
label = [[[UILabel alloc] initWithFrame:CGRectMake(55, 4, 260, 20)] autorelease];
//make Your alignments to this label
label.font = [UIFont boldSystemFontOfSize:15.0];
label.tag=25;
//make Your alignments to this detail label
detailLabel = [[[UILabel alloc] initWithFrame:CGRectMake(55, 25, 260, 15)] autorelease];
detailLabel.font = [UIFont systemFontOfSize:13.0];
detailLabel.tag=30;
[cell.contentView addSubview:label];
[cell.contentView addSubview:detailLabel];
}
else
{
label = (UILabel *)[cell.contentView viewWithTag:25];
detailLabel = (UILabel *)[cell.contentView viewWithTag:30];
}
label.text =[curRow objectForKey:@"Description"];
detailLabel.text=[curRow objectForKey:@"Stats"];
return cell;
}发布于 2011-10-19 16:59:24
或者,如果您希望在使用自动垂直居中的同时将detailTextLabel居中(如果detailTextLabel为空,则textLabel将垂直居中),则需要覆盖- (void) layoutSubviews。
否则,标签的大小将适合内容,因此textAlignment = UITextAlignmentCenter将无法工作。
- (void) layoutSubviews
{
[super layoutSubviews];
self.textLabel.frame = CGRectMake(0, self.textLabel.frame.origin.y, self.frame.size.width, self.textLabel.frame.size.height);
self.detailTextLabel.frame = CGRectMake(0, self.detailTextLabel.frame.origin.y, self.frame.size.width, self.detailTextLabel.frame.size.height);
}https://stackoverflow.com/questions/6591409
复制相似问题