将解析字符串显示为标签问题

时间:2012-12-03 17:30:38

标签: iphone ios parsing uilabel

我有一个显示在子视图中的标签。我从解析时的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!";

                                 }
                             }];

1 个答案:

答案 0 :(得分:1)

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

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

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

UILabel *infoLabel = [[UILabel alloc] initWithFrame:infoLabelRect];
infoLabel.text = @"Connection error!";
[contentView addSubview:infoLabel];