         return false
     }
     
-    override func sizeThatFits(size: CGSize) -> CGSize {
-        var newSize = super.sizeThatFits(size)
-        newSize.width += 20.0 + borderWidth
-        newSize.height += 15.0 + borderWidth
-        return newSize
+    override func sizeToFit() {
+        self.valueLabel.sizeToFit()
+        let valueLabelSize = self.valueLabel.frame.size
+        var newSize = valueLabelSize
+        
+        let verticalMargin: CGFloat = 7.0 + borderWidth
+        let horizontalMargin: CGFloat = 7.0 + borderWidth
+        let equationSpace: CGFloat = 2.0
+        
+        if let equationView = self.equationView {
+            if let equationSize = self.equationViewSize {
+                equationView.frame.size = equationSize
+                
+                newSize.width = max(newSize.width, equationSize.width)
+                newSize.height += equationSpace + equationSize.height
+            }
+        }
+
+        newSize.width += horizontalMargin * 2
+        newSize.height += verticalMargin * 2
+        
+        self.valueLabel.center = CGPointMake(newSize.width / 2, verticalMargin + valueLabelSize.height / 2)
+        if let equationView = self.equationView {
+            equationView.center = CGPointMake(newSize.width / 2,
+                verticalMargin + valueLabelSize.height + equationSpace + equationView.frame.size.height / 2)
+        }
+        
+        self.frame.size = newSize
     }
 }
 

    
    let borderWidth: CGFloat = 3
    private func connectorLabelInitialize() {
        self.font = UIFont.boldSystemFontOfSize(22)
        self.layer.borderWidth = borderWidth
        self.layer.cornerRadius = 12
        self.textAlignment = .Center
        self.layer.masksToBounds = true
        self.hasError = false
    }
    
    var isSelected: Bool = false {
        didSet {
            if self.isSelected {
                self.backgroundColor = UIColor(white: 0.45, alpha: 1.0)
                self.textColor = UIColor.whiteColor()
            } else {
                self.backgroundColor = UIColor.whiteColor()
                self.textColor = UIColor.grayColor()
            }
        }
    }


import UIKit
import DigitRecognizerSDK
import WebKit

// Views that contain and draw Connector, Adder, or Multiplier. Their center stays the same, but otherwise they can rotate and resize to fit

class ConnectorLabel: UIView, WKScriptMessageHandler {
    let valueLabel: UILabel = UILabel()
    var scale: Double = 1
    let connector: Connector
    var isDependent: Bool = false
    var isPercent: Bool = false
    var equationView: WKWebView?
    var equationViewSize: CGSize?

    init(connector: Connector) {
        self.connector = connector
