2015-09-09 105 views
0

我正在做一個簡單的報價生成器的個人項目。UILabel如何自動調整內容?

用戶可以將喜歡的引用保存到列表中,並且在列表視圖中,我希望UILabel能夠響應內容的大小(一些引號很短,有些很長 - 可能看起來有點奇怪)。

任何幫助將不勝感激。

UILabel *favouriteLabel; 
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){ 
     favouriteLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, -10, 400, 140)]; 
    }else { 
     favouriteLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, -20, 180, 130)]; 
    } 
    favouriteLabel.backgroundColor = [UIColor clearColor]; 

    favouriteLabel.text = [self.arrayFromFile objectAtIndex:indexPath.row]; 
    NSLog(@"Row alloc %d", indexPath.row); 
    NSLog(@"Load label %@", favouriteLabel.text); 
    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){ 
     favouriteLabel.font = [UIFont fontWithName: @"Eras Bold ITC" size: 18]; 
    }else if([GUIHelper isPhone5]){ 
     favouriteLabel.font = [UIFont fontWithName: @"Dosis-Medium" size: 15]; 
    } 
    else 
     favouriteLabel.font = [UIFont fontWithName: @"Dosis-Medium" size: 15]; 
    favouriteLabel.textColor = [UIColor colorWithRed:68.0/255.0 green:68.0/255.0 blue:68.0/255.0 alpha:1]; 
    favouriteLabel.textAlignment = UITextAlignmentCenter; 
    favouriteLabel.numberOfLines = 0; 
    favouriteLabel.center = CGPointMake(cell.center.x - 40, cell.center.y + 20); 
    favouriteLabel.tag = 900; 
    [cell.contentView addSubview:favouriteLabel]; 

    UIImage *lineImg = [UIImage imageNamed:@"[email protected]"]; 
    CGRect lineImageFrame; 
    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){ 
     lineImageFrame = CGRectMake(0, 120, lineImg.size.width, lineImg.size.height); 
    }else{ 
     lineImageFrame = CGRectMake(0, 62, lineImg.size.width * 0.9, 0.8*lineImg.size.height); 
    } 
    UIImageView *lineImageView = [[UIImageView alloc] initWithFrame:lineImageFrame]; 
    lineImageView.image = lineImg; 
    lineImageView.center = CGPointMake(cell.center.x - 40, lineImageView.center.y + 35); 

回答

0

在將標籤添加到視圖之前,請嘗試[favoriteLabel sizeToFit]