Skip to content

Commit

Permalink
fix(ios): allow on the fly customisation
Browse files Browse the repository at this point in the history
  • Loading branch information
Jesse Onolememen committed Mar 30, 2020
1 parent 0620497 commit 8928600
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 31 deletions.
77 changes: 46 additions & 31 deletions ios/RNBubbleSelectNode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,52 +51,58 @@ class RNBubbleSelectNodeView: UIView {
color: color ?? .black,
radius: radius ?? 30
)
return node
}()

override func didUpdateReactSubviews() {
updateNode()
}

func updateNode() {
node.id = id
node.label.fontName = fontName ?? "AvenirNext-Medium"
node.label.fontSize = fontSize ?? 13
node.label.fontColor = fontColor
node.label.fontColor = fontColor ?? .white
node.label.lineHeight = lineHeight
node.strokeColor = borderColor ?? .black
node.lineWidth = borderWidth ?? 0
return node
}()
node.color = color ?? .black
node.text = text

if let radius = radius {
set(radius: radius)
}
}

func resizeBubble() {
private func set(radius: CGFloat, width: CGFloat? = nil) {
guard let path = SKShapeNode(circleOfRadius: radius).path else { return }
node.path = path
node.label.width = width ?? radius
node.physicsBody = {
let marginScale = CGFloat(self.marginScale ?? 1.01)
var transform = CGAffineTransform.identity.scaledBy(x: marginScale, y: marginScale)
let body = SKPhysicsBody(polygonFrom: path.copy(using: &transform)!)
body.allowsRotation = false
body.friction = 0
body.linearDamping = 3
return body
}()
}

private func resizeBubble() {
let defaultFontName = fontName ?? "AvenirNext-Medium"
let defaultFontSize = fontSize ?? 13
if let radius = radius {
guard let path = SKShapeNode(circleOfRadius: radius).path else { return }
node.path = path
node.label.width = radius
node.physicsBody = {
let marginScale = CGFloat(self.marginScale ?? 1.01)
var transform = CGAffineTransform.identity.scaledBy(x: marginScale, y: marginScale)
let body = SKPhysicsBody(polygonFrom: path.copy(using: &transform)!)
body.allowsRotation = false
body.friction = 0
body.linearDamping = 3
return body
}()
return

if (radius != nil) {
return
}

if let text = text, let font = UIFont(name: defaultFontName, size: defaultFontSize) {
let fontAttributes = [NSAttributedString.Key.font: font]
let size = (text as NSString).size(withAttributes: fontAttributes)
let padding = self.padding ?? 20
let radius = size.width / 2 + CGFloat(padding)
guard let path = SKShapeNode(circleOfRadius: radius).path else { return }
node.path = path
node.label.width = size.width
node.physicsBody = {
let marginScale = CGFloat(self.marginScale ?? 1.01)
var transform = CGAffineTransform.identity.scaledBy(x: marginScale, y: marginScale)
let body = SKPhysicsBody(polygonFrom: path.copy(using: &transform)!)
body.allowsRotation = false
body.friction = 0
body.linearDamping = 3
return body
}()
set(radius: radius, width: size.width)
}

}
Expand All @@ -110,20 +116,23 @@ extension RNBubbleSelectNodeView {

@objc func setText(_ text: String?) {
self.text = text
updateNode()
resizeBubble()
}

@objc func setImage(_ image: UIImage?) {
self.image = image
updateNode()
}

@objc func setColor(_ color: UIColor?) {
self.color = color
updateNode()
}

@objc func setRadius(_ radius: CGFloat) {
self.radius = radius
resizeBubble()
updateNode()
}

@objc func setMarginScale(_ marginScale: CGFloat) {
Expand All @@ -133,28 +142,34 @@ extension RNBubbleSelectNodeView {
// Label Styling
@objc func setFontSize(_ fontSize: CGFloat) {
self.fontSize = fontSize
updateNode()
resizeBubble()
}

@objc func setFontName(_ fontName: String?) {
self.fontName = fontName
updateNode()
resizeBubble()
}

@objc func setFontColor(_ fontColor: UIColor?) {
self.fontColor = fontColor
updateNode()
}

@objc func setLineHeight(_ lineHeight: CGFloat) {
self.lineHeight = lineHeight
updateNode()
}

@objc func setBorderColor(_ color: UIColor?) {
self.borderColor = color
updateNode()
}

@objc func setBorderWidth(_ width: CGFloat) {
self.borderWidth = width
updateNode()
}

@objc func setPadding(_ padding: CGFloat) {
Expand Down
1 change: 1 addition & 0 deletions ios/RNBubbleSelectNodeViewManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class RNBubbleSelectNodeViewManager: RCTViewManager {
node.fontSize = fontSize
node.fontColor = fontColor ?? .white
node.lineHeight = lineHeight ?? 1.1
node.updateNode()
return node
}
}
Expand Down

0 comments on commit 8928600

Please sign in to comment.