【发布时间】:2015-06-06 17:07:08
【问题描述】:
我有以下包含UILabel 的视图:
class MyView : UIView {
func viewDidLoad() {
self.autoresizingMask = UIViewAutoresizing.FlexibleHeight | UIViewAutoresizing.FlexibleWidth
bottomView = UIView(frame: CGRectMake(self.bounds.origin.x, self.bounds.origin.y + self.imageView!.bounds.size.width, self.bounds.size.width, self.bounds.size.height - self.imageView!.bounds.size.height))
// bottomView frame calculation is: (0.0, 355.0, 355.0, 130.0)
bottomView?.backgroundColor = UIColor.greenColor()
bottomView?.autoresizingMask = UIViewAutoresizing.FlexibleWidth
bottomView?.clipsToBounds = true
self.addSubview(self.bottomView!)
var descriptionRect: CGRect = CGRectInset(self.bottomView!.bounds, leftRightInset, 20/2)
let descriptionLabel = UILabel()
descriptionLabel.numberOfLines = 3
descriptionLabel.autoresizingMask = UIViewAutoresizing.FlexibleWidth
descriptionLabel.font = UIFont(name: MGFont.helvetica, size: 22)
descriptionLabel.textColor = UIColor.whiteColor()
descriptionLabel.textAlignment = NSTextAlignment.Left
descriptionLabel.backgroundColor = UIColor.blueColor()
var paragraphStyle:NSMutableParagraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = 1.0
paragraphStyle.lineBreakMode = NSLineBreakMode.ByTruncatingTail
let attributes = [NSParagraphStyleAttributeName : paragraphStyle]
descriptionLabel.attributedText = NSAttributedString(string: previewCard.title, attributes:attributes)
bottomView?.addSubview(descriptionLabel)
descriptionLabel.bounds = descriptionRect
descriptionLabel.sizeToFit()
descriptionLabel.center = CGPointMake(bottomView!.bounds.width/2, bottomView!.bounds.height/2 - hotelNameLableHeight/2)
}
}
bottomView 的高度应该始终是固定的。
MyView 在运行时调整大小。这意味着绿色底视图的大小也会增加。
这是标签有两行和三行时的结果:
UILabel 似乎调整了其超级视图的大小。
请注意,我不使用 AutoLayout。
override func layoutSubviews() {
super.layoutSubviews()
}
如何防止 UILabel 调整其 SuperView 的大小?
编辑:我也尝试评论bottomView?.clipsToBounds = true
【问题讨论】:
-
标签是否放置在使用自动布局的视图层次结构中?
-
您是否使用 4 行、5 行或更多行文本进行了验证? bottomView 的高度是不是一直在增加?
-
发布您的
layoutSubviews的self -
评论此行并检查。 bottomView?.clipsToBounds = true
-
@Bannings 我发布了 layoutSubviews