Created
December 12, 2014 15:03
-
-
Save Katafalkas/eb5e840df1ace981c359 to your computer and use it in GitHub Desktop.
Swift UITableViewCell custom subclass simple Chat Bubble
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Cell: UITableViewCell { | |
override func drawRect(rect: CGRect) { | |
var bubbleSpace = CGRectMake(20.0, self.bounds.origin.y, self.bounds.width - 20, self.bounds.height) | |
let bubblePath1 = UIBezierPath(roundedRect: bubbleSpace, byRoundingCorners: .TopLeft | .TopRight | .BottomRight, cornerRadii: CGSize(width: 20.0, height: 20.0)) | |
let bubblePath = UIBezierPath(roundedRect: bubbleSpace, cornerRadius: 20.0) | |
UIColor.greenColor().setStroke() | |
UIColor.greenColor().setFill() | |
bubblePath.stroke() | |
bubblePath.fill() | |
var triangleSpace = CGRectMake(0.0, self.bounds.height - 20, 20, 20.0) | |
var trianglePath = UIBezierPath() | |
var startPoint = CGPoint(x: 20.0, y: self.bounds.height - 40) | |
var tipPoint = CGPoint(x: 0.0, y: self.bounds.height - 30) | |
var endPoint = CGPoint(x: 20.0, y: self.bounds.height - 20) | |
trianglePath.moveToPoint(startPoint) | |
trianglePath.addLineToPoint(tipPoint) | |
trianglePath.addLineToPoint(endPoint) | |
trianglePath.closePath() | |
UIColor.greenColor().setStroke() | |
UIColor.greenColor().setFill() | |
trianglePath.stroke() | |
trianglePath.fill() | |
} | |
override init(style: UITableViewCellStyle, reuseIdentifier: String!) { | |
super.init(style: style, reuseIdentifier: reuseIdentifier) | |
} | |
required init(coder aDecoder: NSCoder) { | |
super.init(coder: aDecoder) | |
} | |
override func layoutSubviews() { | |
super.layoutSubviews() | |
// var backgroundImage = UIImageView(image: UIImage(named: "star")) | |
// backgroundImage.contentMode = UIViewContentMode.ScaleAspectFit | |
// self.backgroundView = backgroundImage | |
} | |
} |
bubblePath1 not used
Kindly update the latest version of swift 4.0 code
class ChatBubbleViewCell: UITableViewCell {
override func draw(_ rect: CGRect) {
var bubbleSpace = CGRect(x: 20.0,y: self.bounds.origin.y,width : self.bounds.width - 20,height : self.bounds.height)
let bubblePath1 = UIBezierPath(roundedRect: bubbleSpace, byRoundingCorners: [.topLeft,.topRight ,.bottomRight], cornerRadii: CGSize(width: 20.0, height: 20.0))
let bubblePath = UIBezierPath(roundedRect: bubbleSpace, cornerRadius: 20.0)
UIColor.green.setStroke()
UIColor.green.setFill()
bubblePath.stroke()
bubblePath.fill()
var triangleSpace = CGRect(x : 0.0, y : self.bounds.height - 20,width : 20,height : 20.0)
var trianglePath = UIBezierPath()
var startPoint = CGPoint(x: 20.0, y: self.bounds.height - 40)
var tipPoint = CGPoint(x: 0.0, y: self.bounds.height - 30)
var endPoint = CGPoint(x: 20.0, y: self.bounds.height - 20)
trianglePath.move(to: startPoint)
trianglePath.addLine(to: tipPoint)
trianglePath.addLine(to: endPoint)
trianglePath.close()
UIColor.green.setStroke()
UIColor.green.setFill()
trianglePath.stroke()
trianglePath.fill()
}
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String!) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
}
override func layoutSubviews() {
super.layoutSubviews()
// var backgroundImage = UIImageView(image: UIImage(named: "star"))
// backgroundImage.contentMode = UIViewContentMode.ScaleAspectFit
// self.backgroundView = backgroundImage
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for code! How to make the answer version? I mean with the triangle on the right.