【发布时间】:2017-06-06 06:50:54
【问题描述】:
我正在使用自定义 TableCell,它的标签为 numberofLines = 0
我想要这样的标签。
只有三个角是圆角的。为此,我在单元格的 swift 文件中编写了这样的代码。我基本上扩展了 UIView。
extension UIView {
func roundCorners(corners:UIRectCorner, radius: CGFloat) {
let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
let mask = CAShapeLayer()
mask.path = path.cgPath
self.layer.mask = mask
}
}
我已经用这样的方法来圆我的角落。
self.chatLabel.roundCorners(corners: [.topLeft,.bottomLeft,.bottomRight], radius: 7.0)
但问题是当标签的内容大于单元格的行高时,它不会显示内容
请在第一张图片中查看第二个单元格的标签。它的内容没有显示。 但是当我删除圆角的代码时,一切正常,但失去了我的圆角半径。
非常感谢任何建议。谢谢。
已编辑:
我的细胞是这样的。
我正在使用下面的代码实现聊天 UI。
var cellTypeToDraw:cellType {
get {
return self.cellTypeToDraw
}
set(newValue) {
switch newValue {
case .cellTypeReceiver:
self.leftImage.isHidden = true
self.rightImage.isHidden = false
self.chatLabel.textAlignment = .right
self.chatLabel.roundCorners(corners: [.topLeft,.bottomLeft,.bottomRight], radius: 7.0)
case .cellTypeSender:
self.rightImage.isHidden = true
self.leftImage.isHidden = false
self.chatLabel.textAlignment = .left
self.chatLabel.roundCorners(corners: [.topRight,.bottomLeft,.bottomRight], radius: 7.0)
}
self.leftImage.layer.cornerRadius = self.leftImage.bounds.size.width/2
self.rightImage.layer.cornerRadius = self.rightImage.bounds.size.width/2
// self.chatLabel.sizeToFit()
// self.chatLabel.autoresizingMask = .flexibleHeight
}
}
并像这样在 cellForRowAtIndexPath 中访问这个变量。
if indexPath.row % 2 != 0 {
cell.cellTypeToDraw = .cellTypeReceiver
}
else {
cell.cellTypeToDraw = .cellTypeSender
}
【问题讨论】:
-
我知道有一个
UITableViewCell,如果有,那么你是否实现了必要的约束让它根据里面的文字增长? -
是的。我已经提到它是 tableCell。
-
你需要给UILabel添加边距checkout this question
-
如果有帮助,您可以采用此库中的圆角方法:github.com/appsquickly/NanoFrame。 .这是旧的 ObjC 代码。
-
尝试在为角落收音机遮罩之前添加文本。