如何在多元标签中淡出最后一行的末端?
请注意,它必须在Uilabel -1,2,3等中使用不同数量的线路。 我已经找到了解决方案对于1行标签,您可以在其中掩盖。 uilabel与Cagradientlayer的图层,但它不起作用对于多行式标签分层并淡入所有线条。
我试图使另一个具有所需宽度的位置的Calayer位于最后一行的位置,并使用Cagradientlayer作为掩膜,并将此层添加为Uilabel的Sublayer,它用于静态对象,但我在UitableViewCell中使用此uilabel,何时在uilable中使用。敲击 - 它会将颜色变成灰色,我可以看到我的图层,因为它使用了背景颜色uilabel查看布局的子视图时,x位置计算也有问题:
extension UILabel {
func fadeOutLastLineEnd() { //Call in layoutSubviews
guard bounds.width > 0 else { return }
lineBreakMode = .byCharWrapping
let tmpLayer = CALayer()
let gradientWidth: CGFloat = 32
let numberOfLines = CGFloat(numberOfLines)
tmpLayer.backgroundColor = UIColor.white.cgColor
tmpLayer.frame = CGRect(x: layer.frame.width - gradientWidth,
y: layer.frame.height / numberOfLines,
width: gradientWidth,
height: layer.frame.height / numberOfLines)
let tmpGrLayer = CAGradientLayer()
tmpGrLayer.colors = [UIColor.white.cgColor, UIColor.clear.cgColor]
tmpGrLayer.startPoint = CGPoint(x: 1, y: 0)
tmpGrLayer.endPoint = CGPoint(x: 0, y: 0)
tmpGrLayer.frame = tmpLayer.bounds
tmpLayer.mask = tmpGrLayer
layer.addSublayer(tmpLayer)
}
}
“ uilabel”>:
- 因此,我需要
- 最后一行的结尾需要褪色(渐变?)
- 在uitableviewcell中起作用,当整个对象更改颜色时
Note, that it must work with different number of lines in UILabel - 1,2,3 etc.
I've already found solution for 1 line label, where you mask UILabel's layer with CAGradientLayer, but it doesn't work for multiline labels, as it masks the whole layer and fades out all lines.
I tried to make another CALayer with position calculated to be in the position of last line with desired width and used CAGradientLayer as mask and add this layer as sublayer of UILabel, it worked for static objects, but i use this UILabel in UITableViewCell and when it's tapped - it changes color to gray and i can see my layer, because it uses background color of UILabel when view layout its subviews, and also something wrong with x position calculation:
extension UILabel {
func fadeOutLastLineEnd() { //Call in layoutSubviews
guard bounds.width > 0 else { return }
lineBreakMode = .byCharWrapping
let tmpLayer = CALayer()
let gradientWidth: CGFloat = 32
let numberOfLines = CGFloat(numberOfLines)
tmpLayer.backgroundColor = UIColor.white.cgColor
tmpLayer.frame = CGRect(x: layer.frame.width - gradientWidth,
y: layer.frame.height / numberOfLines,
width: gradientWidth,
height: layer.frame.height / numberOfLines)
let tmpGrLayer = CAGradientLayer()
tmpGrLayer.colors = [UIColor.white.cgColor, UIColor.clear.cgColor]
tmpGrLayer.startPoint = CGPoint(x: 1, y: 0)
tmpGrLayer.endPoint = CGPoint(x: 0, y: 0)
tmpGrLayer.frame = tmpLayer.bounds
tmpLayer.mask = tmpGrLayer
layer.addSublayer(tmpLayer)
}
}
So, i need :
- which can be multiline
- end of last line needs to be faded out (gradient?)
- works in UITableViewCell, when the whole object changes color
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有多种方法可以做到这一点 - 这是一种方法。
我们可以通过设置
layer.mask
来掩盖视图。面具的不透明将显示出透明的区域,透明区域不会。因此,我们需要的是一个自定义层子类,看起来像这样:
这是一个示例,我将调用
InvertedGradientLayer
:接下来,我们将使我们制作a <代码> uilabel 实现
InvertedGradientLayer
作为图层蒙版的子类:这是使用它在使用中的示例视图控制器:
运行时,看起来像这样:
红色虚线的边界就在那里,以便我们可以看到标签的框架。敲击上/向下箭头将增加/减少每个标签中要显示的最大线数。
There are various ways to do this -- here's one approach.
We can mask a view by setting the
layer.mask
. The opaque areas of the mask will show-through, and the transparent areas will not.So, what we need is a custom layer subclass that will look like this:
This is an example that I'll call
InvertedGradientLayer
:Next, we'll make a
UILabel
subclass that implements thatInvertedGradientLayer
as a layer mask:and here is a sample view controller showing it in use:
When running, it looks like this:
The red dashed borders are there just so we can see the frames of the labels. Tapping the up/down arrows will increment/decrement the max number of lines to show in each label.
您应该使用与
uilabel
相同的文本属性创建catextlayer
。用您希望褪色的文字结尾填充它。
然后计算
uilabel
中此文本段的位置。终于覆盖了这两个。
以下是解释的一些方面。
You should create a
CATextLayer
with the same text properties as yourUILabel
.Fill it with the end of your text you wish to fade.
Then calculate the position of this text segment in your
UILabel
.Finally overlay the two.
Here are some aspect explained.