UILabel调整其SuperView的大小?

时间:2015-06-06 17:07:08

标签: ios swift uilabel

我有以下视图,其中包含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在运行时调整大小。这意味着绿色底部视图的大小也会增加。

当标签有两行和三行时,结果如下:

enter image description here

UILabel似乎调整了其超级视图的大小。

请注意,我不使用AutoLayout。

  override func layoutSubviews() {
    super.layoutSubviews()
  }

如何阻止UILabel调整其SuperView的大小?

编辑:我还尝试发表评论bottomView?.clipsToBounds = true

2 个答案:

答案 0 :(得分:0)

覆盖超级视图的setFrame:setBounds:(子类,如果它们是普通UIView s),添加断点,并查看堆栈跟踪以找出&#39 ; s导致他们调整大小。

答案 1 :(得分:0)

无需在标签上设置autoResizingMask,只需设置框架即可自动居中。当然,您可以相应地设置UILabel的插图。我在下面添加了测试代码FYI:

 override func viewDidLayoutSubviews() {
    addTestView(CGRectMake(0, 200, view.bounds.width, 50), labelStr: "I am a short testing label")
    addTestView(CGRectMake(0, 260, view.bounds.width, 50), labelStr: "I am a very longlonglonglonglonglonglong testing label")
     addTestView(CGRectMake(0, 320, view.bounds.width, 50), labelStr: "I am a very longlonglonglonglonglonglonglonglonglonglonglong testing label. Will be truncated")
}

func addTestView(frame:CGRect, labelStr: String){
    let bottomView = UIView(frame:frame)
    bottomView.backgroundColor = UIColor.greenColor()
    bottomView.autoresizingMask = UIViewAutoresizing.FlexibleWidth
    bottomView.clipsToBounds = true
    view.addSubview(bottomView)

    var label = UILabel(frame: bottomView.bounds)
    label.textAlignment = NSTextAlignment.Left
    label.numberOfLines = 0
    label.text = labelStr
    bottomView.addSubview(label)

}