Skip to content

Commit 67ad68e

Browse files
committed
fix(ios): allow to drawText with NSAttributedString
1 parent 7afa2ed commit 67ad68e

File tree

1 file changed

+21
-19
lines changed

1 file changed

+21
-19
lines changed

Diff for: src/canvas.ios.ts

+21-19
Original file line numberDiff line numberDiff line change
@@ -933,24 +933,27 @@ export class Paint implements IPaint {
933933
public setARGB(a: number, r: number, g: number, b: number): void {
934934
this.mColor = new Color(a, r, g, b);
935935
}
936-
public measureText(text: string, start = 0, end?) {
936+
public measureText(text: string | NSAttributedString, start = 0, end?) {
937937
if (end === undefined) {
938938
end = text.length;
939939
}
940+
if (text instanceof NSAttributedString) {
941+
const rect = new Rect();
942+
this.getTextBounds(text, start, end, rect);
943+
return rect.width();
944+
}
940945
return UIDrawingText.measureTextFromToAttributes(text, start, end, this.getDrawTextAttribs());
941-
// const result = NSString.stringWithString(text.slice(start, end)).sizeWithFont(this.getUIFont());
942-
// return result.width;
943-
}
944-
public getTextBounds(text: string, start: number, end: number, rect: Rect): void {
945-
const cgrect = UIDrawingText.getTextBoundsFromToAttributes(text, start, end, this.getDrawTextAttribs());
946-
// const cgrect = NSString.stringWithString(text.slice(start, end)).boundingRectWithSizeOptionsAttributesContext(
947-
// CGSizeMake(Number.MAX_VALUE, Number.MAX_VALUE),
948-
// NSStringDrawingOptions.UsesDeviceMetrics,
949-
// this.getDrawTextAttribs(),
950-
// null
951-
// );
952-
// rect.cgRect = CGRectMake(0, -cgrect.size.height, cgrect.size.width, cgrect.size.height);
953-
rect.cgRect = cgrect;
946+
}
947+
public getTextBounds(text: string | NSAttributedString, start: number, end: number, rect: Rect): void {
948+
if (text instanceof NSAttributedString) {
949+
rect.cgRect = applyAttributesToNSAttributedString(text, this.getDrawTextAttribs()).boundingRectWithSizeOptionsContext(
950+
CGSizeMake(Number.MAX_SAFE_INTEGER, Number.MAX_SAFE_INTEGER),
951+
NSStringDrawingOptions.UsesDeviceMetrics,
952+
null
953+
);
954+
} else {
955+
rect.cgRect = UIDrawingText.getTextBoundsFromToAttributes(text, start, end, this.getDrawTextAttribs());
956+
}
954957
}
955958
public isAntiAlias(): boolean {
956959
return this.antiAlias;
@@ -1961,11 +1964,10 @@ export class Canvas implements ICanvas {
19611964
}
19621965
const font = paint.getUIFont();
19631966
const color = paint.getUIColor();
1964-
if (paint.letterSpacing !== undefined) {
1965-
const attribs = paint.getDrawTextAttribs();
1966-
UIDrawingText.drawStringXYWithAttributes(text, offsetx, offsety - font.ascender, attribs);
1967-
} else if (text instanceof NSAttributedString) {
1968-
UIDrawingText.drawAttributedStringXYFontColor(text, offsetx, offsety - font.ascender, font, color);
1967+
if (text instanceof NSAttributedString) {
1968+
UIDrawingText.drawAttributedStringXYFontColor(applyAttributesToNSAttributedString(text, paint.getDrawTextAttribs()), offsetx, offsety - font.ascender, font, color);
1969+
} else if (paint.letterSpacing !== undefined) {
1970+
UIDrawingText.drawStringXYWithAttributes(text, offsetx, offsety - font.ascender, paint.getDrawTextAttribs());
19691971
} else {
19701972
UIDrawingText.drawStringXYFontColor(text, offsetx, offsety - font.ascender, font, color);
19711973
}

0 commit comments

Comments
 (0)