我有以下代码:
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];
}];
}
标签extraname
和scriptname
最终被填满或被砍掉,即不完整的数据。我怎样才能解决这个问题?当我静态地将大文本放入其中时,数据很好地填充到这些视图中。救命啊!
答案 0 :(得分: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宽度更改其字体,请使用以下内容; 注意:仅当self.label.numberOfLines = 1时才有效。
self.label.adjustsFontSizeToFitWidth = YES;
self.label.minimumFontSize = 0;