Ios 使用Autolayout在自定义UITableviewCell中的UILlabel上添加掩码

Ios 使用Autolayout在自定义UITableviewCell中的UILlabel上添加掩码,ios,iphone,swift,autolayout,uilabel,Ios,Iphone,Swift,Autolayout,Uilabel,我想做的是当一个UILabels文本太长时,添加一个渐变来添加文本的结尾。我的自定义单元格包含2个标签和1个containerview -------------------------------------------- | <LABEL1> <LABEL2 > <UIVIEW> | -------------------------------------------- 然后,我使用一些RXSwift绑定来自VM的属性,并使用

我想做的是当一个UILabels文本太长时,添加一个渐变来添加文本的结尾。我的自定义单元格包含2个标签和1个containerview

--------------------------------------------
| <LABEL1>  <LABEL2             > <UIVIEW> |
--------------------------------------------
然后,我使用一些RXSwift绑定来自VM的属性,并使用一个私有函数检查标签是否应该在末尾添加渐变

private func connectViewModel(_ viewModel: VerticalTVGuideTimeBlockEventViewModel?) {
    guard let viewModel = viewModel else {
        return
    }

    styleViews(theme: viewModel.theme)

    viewModel.startTimeHHmmString.subscribe { [weak timeLabel] startTimeString in
        timeLabel?.text = startTimeString
    }?.disposable.addDisposableTo(viewModel.disposeBag)

    viewModel.title.subscribe { [weak titleLabel] title in
        titleLabel?.text = title
        }?.disposable.addDisposableTo(viewModel.disposeBag)

    viewModel.checkTruncated.subscribe { [weak titleLabel] _, _ in
        self.setNeedsUpdateConstraints()
        self.checkTruncated(for: titleLabel)
    }?.disposable.addDisposableTo(viewModel.disposeBag)

    viewModel.fakeIcons.subscribe { [weak containerView] icons in
        if icons.count > 0 {
            let images = icons.map { icon in
                return icon.image
            }
            let containerWidth = (images.count * 22) + ((images.count - 1) * 4)
            containerView?.showIcons(images: images)

            containerView?.snp.updateConstraints { make in
                make.width.equalTo(containerWidth)
            }
        }
    }?.disposable.addDisposableTo(viewModel.disposeBag)
}

private func checkTruncated(for label: UILabel?) {
    guard let label = label, let viewModel = viewModel else {
        return
    }

    if label.isTruncated() {
        label.addFadeOut(theme: viewModel.theme)
    }
}
要结束addFadeOut方法,请执行以下操作:

func addFadeOut(主题:主题){ self.lineBreakMode=.byClipping

let gradient = CAGradientLayer()
gradient.frame = self.bounds
gradient.colors = [theme.darkJungleGreenColor().cgColor, theme.darkJungleGreenColor().withAlphaComponent(0.0).cgColor]
gradient.rasterizationScale = self.contentScaleFactor
gradient.shouldRasterize = true

//Get Height and width
let width = self.frame.size.width
let height = self.frame.size.height

//Calculate the start points and end points
let startPointX = width.subtracting(40).divided(by: width)
let startPointY = height.divided(by: 2).divided(by: height)
let endPointX = width.subtracting(10).divided(by: width)
let endPointY = height.divided(by: 2).divided(by: height)

gradient.startPoint = CGPoint(x: startPointX, y: startPointY)
gradient.endPoint = CGPoint(x: endPointX, y: endPointY)
self.layer.mask = gradient


log.debug("LABEL WIDTH = \(self.frame.size.width)")
}

问题

就像我说的,我的
UIView
有一个可变的宽度,所以这应该会导致我的
Label2
的宽度不同。但是当我在
AddFadeOut
方法中检查日志语句时,我发现它们都是相同的。所以我现在的问题是,我的
标签2
什么时候有最终宽度,什么时候需要检查标签是否需要添加渐变

let gradient = CAGradientLayer()
gradient.frame = self.bounds
gradient.colors = [theme.darkJungleGreenColor().cgColor, theme.darkJungleGreenColor().withAlphaComponent(0.0).cgColor]
gradient.rasterizationScale = self.contentScaleFactor
gradient.shouldRasterize = true

//Get Height and width
let width = self.frame.size.width
let height = self.frame.size.height

//Calculate the start points and end points
let startPointX = width.subtracting(40).divided(by: width)
let startPointY = height.divided(by: 2).divided(by: height)
let endPointX = width.subtracting(10).divided(by: width)
let endPointY = height.divided(by: 2).divided(by: height)

gradient.startPoint = CGPoint(x: startPointX, y: startPointY)
gradient.endPoint = CGPoint(x: endPointX, y: endPointY)
self.layer.mask = gradient


log.debug("LABEL WIDTH = \(self.frame.size.width)")