我想让我的 UILabel 在覆盖层中水平和垂直居中显示,并且我希望覆盖层 UIView(时髦 lorem ipsum 文本后面带有浅色 alpha 的黑色区域)调整大小以给我 10 像素顶部和底部的填充,但仍然自动拉伸(stretch)其宽度。谁能指出我正确的方向吗?我想让它尽可能充满活力。
这是我必须让它看起来像这样的:
descView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, self.view.bounds.size.width, 160)];
descView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
descView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
descLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 600, 120)];
descLabel.center = CGPointMake(self.view.bounds.size.width/2, descView.bounds.size.height/2);
descLabel.backgroundColor = [UIColor clearColor];
descLabel.opaque = NO;
descLabel.textAlignment = UITextAlignmentCenter;
descLabel.text = currentPhoto.desc;
descLabel.numberOfLines = 5;
descLabel.textColor = [UIColor whiteColor];
descLabel.autoresizingMask = UIViewAutoresizingFlexibleMargins;
CGRect frame = descLabel.frame;
frame.origin.x = (descView.frame.size.width - descLabel.frame.size.width)/2;
descLabel.frame = frame;
[self.view addSubview:descView];
[descView addSubview:descLabel];
最佳答案
使用https://gist.github.com/2596057 (这是 Marco Arment 的子类 UILabel)
(或者)您可以使用 resizeHeightToFitText 方法的代码来执行相同的操作,而不使用 hte IPInsetLabel
调整标签大小以适应文本后,根据高度和宽度设置标签的原点
关于ios - 设置UIView根据UILabel调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10658996/