【问题标题】:Stretch UIBarButtonItem to accommodate customView拉伸 UIBarButtonItem 以适应 customView
【发布时间】:2015-02-11 16:36:44
【问题描述】:

我将UILabel 设置为customView 用于UIBarButtonItem。但是,我需要按钮宽度是动态的以适应更长的标签。目前它只是截断它们。这是我的代码:

let participantCountLabel = UILabel()
participantCountLabel.text = "\(participantCount) participants"
self.toolbarLeftButton.customView = participantCountLabel

我认为这可能是因为灵活空间。需要调整大小吗?

截图:

这是故事板:

【问题讨论】:

    标签: ios swift uibarbuttonitem


    【解决方案1】:

    也许你可以这样做:

    let participantCountLabel = UILabel()
    participantCountLabel.text = "\(participantCount) participants"
    self.toolbarLeftButton.customView = participantCountLabel
    self.toolbarLeftButton.customView.sizeToFit()
    

    【讨论】:

      【解决方案2】:

      试试这个方法找到文本的宽度和高度,然后设置标签大小

          var participantCountLabel: UILabel=UILabel(frame: CGRectMake(100, 100, 10, 40));
          participantCountLabel.text = "\(participantCount) participants"
      
      
          var strText:NSString=participantCountLabel.text!
      
          let styleLineBreakWordWrap :  NSMutableParagraphStyle=NSMutableParagraphStyle()
          styleLineBreakWordWrap.lineBreakMode=NSLineBreakMode.ByWordWrapping
          styleLineBreakWordWrap.alignment=NSTextAlignment.Left
      
      
          var attributes:NSDictionary=[NSFontAttributeName : participantCountLabel.font,
              NSParagraphStyleAttributeName:styleLineBreakWordWrap]
      
          strText.sizeWithAttributes(attributes);
      
          var maximumLabelSize : CGSize=CGSizeMake(1000, 100)
      
          var textRect: CGRect=strText.boundingRectWithSize(maximumLabelSize, options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: attributes, context: nil)
      
          participantCountLabel.frame.size=textRect.size;
      
          self.toolbarLeftButton.customView = participantCountLabel
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-07-20
        • 2013-08-22
        • 1970-01-01
        • 2010-11-13
        • 2020-10-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-25
        相关资源
        最近更新 更多