[TIMOB-24424] Calculating of height of label without adding to parent view
GitHub Issue | n/a |
---|---|
Type | New Feature |
Priority | n/a |
Status | Open |
Resolution | Unresolved |
Affected Version/s | n/a |
Fix Version/s | n/a |
Components | n/a |
Labels | n/a |
Reporter | Rainer Schleevoigt |
Assignee | Unknown |
Created | 2017-02-21T15:49:45.000+0000 |
Updated | 2018-02-28T19:55:15.000+0000 |
Description
Hi,
Task: module for measurement (getHeight) of TiUILabel without adding to a parent.
I found
TiUIView createView(Activity activity)
will called after adding to parent view.
In a Kroll.method of proxy I call:
private float handleCalculateHeight() {
TiUILabel textView = new TiUILabel(this);
int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(300,View.MeasureSpec.AT_MOST);
int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
textView.getNativeView().measure(widthMeasureSpec, heightMeasureSpec);
float h = textView.getNativeView().getMeasuredHeight();
Log.d(LCAT, "HEIGHT=" + h);
return h;
}
The result is always 81.0. I think the (JS) options will not proceeded. Any ideas?
I figured the secret i the first paramter of this line:
Any hints?
Solved: ========== @Kroll.method public final float calculateHeight() { float height = 0f; if (TiApplication.isUIThread()) { Log.d(LCAT, "calculateHeight in UIthread"); height = handleCalculateHeight(); } else { TiMessenger.sendBlockingMainMessage(new Handler(TiMessenger .getMainMessenger().getLooper(), new Handler.Callback() { public boolean handleMessage(Message msg) { AsyncResult result = (AsyncResult) msg.obj; result.setResult(handleCalculateHeight()); return true; } }).obtainMessage(0), null); } return height; } private float handleCalculateHeight() { TiUILabel textView = new TiUILabel(this); textView.processProperties(opts); // don't forget ;-) TextView tv = (TextView) textView.getNativeView(); int specWidth = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); int specHeight = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); tv.setVisibility(View.VISIBLE); tv.measure(specWidth, specHeight); return tv.getMeasuredHeight(); } ==== Simple add this snippet in the end of LabelProxy.java and with the new method the user can get the calculated height of label.
Please create a PR of your request.
Is there a receipt/howto? A clone is on my machine …