@@ -933,24 +933,27 @@ export class Paint implements IPaint {
933
933
public setARGB ( a : number , r : number , g : number , b : number ) : void {
934
934
this . mColor = new Color ( a , r , g , b ) ;
935
935
}
936
- public measureText ( text : string , start = 0 , end ?) {
936
+ public measureText ( text : string | NSAttributedString , start = 0 , end ?) {
937
937
if ( end === undefined ) {
938
938
end = text . length ;
939
939
}
940
+ if ( text instanceof NSAttributedString ) {
941
+ const rect = new Rect ( ) ;
942
+ this . getTextBounds ( text , start , end , rect ) ;
943
+ return rect . width ( ) ;
944
+ }
940
945
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
+ }
954
957
}
955
958
public isAntiAlias ( ) : boolean {
956
959
return this . antiAlias ;
@@ -1961,11 +1964,10 @@ export class Canvas implements ICanvas {
1961
1964
}
1962
1965
const font = paint . getUIFont ( ) ;
1963
1966
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 ( ) ) ;
1969
1971
} else {
1970
1972
UIDrawingText . drawStringXYFontColor ( text , offsetx , offsety - font . ascender , font , color ) ;
1971
1973
}
0 commit comments