0

我有一个显示在子视图中的标签。我在解析时从 NSString 获得了标签的文本。但是,我尝试显示文本,但它似乎不起作用。没有显示文本。

这是我的代码。如果您知道如何解决此问题,请告诉我:

PFQuery *query = [PFQuery queryWithClassName:@"myapp"];
[query getObjectInBackgroundWithId:@"myId"
                             block:^(PFObject *textu, NSError *error) {
                                 if (!error) {

                                     CGRect infoLabelRect = CGRectMake(10, 250, 260, 350);
                                     UILabel *infoLabel = [[UILabel alloc] initWithFrame:infoLabelRect];


                                     NSString *text = [textu objectForKey:@"newstext"];
                                     UIFont *font = nil;
                                     CGFloat points = 17;
                                     CGFloat maxHeight = infoLabel.frame.size.height;
                                     CGFloat textHeight;
                                     do {
                                         font = [UIFont systemFontOfSize:points];
                                         CGSize size = CGSizeMake(infoLabelRect.size.width, 100000);
                                         CGSize textSize = [text sizeWithFont:font constrainedToSize:size lineBreakMode: NSLineBreakByWordWrapping];
                                         textHeight = textSize.height;
                                         points -= 1;
                                     } while (textHeight > maxHeight);
                                     infoLabel.font = font;
                                     infoLabel.numberOfLines = 9;

                                     infoLabel.textColor = [UIColor whiteColor];
                                     infoLabel.textAlignment = NSTextAlignmentCenter;
                                     infoLabel.backgroundColor = [UIColor clearColor];
                                     infoLabel.shadowColor = [UIColor blackColor];
                                     infoLabel.shadowOffset = CGSizeMake(0, 1);

                                     [infoLabel sizeToFit];
                                     [contentView addSubview:infoLabel];


                                 } else {
                                     // Log details of our failure
                                     CGRect infoLabelRect = CGRectMake(10, 250, 260, 350);
                                     UILabel *infoLabel = [[UILabel alloc] initWithFrame:infoLabelRect];

                                     infoLabel.text = @"Connection error!";

                                 }
                             }];
4

1 回答 1

1

您的第一部分代码不包含对文本的分配

infoLabel.text = text; // or @"Some text" for testing

您的最后一部分代码仅创建 UILabel,但不会将其添加为子视图

UILabel *infoLabel = [[UILabel alloc] initWithFrame:infoLabelRect];
infoLabel.text = @"Connection error!";
[contentView addSubview:infoLabel];
于 2012-12-03T17:37:33.457 回答