如何在 UITableViewCell 中创建一个圆形?
How to create a circle shape within UITableViewCell?
如何在 table 单元格上创建圆形图像?例如:
这是我们的故事板,箭头指向我们想要突出显示该行时尝试绘制圆圈的位置:
更新:
决定使用自定义字体并关联 Swift library 来执行此操作。正如 Leo 指出的那样,当您可以使用图像(或字体)时生成一个圆圈是过大的。就像添加新标签和设置 font/icon:
一样简单
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
..
if doHighlight {
cell.statusLabel.setFAIcon(icon: .FACircle, iconSize: 6)
}
..
添加一个 UIView 实例并将其 layer.cornerRadius
设置为 width/height 的一半。
如果高度和宽度 = 50
View.clipsToBounds = true
View.layer.cornerRadius= View.frame.size.width/2
class CircularImageView: UIImageView {
override func layoutSubviews() {
super.layoutSubviews()
let radius: CGFloat = self.bounds.size.width / 2.0
self.layer.cornerRadius = radius
self.clipsToBounds = true
} }
你也可以制作一个圆形图像视图,它是 UIImageView 的子类,这样你就可以直接将它设置为 UIImageView...
您可以制作一个添加 CAShapeLayer
带圆圈的视图:
@IBDesignable class CircleView: UIView {
@IBInspectable public var fillColor: UIColor = #colorLiteral(red: 0, green: 0, blue: 1, alpha: 1) { didSet { shapeLayer.fillColor = fillColor.cgColor } }
@IBInspectable public var strokeColor: UIColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 0) { didSet { shapeLayer.strokeColor = strokeColor.cgColor } }
@IBInspectable public var lineWidth: CGFloat = 0 { didSet { setNeedsLayout() } }
lazy private var shapeLayer: CAShapeLayer = {
let _shapeLayer = CAShapeLayer()
_shapeLayer.fillColor = self.fillColor.cgColor
_shapeLayer.strokeColor = self.strokeColor.cgColor
self.layer.insertSublayer(_shapeLayer, at: 0)
return _shapeLayer
}()
override func layoutSubviews() {
super.layoutSubviews()
let center = CGPoint(x: bounds.midX, y: bounds.midY)
let radius = (min(bounds.size.width, bounds.size.height) - lineWidth) / 2
shapeLayer.path = UIBezierPath(arcCenter: center, radius: radius, startAngle: 0, endAngle: .pi * 2, clockwise: true).cgPath
shapeLayer.lineWidth = lineWidth
}
}
请注意,那是 @IBDesignable
,因此您将能够在 Interface Builder 中使用它。只需创建一个单独的目标 ("File" - "New" - "Target...") 并选择 "Cocoa Touch Framework" 并为其命名(例如,如果您的应用是 "Foo",您会调用这个框架 "FooKit")。然后把上面的UIView
subclass添加到那个framework中,在IB中就可以引用这个class(只需要添加一个UIView
对象,把它的base改成class至 CircleView
):
最有效的方法是添加圆形图像。设置 cornerRadius
会起作用,但它会消耗性能,而且对于简单的彩色圆圈来说有点矫枉过正。
如何在 table 单元格上创建圆形图像?例如:
这是我们的故事板,箭头指向我们想要突出显示该行时尝试绘制圆圈的位置:
更新:
决定使用自定义字体并关联 Swift library 来执行此操作。正如 Leo 指出的那样,当您可以使用图像(或字体)时生成一个圆圈是过大的。就像添加新标签和设置 font/icon:
一样简单override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
..
if doHighlight {
cell.statusLabel.setFAIcon(icon: .FACircle, iconSize: 6)
}
..
添加一个 UIView 实例并将其 layer.cornerRadius
设置为 width/height 的一半。
如果高度和宽度 = 50
View.clipsToBounds = true
View.layer.cornerRadius= View.frame.size.width/2
class CircularImageView: UIImageView {
override func layoutSubviews() {
super.layoutSubviews()
let radius: CGFloat = self.bounds.size.width / 2.0
self.layer.cornerRadius = radius
self.clipsToBounds = true
} }
你也可以制作一个圆形图像视图,它是 UIImageView 的子类,这样你就可以直接将它设置为 UIImageView...
您可以制作一个添加 CAShapeLayer
带圆圈的视图:
@IBDesignable class CircleView: UIView {
@IBInspectable public var fillColor: UIColor = #colorLiteral(red: 0, green: 0, blue: 1, alpha: 1) { didSet { shapeLayer.fillColor = fillColor.cgColor } }
@IBInspectable public var strokeColor: UIColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 0) { didSet { shapeLayer.strokeColor = strokeColor.cgColor } }
@IBInspectable public var lineWidth: CGFloat = 0 { didSet { setNeedsLayout() } }
lazy private var shapeLayer: CAShapeLayer = {
let _shapeLayer = CAShapeLayer()
_shapeLayer.fillColor = self.fillColor.cgColor
_shapeLayer.strokeColor = self.strokeColor.cgColor
self.layer.insertSublayer(_shapeLayer, at: 0)
return _shapeLayer
}()
override func layoutSubviews() {
super.layoutSubviews()
let center = CGPoint(x: bounds.midX, y: bounds.midY)
let radius = (min(bounds.size.width, bounds.size.height) - lineWidth) / 2
shapeLayer.path = UIBezierPath(arcCenter: center, radius: radius, startAngle: 0, endAngle: .pi * 2, clockwise: true).cgPath
shapeLayer.lineWidth = lineWidth
}
}
请注意,那是 @IBDesignable
,因此您将能够在 Interface Builder 中使用它。只需创建一个单独的目标 ("File" - "New" - "Target...") 并选择 "Cocoa Touch Framework" 并为其命名(例如,如果您的应用是 "Foo",您会调用这个框架 "FooKit")。然后把上面的UIView
subclass添加到那个framework中,在IB中就可以引用这个class(只需要添加一个UIView
对象,把它的base改成class至 CircleView
):
最有效的方法是添加圆形图像。设置 cornerRadius
会起作用,但它会消耗性能,而且对于简单的彩色圆圈来说有点矫枉过正。