Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set size of bubble based on size of content #87

Open
Walker123t opened this issue Feb 27, 2020 · 1 comment
Open

Set size of bubble based on size of content #87

Walker123t opened this issue Feb 27, 2020 · 1 comment

Comments

@Walker123t
Copy link

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

Environment

  • iOS Version: 13.3
  • Device(s): iPhone 11 Pro
@jesster2k10
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants