在native层 是否可以获取文件宽度?

当我在native层,使用cavans画字时。我需要提前得知文本画在画布上的长和宽,以确定bitmap的长宽,或者做一些其它的处理。但是我发现,获取文本的长,相应的api是存在的OH\_Drawing\_TypographyGetHeight,获取文本宽度却没有

阅读 580
1 个回答
DRAWING_LOGE("renhwndk NapiTextEngin SampleBitMap::DrawText() in");

OH_Drawing_TypographyStyle *typoStyle = OH_Drawing_CreateTypographyStyle();
OH_Drawing_SetTypographyTextDirection(typoStyle, TEXT_DIRECTION_LTR);
OH_Drawing_SetTypographyTextAlign(typoStyle, TEXT_ALIGN_LEFT);

OH_Drawing_TextStyle *txtStyle = OH_Drawing_CreateTextStyle();
OH_Drawing_SetTextStyleColor(txtStyle, OH_Drawing_ColorSetArgb(0xFF, 0x00, 0x00, 0xff));

OH_Drawing_SetTextStyleFontSize(txtStyle, 50);
OH_Drawing_SetTextStyleFontWeight(txtStyle, FONT_WEIGHT_400);
OH_Drawing_SetTextStyleBaseLine(txtStyle, TEXT_BASELINE_ALPHABETIC);
OH_Drawing_SetTextStyleFontHeight(txtStyle, 1);

const char *fontFamilies[] = {"Roboto"};
OH_Drawing_SetTextStyleFontFamilies(txtStyle, 1, fontFamilies);
OH_Drawing_SetTextStyleFontStyle(txtStyle, FONT_STYLE_NORMAL);
OH_Drawing_SetTextStyleLocale(txtStyle, "en");

OH_Drawing_TypographyCreate *handler =
  OH_Drawing_CreateTypographyHandler(typoStyle, OH_Drawing_CreateFontCollection());
OH_Drawing_TypographyHandlerPushTextStyle(handler, txtStyle);


OH_Drawing_TypographyHandlerAddText(handler, " hello world");
OH_Drawing_TypographyHandlerPopTextStyle(handler);
OH_Drawing_Typography *typography = OH_Drawing_CreateTypography(handler);

double maxWidth = 600;
OH_Drawing_TypographyLayout(typography, maxWidth);

double width = OH_Drawing_TypographyGetLineWidth(typography, 0);
DRAWING_LOGE("renhwndk OH_Drawing_TypographyGetLineWidth == %{public}f",width);
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进