【问题标题】:How to set padding for UILabel using @IBDesignable property?如何使用 @IBDesignable 属性为 UILabel 设置填充?
【发布时间】:2016-08-22 12:21:02
【问题描述】:

这是UILabel 的子类的外观:

@IBDesignable class AttributedLabel: UILabel {

    @IBInspectable var padding: CGFloat = 0

    override func drawTextInRect(rect: CGRect) {
        super.drawTextInRect(UIEdgeInsetsInsetRect(rect, UIEdgeInsetsMake(padding, padding, padding, padding)))
    } 
}

我在情节提要中正确设置了padding,但它不起作用,因为填充仍然是0

怎样做才能让它工作?是否可以在 Storyboard 中实时渲染?

【问题讨论】:

    标签: ios swift uilabel ibdesignable


    【解决方案1】:

    这样使用;更改顶部、底部、左侧、右侧的内边距。

    @IBDesignable class AttributedLabel: UILabel {
    
        @IBInspectable var topInset: CGFloat = 5.0
        @IBInspectable var bottomInset: CGFloat = 5.0
        @IBInspectable var leftInset: CGFloat = 7.0
        @IBInspectable var rightInset: CGFloat = 7.0
    
        override func drawTextInRect(rect: CGRect) {
            let insets = UIEdgeInsets(top: topInset, left: leftInset, bottom: bottomInset, right: rightInset)
            super.drawTextInRect(UIEdgeInsetsInsetRect(rect, insets))
        }
    
        override func intrinsicContentSize() -> CGSize {
            var intrinsicSuperViewContentSize = super.intrinsicContentSize()
            intrinsicSuperViewContentSize.height += topInset + bottomInset
            intrinsicSuperViewContentSize.width += leftInset + rightInset
            return intrinsicSuperViewContentSize
        }
    }
    

    谢谢

    【讨论】:

      【解决方案2】:

      您的子类看起来不完整。如文档中所述,您应该覆盖这两种方法:

      public func textRectForBounds(bounds: CGRect, limitedToNumberOfLines numberOfLines: Int) -> CGRect
      public func drawTextInRect(rect: CGRect)
      

      这是一个应该可以工作的示例实现:

      @IBDesignable class AttributedLabel : UILabel
      {
          @IBInspectable var padding: CGFloat = 0 {
              didSet {
                  self.textInsets = UIEdgeInsets(top: self.padding, left: self.padding, bottom: self.padding, right: self.padding)
              }
          }
          var textInsets = UIEdgeInsetsZero {
              didSet {
                  self.invalidateIntrinsicContentSize()
              }
          }
      
          override func textRectForBounds(bounds: CGRect, limitedToNumberOfLines numberOfLines: Int) -> CGRect
          {
              var insets = self.textInsets
              let insetRect = UIEdgeInsetsInsetRect(bounds, insets)
              let textRect = super.textRectForBounds(insetRect, limitedToNumberOfLines: numberOfLines)
              insets = UIEdgeInsets(top: -insets.top, left: -insets.left, bottom: -insets.bottom, right: -insets.right)
              return UIEdgeInsetsInsetRect(textRect, insets)
          }
      
          override func drawTextInRect(rect: CGRect) {
              super.drawTextInRect(UIEdgeInsetsInsetRect(rect, self.textInsets))
          }
      }
      

      您将无法在 Interface Builder 中实时呈现它。

      【讨论】:

      • 为什么不能在 Interface builder 中实时渲染?
      猜你喜欢
      • 1970-01-01
      • 2011-01-26
      • 1970-01-01
      • 1970-01-01
      • 2019-12-25
      • 1970-01-01
      • 2010-12-31
      • 2017-11-07
      相关资源
      最近更新 更多