我正在尝试添加可变大小的UILabel。
标签包含用户的评论。我在这里试图实现我的标签的动态大小调整,然后我将它们添加到我的视图中(我后来添加到主视图中)
我知道这会使标签出现在彼此的 ontop 上,但这不是问题。标签根本没有显示。
NSMutableDictionary *comments = [[NSMutableDictionary alloc] init];
[comments setObject:@"xxxxxxxxx" forKey:@"simon"];
[comments setObject:@"xxxxxxxxxxxxxxxx" forKey:@"pontus"];
[comments setObject:@"xxxxx" forKey:@"sudden"];
[comments setObject:@"xxxx" forKey:@"juan"];
[comments setObject:@"xxxxxxxxxxx" forKey:@"eric"];
DetailedViewController *detailedBurger= [[DetailedViewController alloc] initWithNibName:nil bundle:nil];
for(NSString *key in comments){
UILabel *label = [[UILabel alloc] init];
label.backgroundColor = [UIColor clearColor];
label.text = [comments objectForKey:key]; //etc...
[label setFont:[UIFont fontWithName:@"PatuaOne-Regular" size:12.0]];
CGSize maximumLabelSize = CGSizeMake(296,9999);
CGSize expectedLabelSize = [label.text sizeWithFont:label.font
constrainedToSize:maximumLabelSize
lineBreakMode:label.lineBreakMode];
//adjust the label the the new height.
CGRect newFrame = label.frame;
newFrame.size.height = expectedLabelSize.height;
newFrame.size.width = expectedLabelSize.width;
NSLog(@"expectedlabelsizeheight %f", newFrame.size.height);
label.frame = newFrame;
[label setNeedsDisplay];
[detailedBurger.commentField addSubview:label];
NSLog(@"%f, %f", label.center.x, label.center.y);
}
// Do any additional setup after loading the view from its nib.
self.backgroundView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background.png"]];
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.backgroundView.frame];
scrollView.contentSize = CGSizeMake(320, 900);
[scrollView addSubview:detailedBurger.view];
detailedBurger.view.center = CGPointMake(160, detailedBurger.view.frame.size.height/2);
[self.backgroundView addSubview:scrollView];
如果我使用硬值显式设置框架(例如label.frame = CGRectMake(0,0,10,10)
),则此代码有效,但动态大小调整似乎不起作用。
控制台输出:
2013-07-09 10:12:42.223 RateYourBurger[31439:c07] expectedlabelsizeheight 15.000000
2013-07-09 10:12:42.224 RateYourBurger[31439:c07] 10.000000, 20.000000
2013-07-09 10:12:42.224 RateYourBurger[31439:c07] expectedlabelsizeheight 15.000000
2013-07-09 10:12:42.224 RateYourBurger[31439:c07] 10.000000, 20.000000
2013-07-09 10:12:42.225 RateYourBurger[31439:c07] expectedlabelsizeheight 15.000000
2013-07-09 10:12:42.225 RateYourBurger[31439:c07] 10.000000, 20.000000
2013-07-09 10:12:42.226 RateYourBurger[31439:c07] expectedlabelsizeheight 15.000000
2013-07-09 10:12:42.226 RateYourBurger[31439:c07] 10.000000, 20.000000
2013-07-09 10:12:42.227 RateYourBurger[31439:c07] expectedlabelsizeheight 15.000000
2013-07-09 10:12:42.227 RateYourBurger[31439:c07] 10.000000, 20.000000
答案 0 :(得分:1)
动态大小调整不起作用,因为您必须在设置框架后调用方法setNeedsDisplay
。
label.frame = newFrame;
[label setNeedsDisplay];
修改强>
NSString *text1 = @"This is my text This is my text This is my text This is my text This is my text This is my text This is my text This is my text";
CGSize constraint1 = CGSizeMake(280, 2000);
CGSize size1 = [text1 sizeWithFont:[UIFont systemFontOfSize:12] constrainedToSize:constraint1 lineBreakMode:UILineBreakModeWordWrap];
label.frame=CGRectMake(posx,posy,size1.width,size1.height);
label.lineBreakMode = UILineBreakModeWordWrap;
label.numberOfLines = size1.height/15;
[label setFont:[UIFont systemFontOfSize:12]];
label.text = text1;
[label setNeedsDisplay];