为什么当我的uilabel文字缩小时,里面的文字不再居中?

时间:2015-09-23 02:35:36

标签: ios objective-c uilabel

如我的图片所示,当我的UILabel中的文字开始缩小时,它开始向中心下方移动得更远。无论是否收缩,我怎样才能确保它留在中心?

问题图片(Bradford West Gwillimbury非常引人注目):

enter image description here

代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if(cell == nil)
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    else
        [[cell.contentView subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];



    UIView *view = [[UIView alloc] initWithFrame: CGRectMake (20, 0, self.view.frame.size.width-20, 90)];
    UILabel *cityLabel = [[UILabel alloc] initWithFrame: CGRectMake (10, 5, self.view.frame.size.width-20, 55)];
    UILabel *supportedCitiesLabel = [[UILabel alloc] initWithFrame: CGRectMake (10, 65, self.view.frame.size.width-20, 20)];

    cityLabel.font = [UIFont boldSystemFontOfSize:50.0];
    cityLabel.text = mainCities[indexPath.row];
    supportedCitiesLabel.text = partCities[indexPath.row];

    if([supportedCitiesLabel.text isEqualToString:@""]) // city has no supported cities so centering main city in view
    {
        supportedCitiesLabel.hidden = true;
        [cityLabel setFrame:CGRectMake (10, 5, self.view.frame.size.width-20, 55+25)];
    }


    cityLabel.lineBreakMode = NSLineBreakByWordWrapping;




    CGRect labelRect = cityLabel.frame;
    cityLabel.adjustsFontSizeToFitWidth = YES;
    cityLabel.numberOfLines = 1;

    CGFloat fontSize = 50;
    while(fontSize > 0.0)
    {
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
        paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
        NSDictionary *attrDict = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont boldSystemFontOfSize:fontSize], NSFontAttributeName, paragraphStyle, NSParagraphStyleAttributeName, nil];

        CGSize size = [cityLabel.text boundingRectWithSize:CGSizeMake(labelRect.size.width, 10000)
                                                                options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin
                                                             attributes:attrDict context:nil].size;

        if(size.height <= labelRect.size.height)
            break;

        fontSize -= 1.0;
    }

    cityLabel.font = [UIFont boldSystemFontOfSize:fontSize];
    [cityLabel setBaselineAdjustment:UIBaselineAdjustmentAlignCenters];


    [cityLabel setBackgroundColor:[UIColor redColor]];
    [supportedCitiesLabel setBackgroundColor:[UIColor yellowColor]];



    [view addSubview:cityLabel];
    [view addSubview:supportedCitiesLabel];
    [cell.contentView addSubview:view];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor whiteColor];



    return cell;
}

当我将cityLabel.adjustsFontSizeToFitWidth设置为NO而不是YES时,这是结果: enter image description here

0 个答案:

没有答案