Weex 獲取組件的 CSS 樣式

2023-12-27 17:18 更新

1.在 0.19 版本之前,Weex 使用 Yoga 排版引擎,可以通過訪問 WXComponent 的 cssNode 屬性獲取。例如:

self.cssNode->style.flex = 1.0;
float height = self.cssNode->style.dimensions[CSS_HEIGHT];

2.從 0.19 版本開始,Weex 使用自已的排版引擎,是 C++ 代碼。獲取 CSS 屬性可以從 WXComponent 的 styles 字典(NSDictionary)里獲取,也可以訪問 WXComponent 的 flexCssNode 屬性,其類型為 C++ 類 WeexCore::WXCoreLayoutNode。注意,需要源文件為 .mm 類型。

3.從 0.20 版本開始,iOS 接入 WeexCore,排版引擎也沉入 WeexCore層,并且 CSS 相關(guān)屬性不再上傳給 WXComponent 對象,WXComponent 對象的 styles 字典里只有非 CSS 樣式。我們的目的是上層 UI 組件只需要關(guān)心排版引擎生成的最終坐標即可,不需要關(guān)心前端標記的 CSS 屬性。如果仍然需要獲取 CSS 樣式,可以在 .mm 文件中直接訪問 flexCssNode,或通過 WXComponent+Layout.h 文件中提供的擴展方法。

/**
 * @abstract Get css style value for key. The key should be of CSS standard form.
 *  This method is for convenience use in C/ObjC environment. And if you want to
 *  retrieve all style values or in C++, you could use flexCssNode directly.
 *
 *  Thread usage:
 *      This method should be invoked in component thread by WXPerformBlockOnComponentThread.
 *      Note that all initWithRef methods of WXComponent and its subclasses are performed in
 *      component thread by default. Therefore you can call this method directly in initWithRef.
 *
 *  Supported keys:
 *      width, height, min-width, min-height, max-width, max-height,
 *      margin-(left/right/top/bottom)
 *      padding-(left/right/top/bottom)
 *      border-(left/right/top/bottom)-width
 *      left, right, top, bottom
 *      flex-grow
 */
- (float)getCssStyleValueForKey:(NSString *)key;

// Other methods which should also be used in component thread.
- (WXCoreFlexDirection)getCssStyleFlexDirection;
- (WXCoreFlexWrap)getCssStyleFlexWrap;
- (WXCoreJustifyContent)getCssStyleJustifyContent;
- (WXCoreAlignItems)getCssStyleAlignItems;
- (WXCoreAlignSelf)getCssStyleAlignSelf;
- (WXCorePositionType)getCssStylePositionType;
- (WXCoreDirection)getCssDirection;


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號