I need the Cocoa method to get _floating number_ width of a given string, ie Double value. I suggest to add to Cocoa. I tried this code, and my demo crashes (I cannot debug it yet, problem with fpcupdeluxe on macOS)
Pls correct my code and add it to Laz.
1) cocoawinapi.inc
function TCocoaWidgetSet.GetTextExtentPointF(DC: HDC; Str: PChar; Count: Integer; out AWidth, AHeight: Double): Boolean;
var
ctx : TCocoaContext;
begin
{$IFDEF VerboseWinAPI}
DebugLn('[TCocoaWidgetSet.GetTextExtentPointF] DC: %x Str: %s Count: %d', [DC, Str, Count]);
{$ENDIF}
ctx:=CheckDC(DC);
Result:=Assigned(ctx);
if not Assigned(ctx) then Exit(False);
Result := ctx.GetTextExtentPointF(Str, Count, AWidth, AHeight);
{$IFDEF VerboseWinAPI}
DebugLn('[TCocoaWidgetSet.GetTextExtentPointF] Size: %d,%d', [Size.cx, Size.cy]);
{$ENDIF}
end;
2) cocoagdiobjects.pas
Copy-paste from GetTextExtentPoint!
function TCocoaContext.GetTextExtentPointF(AStr: PChar; ACount: Integer; out AWidth, AHeight: Double): Boolean;
var
s : NSString;
M : NSMutableAttributedString;
r : NSRect;
begin
AWidth := 0;
AHeight := 0;
{FText.SetText(AStr, ACount);
Size := FText.GetSize;
Result := True;}
S := NSString( CFStringCreateWithBytesNoCopy(nil, AStr, ACount, kCFStringEncodingUTF8,
false,
kCFAllocatorNull));
Result := Assigned(S);
if not Result then Exit;
M := NSMutableAttributedString.alloc.initWithString(S);
Result := Assigned(M);
if Result then
begin
FText.SetFontToStr(M);
r := M.boundingRectWithSize_options(NSMakeSize(MaxInt, MaxInt), 0);
AWidth := r.size.width;
AHeight := r.Size.height;
M.release;
end;
CFRelease(S);
end;
3) demo to test it. Zip.