Reputation: 7304
I know how to align text at UILabel.
But this problem is a bit wired.
All other alignments work such as NSTextAlignmentJustified
, NSTextAlignmentNatural
. Only NSTextAlignmentCenter
does not work to make the text at center.
My UILabel is inserted using StoryBoard.
Then text formatting at UILabel is implemented at program as
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"CustomCell" forIndexPath:indexPath];
CGRect frame = cell.cellText.frame;
frame.size.width = cell.bounds.size.width;
frame.size.height = cell.bounds.size.height;
cell.cellText.frame = frame;
cell.cellText.font = [UIFont fontWithName:@"ArialMT" size:screenRect.size.height*0.0163];
cell.cellText.textAlignment = NSTextAlignmentCenter;
cell.cellText.lineBreakMode = NSLineBreakByWordWrapping;
cell.cellText.numberOfLines = 0;
cell.cellText.clipsToBounds = YES;
cell.cellText.backgroundColor = [UIColor clearColor];
cell.cellText.textColor = [UIColor blackColor];
if(indexPath.section == 0){
cell.backgroundColor=[UIColor blackColor];
cell.cellText.textColor = [UIColor whiteColor];
if(indexPath.item == 0)
cell.cellText.text = @"Date";
else if(indexPath.item == 1)
cell.cellText.text = @"Age";
else if(indexPath.item == 2)
cell.cellText.text = @"Height (cm)";
else if(indexPath.item == 3)
cell.cellText.text = @"%le";
else if(indexPath.item == 4)
cell.cellText.text = @"Weight (kg)";
else if(indexPath.item == 5)
cell.cellText.text = @"%le";
}
else if(indexPath.section % 2 == 0 && indexPath.section != 0)
cell.backgroundColor=[UIColor grayColor];
else
cell.backgroundColor=[UIColor lightGrayColor];
return cell;
}
Why can't align to center as shown in the picture.
EDIT: I changed the UILabel's background as
if(indexPath.section == 0){
cell.backgroundColor=[UIColor blackColor];
cell.cellText.textColor = [UIColor whiteColor];
cell.cellText.backgroundColor = [UIColor redColor];
if(indexPath.item == 0)
cell.cellText.text = @"Date";
else if(indexPath.item == 1)
cell.cellText.text = @"Age";
else if(indexPath.item == 2)
cell.cellText.text = @"Height (cm)";
else if(indexPath.item == 3)
cell.cellText.text = @"%le";
else if(indexPath.item == 4)
cell.cellText.text = @"Weight (kg)";
else if(indexPath.item == 5)
cell.cellText.text = @"%le";
}
Upvotes: 0
Views: 231
Reputation: 5130
Replace these lines
CGRect frame = cell.cellText.frame;
frame.size.width = cell.bounds.size.width;
frame.size.height = cell.bounds.size.height;
cell.cellText.frame = frame;
With this code.
cell.cellText.center = cell.center;
CGRect frame = cell.cellText.frame;
frame.size.width = cell.bounds.size.width;
frame.size.height = cell.bounds.size.height;
cell.cellText.frame = frame;
If you want to use autolayout then just add two constraints two your label.
Upvotes: 1
Reputation: 1505
Don't use the frame for setting the position of the cellText
. The cell hasn't been laid out in collectionView:cellForItemAtIndexPath
so the frame will be wrong. It's entirely plausible that the frame will be the frame of the last time that cell was reused.
Instead, use AutoLayout to constrain the UILabel to all edges of the cell.
Upvotes: 1