2017-03-23 7 views
0

私は、次のコードを持っている:カスタムラベルを完全

if (product) { 

    if ([product.title isStringOfInterest]) { 

     scriptName = [[PPLinearLayoutLabelItem alloc] initWithText:@"" font:[PPFonts bold20] maxWidth:[LayoutValues getMaxWidthClipped]]; 

     [self.topContainerContent addObject:scriptName]; 

     extraName = [[PPLinearLayoutLabelItem alloc] initWithText:@"" font:[PPFonts regular14] maxWidth:[LayoutValues getMaxWidthClipped]]; 
     [scriptName setPaddingTop:20]; 


     [self.topContainerContent addObject:extraName]; 
     } 
     } 

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^(void){ 
    [self loadProductDetails]; 
    }); 



-(void) loadProductDetails{ 
    IHProduct *product = orderDisplayed.product; 
    id<ProductDownloadServiceProtocol> productDetailsDownloader = [[ProductDownloadService alloc] initWithClient:[[HttpClient alloc] initWith:APIbackend forEndpoint:EndpointProduct]]; 


    [productDetailsDownloader downloadProductWithProductID:product.productID success:^(ProductDownloadResponse *response) { 

    scriptName.label.text = [NSString stringWithFormat:@"%@ (%@)",response.product.title,response.product.pillTypeShort]; 
    extraName.label.text = response.product.sameAs; 

    NSString *qtyText = [NSString stringWithFormat:@"%@ PACKS (%@ at $%@ per pack)",[orderDisplayed.interval objectForKey:@"quantity_per_interval"], [orderDisplayed.interval objectForKey:@"quantity_per_interval"], response.product.price]; 

    quantity.label.text = qtyText; 
    } error:^(ProductDownloadResponse *response) { 
     [self hideHttpLoadingOverlay]; 
     [RMUniversalAlert showAlertInViewController:self 
              withTitle:alertErrorTitle 
              message:@"Error downloading mail order product details" 
            cancelButtonTitle:OKDialogButton  
          destructiveButtonTitle:nil 
            otherButtonTitles:nil 
              tapBlock:nil]; 
     }]; 
} 

ラベルextranamescriptnameは、すなわち、不完全なデータを埋めなっているか、最後に切り落とさばかり。これをどうすれば解決できますか?私が静的に大きなテキストを置くとき、データはそれらのビューにうまく埋められます。助けて!

答えて

1

テキストコンテンツに基づいてUILabelの幅を調整したい場合は、以下のコードを使用してください。

NSDictionary *fontAttributes = @{NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue" size:14]}; 
CGRect rectOfText = [textToMeasure boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX) 
              options:NSStringDrawingUsesLineFragmentOrigin 
             attributes:fontAttributes 
              context:nil]; 
CGRect tempFrame = self.label.frame; 
tempFrame.size.width = rectOfText.size.width; 
self.label.frame = tempFrame; 

そして、フォントをuilabelの幅に基づいて変更したい場合は、以下のようにします。 注:これは= 1

self.label.adjustsFontSizeToFitWidth = YES; 
self.label.minimumFontSize = 0; 
+0

self.label.numberOfLinesが、私は私のloadproductdetails内からすなわちディスパッチスレッドこのすべてを行うことはできますかように、あなたが最初に提案するように私は私のラベルを設定する必要がない場合にのみ動作しますapiコールは後でそれをデータで埋めますか? – Ackman

+0

PS rectOfTextとは何ですか? fontAttributes? – Ackman

+0

私はコードを修正しました!今すぐloadproductdetailsに追加してください(テスト目的)! @Ackman –