detailTextLabel textalignment
I am trying to get the cell detail text to be centered.
I have read all kinds of posts on this but they all seem to be talking about older versions of IOS. I t开发者_JAVA百科hink I tried every combination of posts but no luck in making it work.
[[cell detailTextLabel] setTextAlignment:UITextAlignmentCenter];
I tried this from willDisplayCell and also in the code below, neither works. Note 2 ways of doing this I tried in both methods.
Does anyone know if this does work or should I create my own center function (method)?
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;
If Alignment is problem for you then you can create custom labels and add subview to the cell.
- (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;
}
Alternatively if you want to center the detailTextLabel
while still making use of the automatic vertical center (textLabel will be vertically centered, if the detailTextLabel
is empty) you need to override - (void) layoutSubviews
.
Otherwise the label's size will fit the content, and therefore textAlignment = UITextAlignmentCenter
will not work.
- (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);
}
精彩评论