Magnetic
Magnetic copied to clipboard
Set size of bubble based on size of content
An easier way to set the size of a bubble based on the content I put in. The way I am doing it now is creating a label, calculating its intrinsic content size, then setting the radius based off of that.
New Issue Checklist
- [ x] I updated Magnetic to the latest version.
- [x ] I read the Contribution Guidelines.
- [ x] I read the documentation.
- [ x] I searched for existing GitHub issues.
Environment
- iOS Version: 13.3
- Device(s): iPhone 11 Pro
This is my (hacky) solution:
func resizeBubble() {
let defaultFontName = fontName ?? "AvenirNext-Medium"
let defaultFontSize = fontSize ?? 13
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 = 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(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
}()
}
It would be possible to create a super class called ResizableNode
that extends the Node
class and implement this code and make a PR mabye