Magnetic icon indicating copy to clipboard operation
Magnetic copied to clipboard

Set size of bubble based on size of content

Open Walker123t opened this issue 4 years ago • 1 comments

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

Walker123t avatar Feb 27 '20 07:02 Walker123t

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

jesster2k10 avatar Mar 30 '20 17:03 jesster2k10