All you need to do is to adjust the clamping with the size of the object. Use the width and height properties of the UI elements's RectTransform (I've never actually done any Unity programming, so these might be referencing some old obsolete patterns, but I'm sure you could find out how to translate the code to use some other implementation).
Once you have your size, you need to convert it to the screen space (as you need to do for your object's origin), using something like
RectTransformUtility.WorldToScreenPoint (Again, could be an obsolete method or a better alternative available out there).
Finally, you need the viewport size, probably from
Screen.Width/Height. Once you have those, all that's left to do is to perform the actual clamping. We need to make sure that all the corners of the UI element are inside the viewport. So, we perform a clamping in the screen space, and finally convert the position back to world space and apply it back to the UI element (I'm not 100% sure about which space UI elements lie in by default, so I'll assume that they use world space coordinates).
origin.x = clamp(origin.x, objectWidth / 2.0, Screen.Width - objectWidth / 2.0);
origin.y = clamp(origin.y, objectHeight / 2.0, Screen.Height - objectHeight / 2.0);
The code above limits the origin to lie within the screen bounds with the object size taken into account. The origin has to be atleast one object half size away from the top-left of the screen
(objectWidth / 2.0, objectHeight / 2.0), and it can't exceed the screen size by the same amount.