react-native-render-html icon indicating copy to clipboard operation
react-native-render-html copied to clipboard

How to achieve textAlign style?

Open cristianoccazinsp opened this issue 7 years ago • 6 comments

This is just a question. I have some HTML code that includes a <td style="text-align: right"><div>...</div></td>. It seems to render some stuff right aligned on IOS, but not on android. When inspecting the td component, the styles are not there. Do they get propagated/moved into the inner most children? I suspect textAlign should actually be translated into alignSelf or alignItems if using flex.

cristianoccazinsp avatar May 23 '19 15:05 cristianoccazinsp

I realized the text-align does not render / accept by the component, even I tried to do it with

tag and without
tag.

Fail path: With tag, <div style="text-align:center"> Without tag, I pass value to props **baseFontStyle**={{ textAlign:'center' }}

Success path: At last I gave up using <div> tag and replace with <p>. Hooray, it's now center aligned 😄

TommyLeong avatar Jul 10 '19 08:07 TommyLeong

Could we get a response from the developers whether this is something they intend to fix? The text-align prop does nothing on android (works fine on iOS). It seems that the prop is never used as the text will align center if it can fit on 1 line within the width, but as soon as the text needs 2 or more lines then it will always left-align. Changing all <div> elements to <p> like @TommyLeong suggest above is not a viable solution.

woba-ko avatar Mar 10 '20 12:03 woba-ko

Hi,

Temporary solution worked on IOS and Android:

render() {
  const defaultRenderer = {
      renderers: {
        p: (htmlAttribs, children, convertedCSSStyles, passProps) => {
          return <Text style={{textAlign: 'center'}}>{children}</Text>;
        },
      },
    };

<HTML {...defaultRenderer} html={content} />

vvusts avatar Jun 19 '20 13:06 vvusts

@vvusts Thanks, I will give this a try and report back if it works in my solution aswell. I assume you can switch the 'p'-element to a 'div'-element if needed, right? like so:

  renderers: {
    div: (htmlAttribs, children, convertedCSSStyles, passProps) => {
      return <Text style={{textAlign: 'center'}}>{children}</Text>;
    },

woba-ko avatar Jun 19 '20 13:06 woba-ko

Yes this way you can implement how each element should render. I use it to render centered small clickable images for and etc.

We are in the process of cleaning the issue tracker to make it manageable. I suggest you continue the discussion in #347, as this seems to be a duplicate.

jsamr avatar Jul 05 '20 14:07 jsamr