Skip to content

Instantly share code, notes, and snippets.

@kirillp
Created January 12, 2017 12:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kirillp/9928370d7f893ce6cbc912b4b8154e4d to your computer and use it in GitHub Desktop.
Save kirillp/9928370d7f893ce6cbc912b4b8154e4d to your computer and use it in GitHub Desktop.
package delightex.moe.nativecontrols;
import apple.coregraphics.c.CoreGraphics;
import apple.coregraphics.struct.CGRect;
import apple.uikit.UITextField;
import apple.uikit.struct.UIEdgeInsets;
import org.moe.natj.general.Pointer;
import org.moe.natj.general.ann.ByValue;
import org.moe.natj.general.ann.Generated;
import org.moe.natj.general.ann.Runtime;
import org.moe.natj.objc.ObjCRuntime;
import org.moe.natj.objc.ann.ObjCClassName;
import org.moe.natj.objc.ann.Selector;
@Runtime(ObjCRuntime.class)
@ObjCClassName("CustomTextField")
class CustomTextField extends UITextField {
private UIEdgeInsets myPaddingInsets;
protected CustomTextField(Pointer peer) {
super(peer);
System.out.println("CustomTextField.ctor");
}
@Selector("alloc")
@Generated
public static native CustomTextField alloc();
@Selector("init")
@Generated
public native CustomTextField init();
@Selector("editingRectForBounds:")
@ByValue
@Override
public CGRect editingRectForBounds(@ByValue CGRect cgRect) {
System.out.println("CustomTextField.editingRectForBounds enter");
CGRect rect = myPaddingInsets != null ? inset(cgRect, myPaddingInsets) : cgRect;
System.out.println("CustomTextField.editingRectForBounds leave, rect = " + rect);
return rect;
}
private static CGRect inset(CGRect rect, UIEdgeInsets insets) {
return CoreGraphics.CGRectMake(
rect.origin().x() + insets.left(),
rect.origin().y() + insets.top(),
rect.size().width() - (insets.left() + insets.right()),
rect.size().height() - (insets.top() + insets.bottom())
);
}
@Selector("textRectForBounds:")
@ByValue
@Generated
@Override
public CGRect textRectForBounds(@ByValue CGRect cgRect) {
System.out.println("CustomTextField.textRectForBounds enter");
CGRect rect = myPaddingInsets != null ? inset(cgRect, myPaddingInsets) : cgRect;
System.out.println("CustomTextField.textRectForBounds leave, rect = " + rect);
return rect;
}
void setPadding(double top, double right, double bottom, double left) {
myPaddingInsets = new UIEdgeInsets(top, left, bottom, right);
setNeedsLayout();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment