     }
     
     private var contentFrame:CGRect {
-        let bFrame = self.bubbleFrame
-        let cFrame = CGRectMake(
-            bFrame.origin.x + cornerRadius,
-            bFrame.origin.y + cornerRadius,
-            bFrame.size.width - cornerRadius*2,
-            bFrame.size.height - cornerRadius*2
-        )
+        let cFrame = self.bubbleFrame.rectByInsetting(dx: cornerRadius, dy: cornerRadius)
         return cFrame
     }
     

            }
        }
        
        let p = targetView.superview!.convertPoint(targetView.center, toView: containerView)
        var x_p = p.x
        var x_b = x_p - round(bubbleSize.width * 0.5)
        
        if (x_b < sidePadding) {
            x_b = sidePadding;
        }
        if (x_b + bubbleSize.width + sidePadding > containerViewWidth) {
            x_b = containerViewWidth - bubbleSize.width - sidePadding;
        }
        if (x_p - pointerSize < x_b + cornerRadius) {
            x_p = x_b + cornerRadius + pointerSize;
        }
        if (x_p + pointerSize > x_b + bubbleSize.width - cornerRadius) {
            x_p = x_b + bubbleSize.width - cornerRadius - pointerSize;
        }
        
        let fullHeight = bubbleSize.height + pointerSize + 10
        var y_b = CGFloat(0)
        
        if (pointDirection == .Up) {
            y_b = topMargin + pointerY;
            targetPoint = CGPoint(x: x_p-x_b, y: 0);
        } else {
            y_b = pointerY - fullHeight;
            targetPoint = CGPoint(x: x_p-x_b, y: fullHeight-2.0);
        }
        
        var finalFrame = CGRect(
            x: x_b - sidePadding,
            y: y_b,
            width: bubbleSize.width + sidePadding * 2,
            height: fullHeight
        )

        var rectWidth = CGFloat(0)
        let containerViewWidth = containerView.bounds.size.width
		let containerViewHeight = containerView.bounds.size.height
        var maxWidthLimit:CGFloat = containerViewWidth - cornerRadius * 2
        var widthProportion:CGFloat!
        
        if UIDevice.currentDevice().userInterfaceIdiom == .Pad {
            widthProportion = 1/3
        } else {
            widthProportion = 2/3
        }
        
        if maxWidth > 0 {
            // If "maxWidth" is specified, we need to check to make sure
            // that it's valid, i.e. less than "maxWidthLimit"
            rectWidth = min(maxWidth, maxWidthLimit)
        } else {
            rectWidth = floor(containerViewWidth * widthProportion)
        }
        
        var contentSize = CGSizeZero
        if let customView = customView {
            contentSize = customView.frame.size
        } else {
            contentSize = titleAndMessageBoundingSize(width: rectWidth)
        }
        
        bubbleSize = CGSize(width: contentSize.width + cornerRadius * 2, height: contentSize.height + cornerRadius * 2)
        
        var superview:UIView! = containerView.superview
        assert(superview != nil, "The container view does not have a superview")
        if superview.isKindOfClass(UIWindow.self) {
            superview = containerView
        }
        
        assert(targetView.superview != nil, "The target view does not have a superview")
        var targetRelativeOrigin = targetView.superview!.convertPoint(targetView.frame.origin, toView: superview)
        var containerRelativeOrigin = superview.convertPoint(containerView.frame.origin, toView: superview)

