Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 无法在tableViewcell中设置UILabel填充_Ios_Iphone_Swift_Uitableview_Uilabel - Fatal编程技术网

Ios 无法在tableViewcell中设置UILabel填充

Ios 无法在tableViewcell中设置UILabel填充,ios,iphone,swift,uitableview,uilabel,Ios,Iphone,Swift,Uitableview,Uilabel,我已使用StackOverflow设置UILabel填充,以解决自动布局问题。 这个线程基本上是一个UILabel扩展 部分答案是: class NRLabel : UILabel { var textInsets = UIEdgeInsets.zero { didSet { invalidateIntrinsicContentSize() } } override func textRect(forBounds bounds: CGRect, lim

我已使用StackOverflow设置UILabel填充,以解决自动布局问题。 这个线程基本上是一个UILabel扩展

部分答案是:

class NRLabel : UILabel {

    var textInsets = UIEdgeInsets.zero {
        didSet { invalidateIntrinsicContentSize() }
    }

    override func textRect(forBounds bounds: CGRect, limitedToNumberOfLines numberOfLines: Int) -> CGRect {
        let insetRect = UIEdgeInsetsInsetRect(bounds, textInsets)
        let textRect = super.textRect(forBounds: insetRect, limitedToNumberOfLines: numberOfLines)
        let invertedInsets = UIEdgeInsets(top: -textInsets.top,
                                          left: -textInsets.left,
                                          bottom: -textInsets.bottom,
                                          right: -textInsets.right)
        return UIEdgeInsetsInsetRect(textRect, invertedInsets)
    }

    override func drawText(in rect: CGRect) {
        super.drawText(in: UIEdgeInsetsInsetRect(rect, textInsets))
    }
}
一切正常,添加了填充,但我需要重新加载tableViewcell才能看到效果

我已经覆盖了customCell的视图WillLayoutSubViews()函数,类似于填充的函数

 self.chatLabel.textInsets = UIEdgeInsets.init(top: 10, left: 10, bottom: 10, right: 10)
效果是这样的

你看,第一个标签是在重新加载单元格后得到它的填充

请建议如何使用上述扩展实现UILabel填充,以解决此问题。添加 将这两个函数添加到扩展中,并删除所有其他函数:

 override func drawText(in rect: CGRect) {
            let insets: UIEdgeInsets = UIEdgeInsets(top: topInset, left: leftInset, bottom: bottomInset, right: rightInset)
            super.drawText(in: UIEdgeInsetsInsetRect(rect, insets))
        }
        override var intrinsicContentSize : CGSize {
            var intrinsicSuperViewContentSize = super.intrinsicContentSize
            intrinsicSuperViewContentSize.height += topInset + bottomInset
            intrinsicSuperViewContentSize.width += leftInset + rightInset
            return intrinsicSuperViewContentSize
        }

使用您的
NRLabel
类,这对我来说很好。标签添加到Storyboard TableViewCell Prototype中,约束设置为允许自动调整行大小

class PaddedLabelCell : UITableViewCell {

    @IBOutlet weak var theLabel: NRLabel!

    override func awakeFromNib() {
        super.awakeFromNib()
        self.setupLabel()
    }

    func setupLabel() -> Void {
        theLabel.layer.cornerRadius = 8.0
        theLabel.layer.masksToBounds = true
        theLabel.textInsets = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
        // any other setup stuff can go here....
    }

}

NRLabel中没有任何内容告诉它在textInsets属性更改时重新绘制。您需要这样做:

var textInsets = UIEdgeInsets.zero {
    didSet { 
        invalidateIntrinsicContentSize() 
        setNeedsDisplay()
    }
}

因此,现在当textInsets属性更改时,NRLabel将使用新的文本插入重新绘制。

经过长时间的疲劳后,我刚刚找到了我从未尝试过的解决方案,我不知道为什么。:(

cellforrowatinexpath中,我刚刚写了customCell文件中的一行

回手机前先打这个电话

cell.chatLabel.textInsets = UIEdgeInsets.init(top: 10, left: 10, bottom: 10, right: 10)
其背后的原因可能是,Cell具有所有的功能,但它并没有得到太多的刷新它的UI。
因此,在显示单元格之前调用行可以解决问题。

不清楚您想要实现什么。您想要一个带填充的UILabel?有没有理由不将标签作为UIView的子视图,而只使用自动布局和约束?@elk\u cloner您在单元格中使用过cell.dequeue吗?我使用过自定义单元格。我想要ui label wi填充我认为问题不在于标签…请检查表格单元格高度…是否相同?…请检查。