1# 使用主题字体(C/C++) 2 3 4## 场景介绍 5 6主题字体,特指系统**主题应用**中能使用的字体,属于一种特殊的自定义字体。可以通过相关接口调用使能主题应用中的主题字体。 7 8 9## 实现机制 10 11 12 13针对主题字的切换使用,应用方应确保订阅主题字变更事件,当接收字体变更事件后,由应用方主动调用页面刷新才能实现主题字的切换,否则主题字只能在重启应用后才生效;主题字的绘制需要使用OH_Drawing_GetFontCollectionGlobalInstance来获取全局字体集对象,仅该接口返回的对象拥有主题字体信息。 14 15> **说明:** 16> 17> 由OH_Drawing_CreateSharedFontCollection创建的字体集对象不包含主题字信息,无法用于绘制主题字。 18 19 20## 接口说明 21 22注册使用主题字体的常用接口如下表所示,详细接口说明请参考[Drawing](../reference/apis-arkgraphics2d/_drawing.md)。 23 24| 接口名 | 描述 | 25| -------- | -------- | 26| OH_Drawing_FontCollection\* OH_Drawing_GetFontCollectionGlobalInstance(void) | 获取全局的字体集对象OH_Drawing_FontCollection。 | 27| [onConfigurationUpdate()](../reference/apis-ability-kit/js-apis-app-ability-ability.md#abilityonconfigurationupdate) | 系统配置更新时调用。<br/>主题应用当前仅提供ArkTS接口发布变更事件,需要应用自行处理进行跨语言调用。 | 28 29 30## 开发步骤 31 321. 请确保在设备系统**主题应用**中,能成功应用一项主题字体。 33 342. 在应用入口文件(默认工程中为EntryAbility.ets)中复写onConfigurationUpdate函数,以响应fontId变更,适配主题字体的切换和页面刷新。 35 36 ```c++ 37 // entryability/EntryAbility.ets 38 export default class EntryAbility extends UIAbility { 39 // ... 40 preFontId =""; 41 onConfigurationUpdate(newConfig: Configuration):void{ 42 let fontId = newConfig.fontId; 43 if(fontId && fontId !=this.preFontId){ 44 this.preFontId = fontId; 45 // 调用C++代码 46 } 47 } 48 // ... 49 }; 50 ``` 51 52 newConfig变化时,会自动触发onConfigurationUpdate函数。应用可从发送的配置信息获取fontId,通过判断是否与应用本地保存的fontId一致来识别主题字的切换。若不一致则刷新本地fontId,并调用C++代码刷新排版结果。 53 543. 本步骤及之后均为主题字体在C++侧的使用,从ArkTS到C++的调用通路需应用根据实际情况选取调用方式,本示例不作推荐。 55 56 导入头文件。 57 58 ```c++ 59 #include <native_drawing/drawing_font_collection.h> 60 #include <native_drawing/drawing_text_typography.h> 61 #include <native_drawing/drawing_register_font.h> 62 #include "common/log_common.h" 63 ``` 64 654. 创建字体管理器。 66 67 > **说明:** 68 > 69 > 注册主题字体作用于字体管理集全局对象,故必须使用OH_Drawing_GetFontCollectionGlobalInstance获取全局字体集对象进行绘制。如若使用OH_Drawing_CreateSharedFontCollection或OH_Drawing_CreateFontCollection创建字体集对象,无法使用主题字体。OH_Drawing_GetFontCollectionGlobalInstance获取的全局字体集不允许释放,释放会造成字体绘制紊乱问题。 70 71 ```c++ 72 OH_Drawing_FontCollection *fontCollection = OH_Drawing_GetFontCollectionGlobalInstance(); 73 ``` 74 755. OH_Drawing_SetTextStyleFontFamilies()接口可以用来指定familyName,从而实现使用指定字体。但使用主题字体,不需要使用OH_Drawing_SetTextStyleFontFamilies()接口指定字体,否则行为变更为优先使用指定字体,而不是主题字体。 76 77 ```c++ 78 OH_Drawing_TextStyle textStyle = OH_Drawing_CreateTextStyle(); 79 // const char* myFontFamilies[] = {"otherFontFamilyName"}; 80 // 注意不要使用此接口来指定字体 81 // OH_Drawing_SetTextStyleFontFamilies(textStyle, 1, myFontFamilies); 82 ``` 83 846. 设置段落文本内容为"Hello World. \nThis is the theme font.",此时该段落文本将应用主题字体。 85 86 ```c++ 87 // 设置其他文本样式 88 OH_Drawing_SetTextStyleColor(textStyle , OH_Drawing_ColorSetArgb(0xFF, 0x00, 0x00, 0x00)); 89 OH_Drawing_SetTextStyleFontSize(textStyle , 70.0); 90 // 创建一个段落样式对象,以设置排版风格 91 OH_Drawing_TypographyStyle *typographyStyle = OH_Drawing_CreateTypographyStyle(); 92 OH_Drawing_SetTypographyTextAlign(typographyStyle, TEXT_ALIGN_LEFT); // 设置段落样式为左对齐 93 // 创建一个段落生成器 94 OH_Drawing_TypographyCreate* handler = OH_Drawing_CreateTypographyHandler(typographyStyle, fontCollection); 95 // 在段落生成器中设置文本样式 96 OH_Drawing_TypographyHandlerPushTextStyle(handler, textStyle); 97 // 在段落生成器中设置文本内容 98 const char* text = "Hello World. \nThis is the theme font."; 99 OH_Drawing_TypographyHandlerAddText(handler, text); 100 // 通过段落生成器生成段落 101 OH_Drawing_Typography* typography = OH_Drawing_CreateTypography(handler); 102 ``` 103 104## 效果展示 105 106以下展示了在系统**主题应用**中切换使用不同主题字体后,对应的文字渲染效果。 107 108不同主题字体显示效果不同,此处仅示意。 109 110