您可以简单地继承 UIButton,并在其中实现自定义绘图。 UIButton 是在不干扰 tableview 触摸的情况下完成任务的最简单方法。如果在表格视图上实现点击手势,则会导致单元格触摸出现问题。
您可以简单地获取自定义图像(+ 符号和文本作为一个图像)并将其用作背景图像。
或者你甚至可以用代码来绘制它。
例如,你可以试试这个:
func drawCanvas1(frame frame: CGRect = CGRectMake(3, 8, 209, 109)) {
//// General Declarations
let context = UIGraphicsGetCurrentContext()
//// Color Declarations
let color = UIColor(red: 0.967, green: 0.423, blue: 0.211, alpha: 1.000)
//// Image Declarations
let screenShot20151111At32900PM = UIImage(named: "screenShot20151111At32900PM.png")!
//// Rectangle Drawing
let rectanglePath = UIBezierPath(roundedRect: CGRectMake(frame.minX + 39, frame.minY + 23, 113, 46), cornerRadius: 8)
color.setFill()
rectanglePath.fill()
//// Rectangle 2 Drawing
let rectangle2Rect = CGRectMake(frame.minX + 51, frame.minY + 27, 33, 34)
let rectangle2Path = UIBezierPath(rect: rectangle2Rect)
CGContextSaveGState(context)
rectangle2Path.addClip()
screenShot20151111At32900PM.drawInRect(CGRectMake(floor(rectangle2Rect.minX - 16 + 0.5), floor(rectangle2Rect.minY - 15 + 0.5), screenShot20151111At32900PM.size.width, screenShot20151111At32900PM.size.height))
CGContextRestoreGState(context)
//// Text Drawing
let textRect = CGRectMake(frame.minX + 97, frame.minY + 23, 73, 46)
let textTextContent = NSString(string: "\nfollow\ntrip\n")
let textStyle = NSParagraphStyle.defaultParagraphStyle().mutableCopy() as! NSMutableParagraphStyle
textStyle.alignment = .Left
let textFontAttributes = [NSFontAttributeName: UIFont.systemFontOfSize(UIFont.labelFontSize()), NSForegroundColorAttributeName: UIColor.whiteColor(), NSParagraphStyleAttributeName: textStyle]
let textTextHeight: CGFloat = textTextContent.boundingRectWithSize(CGSizeMake(textRect.width, CGFloat.infinity), options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: textFontAttributes, context: nil).size.height
CGContextSaveGState(context)
CGContextClipToRect(context, textRect);
textTextContent.drawInRect(CGRectMake(textRect.minX, textRect.minY + (textRect.height - textTextHeight) / 2, textRect.width, textTextHeight), withAttributes: textFontAttributes)
CGContextRestoreGState(context)
}
结果会是这样的: