Parent size fit child size

Now I have an Image. The image has a Text as its child. The Text has a component Content Size Fitter.
The width of Text depends on the length of the text. What I wish is to make the width of Image depends on the width of Text. Because of the Content Size Fitter, I can’t get the width of Text (It’s always 0).

If you make text parent of image and set image stretch width, you will have image size of text

If I do this, the image will cover the text.

My bad, tested this solution without overlaying image and text. Ok, then to get this working scripting should be used. To get actual text use int textWidth = text.rectTransform.rect.width;
but keep in mind that text width don’t update immediately after setting(i guess it happens in LateUpdate() ). This mean that image size could be updated on the next frame.
Example:

    void  MyMethod()
    {
        Text.text = "hello world";
        StartCoroutine(ImageSizeUpdate());
    }

    IEnumerator ImageSizeUpdate()
    {
        yield return 0;
        image.rectTransform.sizeDelta = new Vector2(Text.rectTransform.rect.width, image.rectTransform.rect.height);
    }