Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Native UI Component] Calculation of width/height for custom native UI element? #2260

Closed
aksonov opened this issue Aug 7, 2015 · 12 comments
Closed
Labels
Resolution: Locked This issue was locked by the bot.

Comments

@aksonov
Copy link

aksonov commented Aug 7, 2015

I want to build iOS react native component - analog of Text, where width and height is not known, but calculated dynamically depending from its content. As I see from debugging, RCTText.drawRect method is called with calculated rect already, but my component is called with empty rect if i did not define sizes via styles.

How to define needed rect for custom View?

I know, it is not bug/issue, but it seems to be lack of documentation, so I'm asking here (i've asked the same at stack overflow, but looks like nobody knows there).

@alinz
Copy link

alinz commented Aug 7, 2015

@aksonov, this might be the answer #1374

@aksonov
Copy link
Author

aksonov commented Aug 7, 2015

Sorry, don't see how apply that measure function. Even opposite, I need to provide it for my custom view.

7 авг. 2015, в 22:13, Ali Najafizadeh notifications@github.com написал(а):

@aksonov, this might be the answer #1374


Reply to this email directly or view it on GitHub.

@alinz
Copy link

alinz commented Aug 7, 2015

Here's what you have to do in order

var {
    React,
    NativeModules: {
        UIManager
    }
} = require('react-native');


class MyCustom extends React.Component {
    ...
}

UIManager is one of the internal utilities which can be used to calculate the location of your component in screen.

@aksonov
Copy link
Author

aksonov commented Aug 7, 2015

I don't need to calculate location, but to create view without fixed width and height, like Text.

Pavel.

7 авг. 2015, в 23:53, Ali Najafizadeh notifications@github.com написал(а):

Here's what you have to do in order

var {
React,
NativeModules: {
UIManager
}
} = require('react-native');

class MyCustom extends React.Component {
...
}
UIManager is one of the internal utilities which can be used to calculate the location of your component in screen.


Reply to this email directly or view it on GitHub.

@brentvatne
Copy link
Collaborator

Hi @aksonov - for a detailed request like this is would help a lot to post an example repository that people can clone and experiment with. 'm not sure I have enough information to act here and code would probably fill that gap

@brentvatne brentvatne changed the title Calculation of width/height for custom native UI element? [Native UI Component] Calculation of width/height for custom native UI element? Aug 9, 2015
@aksonov
Copy link
Author

aksonov commented Aug 11, 2015

Okey, here is my test repository: https://github.com/aksonov/TestApp
The problem there is SVG Path element which i want to wrap by react View, make it clickable yet. I could scale it to fit screen, but i don't want to define its "width" and "height" (because i don't know it) but only "scale" property. The same as RCTText or RCTStaticImage. I've tried to study sources but still cannot find place where these elements (RCTText/RCTStaticImage) calculates its width/height and pass it to parent view.

SVG Path uses CGPathRef internally so it could get own height/width using
CGRect boundingBox = CGPathGetBoundingBox(path);
but how to pass it to "style" before drawing phase?

        <View style={{borderWidth:2}}>
        <SVGPath style={{width:1000, height:1000}} scale={0.5} filename="BezierCurve3"/>
          </View>

should be just

        <View style={{borderWidth:2}}>
        <SVGPath scale={0.5} filename="BezierCurve3"/>
          </View>

@aksonov
Copy link
Author

aksonov commented Aug 11, 2015

Finally, I've found that code from Image.ios.js, so maybe best way is to do the same - use another native iOS utility class to get height/width for SVG path and call it from render() method like done in Image.ios.js:

    var source = resolveAssetSource(this.props.source) || {};

    var {width, height} = source;

@aksonov
Copy link
Author

aksonov commented Aug 27, 2015

Sorry, that code doesn't help. Could you check test repository and my examples? I still can't find a way to get data from native UI component before rendering (to get width/height of svg element).

@aksonov
Copy link
Author

aksonov commented Aug 27, 2015

Looks like magic is hidden within NativeMethodsMixin and its measure methods, but it is still not clear. Is there any documentation?

UPD: I've found text calculation within Layout.c static void layoutNodeImpl(css_node_t *node, float parentMaxWidth), but it is not clear how to make something similar for my component...

UPD2: Looks like it is done using RCTShadowText view for Text, so should i create own "shadow" view for my custom component which will calculate its size?

@aksonov
Copy link
Author

aksonov commented Aug 27, 2015

Okey, finally i've found exactly what i need - create own RCTShadowView descendant, add shadowView factory method to my own RCTViewManager, override fillCSSNode method and do calculation within own RCTShadowView using C static function:

- (void)fillCSSNode:(css_node_t *)node
{
    [super fillCSSNode:node];
    node->measure = RCTCircleMeasure;
    node->children_count = 0;
}

Maybe you could add it to documentation, because now it is pretty difficult to find out.

@aksonov aksonov closed this as completed Aug 27, 2015
@aksonov
Copy link
Author

aksonov commented Aug 27, 2015

That way solved issue for 'child' element, but not for composite parent - native View. I expected View will calculate its size automatically as it does for children Text, but it doesn't... Please provide correct way, looks like I've missed something.

@aksonov aksonov reopened this Aug 27, 2015
@aksonov
Copy link
Author

aksonov commented Aug 27, 2015

Okey, probably i don't understand how React Native Layout works, I can't wrap View nicely around Text too. I've created issue #2465 for it.

@aksonov aksonov closed this as completed Aug 27, 2015
@facebook facebook locked as resolved and limited conversation to collaborators Jul 22, 2018
@react-native-bot react-native-bot added the Resolution: Locked This issue was locked by the bot. label Jul 22, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Resolution: Locked This issue was locked by the bot.
Projects
None yet
Development

No branches or pull requests

4 participants