             } else {
                 x = padding + maxWidth - size.width
             }
-            textRect = CGRect(x: x,
+            textRect = CGRect(x: min(x, maxX),
                               y: arrowRect.maxY + spacing,
                               width: size.width,
                               height: size.height)

            } else {
                x = padding + maxWidth - size.width
            }
            textRect = CGRect(x: x,
                              y: arrowRect.minY - spacing - size.height,
                              width: size.width,
                              height: size.height)

            } else {
                x = padding
            }
            textRect = CGRect(x: min(x, maxX),
                              y: arrowRect.minY - spacing - size.height,
                              width: size.width,
                              height: size.height)
