ios - 设置UIView根据UILabel调整大小

标签 ios xcode uiview alignment uilabel

Screenshot

我想让我的 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/

相关文章:

iphone - iPhone 6 和 iPhone 5s、4s 的字体大小和 UIView 大小相同

IOS/objective-C : Methods to start and end uiactivityindicatorview

c - 在 Xcode 中使用 C 的文件 I/O 时遇到问题

iphone - UIView 如何访问 UIViewController 的属性?

ios - xcode 在 iPhone 设备上运行应用程序时出错

iphone - 指定图像到 4 英寸 iOS 设备

iphone - 在 iphone 中只显示一次弹出窗口?

ios - 上传多张图片到服务器 IOS

ios - 使用未声明的类型 'PHAsset' BSImagePicker

ios - iOS7、iOS8 均显示警报