Jump to content


Photo

SmartCL.Components MeasureText functions not working in chrome


  • Please log in to reply
3 replies to this topic

#1 recursiveElk

recursiveElk
  • Members
  • 36 posts

Posted 31 August 2017 - 05:36 AM

Hi,

I have found the functions as listed don't work in chrome but do work as expected in the internal browser.

example code:

procedure Test.Resize();
begin
inherited;
writeln(ErrorLabel.MeasureTextSize(errorlabel.handle,'my test').tmWidth);
MyLabel.width := ErrorLabel.MeasureText(ErrorLabel.Caption).tmWidth ;
end;

In chrome this outputs 0, and looks like this: https://puu.sh/xnKRo/df97c53ccb.png

but in SMS this will output 73 and display correctly (more or less): https://puu.sh/xnKPx/f596b44247.png

Please let me know if i'm doing something wrong!

EDIT:

Seems like further testing is needed, it works on chrome on my mobile.

#2 jarto

jarto
  • Administrators
  • 40 posts
  • LocationVaasa, Finland
  • Enterprise Edition

Posted 31 August 2017 - 01:23 PM

In general I would not use MeasureTextSize to set the width of a label. As a method it is pretty slow.

Before the new RTL is released, a quick and nice way to do this is to use a TW3CustomControl:

Lab:=TW3CustomControl.Create(Parent);
Lab.MoveTo(where,ever);
Lab.InnerText:='Whatever you want';
Lab.StyleClass:='TW3LabelText'; //Unless you have your own style

The trick is this: When you don't set the size yourself, the browser resizes it automatically.

In the new RTL you can simply set in Label.AutoSize:=True, but this same trick works there too.
  • recursiveElk likes this

#3 recursiveElk

recursiveElk
  • Members
  • 36 posts

Posted 31 August 2017 - 11:46 PM

Cheers for the info i didn't know this was the case! And yeah i assumed it would probably be a case of Chrome being 'too' fast as this seems to happen a lot haha

#4 Nico Wouterse

Nico Wouterse
  • Moderators
  • 250 posts
  • LocationAustralia

Posted 02 September 2017 - 07:44 AM

AFAIK desktop browsers always give a correct result using measuretext
However some mobile browsers reply with 0

status April 2017:
Android-Chrome, Android-Chrome-beta and Android-Chrome-dev do that,
Android-firefox is ok


The following workaround works in the current version of SMS (2.2.1.4522) :

  mObj:=TW3FontDetector.Create;

    W := mObj.MeasureText(mObj.getFontInfo(self.handle),
           Label.innerHtml).tmWidth;
    If W = 0 then
    // some mobile browsers report mObj.MeasureText as zero
    // so in that case use workaround
      W := JSMeasureText(Label.InnerHTML,16,0,'');

Function JSMeasureText(mText: String; mFontSize: Integer; mywidth: integer; mStyle: String) : Integer;
var
  S, S2 : String;
  W, H : Integer;
begin
  S := mText;
  S2 := inttostr(mywidth);
asm
function MyMeasureText(pText, pFontSize, pWidth, pStyle) {
    var lDiv = document.createElement('lDiv');

    document.body.appendChild(lDiv);

    lDiv.innerHTML = pText;

    if (pStyle != '') {
        lDiv.style = pStyle;
    }
    lDiv.style.fontSize = "" + pFontSize + "px";
    lDiv.style.position = "absolute";
    lDiv.style.left = -2000;
    lDiv.style.top = -2000;
    lDiv.style.width = "" + pWidth + "px";
    lDiv.style.float = "none";

    var lResult = {
        width: lDiv.clientWidth,
        height: lDiv.getBoundingClientRect().bottom
//        height: lDiv.clientHeight
    };

    document.body.removeChild(lDiv);
    lDiv = null;

    return lResult;
}

  • recursiveElk likes this
Nico Wouterse




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users