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

[Layout] Text overflow in ListView with flexDirection: 'row'

Open michalraska opened this issue 11 years ago • 29 comments

Hi, I have problem with text overflow in ListView, when ancestor has set style flexDirection: 'row':

image

part of code:

  render: function() {
    return (
      <View style={styles.container}>
        <ListView
          dataSource={this.state.dataSource}
          renderRow={this.renderRow}
        />
      </View>
    );
  },

  renderRow: function (message, sectionID: number, rowID: number) {
    return (
      <View style={{flexDirection: 'row'}}>
        <Image
          source={{
            uri: 'http://icons.iconarchive.com/icons/designbolts/free-male-avatars/128/Male-Avatar-icon.png',
          }}
          style={{flexWrap: 'wrap', width: 32, height:32}}
        />
        <View style={{flexWrap: 'wrap'}}>
          <Text>
            {message}
          </Text>
        </View>
      </View>
    );
  },

How to wrap text? Thanks.

michalraska avatar Apr 17 '15 12:04 michalraska

I solved problem by set flex: 1 to wrapper view of text.

      <View style={{flexDirection: 'row'}}>
        <Image
          source={{
            uri: 'http://icons.iconarchive.com/icons/designbolts/free-male-avatars/128/Male-Avatar-icon.png',
          }}
          style={{width: 32, height:32}}
        />
        <View style={{flex: 1}}>
          <Text>
            {message}
          </Text>
        </View>
      </View>

michalraska avatar Apr 17 '15 13:04 michalraska

@michalraska - that's the right way to go! Thanks for updating with your solution, should be helpful to others who have the same issue.

brentvatne avatar Apr 17 '15 22:04 brentvatne

Setting flex: 1 will trigger the underlying text to wrap, but what if you want your view to grow with the text, and then wrap when it reaches the boundary?
wrap-example

sjmueller avatar Apr 25 '15 18:04 sjmueller

The temporary hack I'm using right now is conditionally adding flex: 1, based on the content length. Of course this is pretty imprecise, and doesn't take into consideration orientation or variations in device resolution.

sjmueller avatar Apr 25 '15 20:04 sjmueller

@brentvatne I just found the this solution only works if flex: 1 is apllied to the view immediately beneath the one where flexDirection: 'row' is being applied to. Example: https://rnplay.org/apps/B8mUMw

If one removes the second view or applies flex: 1 to it it works, otherwise it won't.

Is this intended behaviour?

despairblue avatar Jun 09 '15 15:06 despairblue

@despairblue - initially I was a bit confused but realize that it does indeed make sense - by setting flexDirection: 'row' we are telling it to flex on the horizontal axis. If you change that to column on the container it works as you would expect.

brentvatne avatar Jun 15 '15 22:06 brentvatne

@brentvatne right, it's a contrived example, by not using row in the first place this does not happen, but let's assume I need to use row because I want to do something like @michalraska. I've updated the example above and made a drawing to elaborate on it:

2015-06-16 13 04 41

It seems in row 1 the view with the question mark simply keeps growing to the right, thus the containing view never wraps. That's what I meant by the view immediately beneath the one where flexDirection: 'row' is applied needs to have flexDirection: 'column' applied. Knowing this it's perfectly fine to avoid it, though finding this out took me some time, I just wonder if this is intended behaviour?

despairblue avatar Jun 16 '15 11:06 despairblue

@brentvatne is there a way to do this without using flex : 1, in my case i have multiple components stacked across a view with flexDirection : 'row'

what i want is each component to render to its full width, and if it overflows then go to the next line.

Using flex : 1, means that each component has the same width , which is not what i want, which means there's a lot of unwanted blank space.

in the screenshot you can see that the first row has less space between the emoticon and the space character, whereas the third row has far more spacing since other components aren't there

ios simulator screen shot 16-jun-2015 4 48 48 pm

thanks for any help that you provide :)

jawadrehman avatar Jun 16 '15 15:06 jawadrehman

cc @vjeux - not quite sure about the above, maybe you have some insight

brentvatne avatar Jun 17 '15 21:06 brentvatne

ping @vjeux ? any ideas on this... i have to use webviews atm to get around this, and the performance isnt really great.

jawadrehman avatar Jun 21 '15 17:06 jawadrehman

I believe what you're trying to do isn't achievable with this layout system right now. I ran into the same problem. I solved this by creating a native bridge to a function which does something like this:

let label = UILabel()
label.font = UIFont(name: "Avenir", size: 16.0)
label.text = THETEXT
return label.intrinsicContentSize().width

9edf182c-1ac3-11e5-94d0-53a5cd267bd2

abhoopathy avatar Jun 25 '15 02:06 abhoopathy

@abhoopathy Why didnt you try hooking that up directly in the RCTTextView.m or something like that ... wouldn't that make it much easier ? Plus also save computation on creating the unnecessary UILabel ?

jawadrehman avatar Jun 25 '15 09:06 jawadrehman

That's probably a good idea. By not using the UILabel, are you suggesting using NSString's sizeWithFont? (discussed here http://stackoverflow.com/questions/1324379/how-to-calculate-the-width-of-a-text-string-of-a-specific-font-and-font-size)

abhoopathy avatar Jun 25 '15 17:06 abhoopathy

@abhoopathy maybe sizeWithAttributes would be better since sizeWithFont is deprecated . However, I think I should have said RCTText.m instead of RCTTextView.

jawadrehman avatar Jun 29 '15 15:06 jawadrehman

Good idea

abhoopathy avatar Jun 29 '15 20:06 abhoopathy

@abhoopathy - any success with that?

brentvatne avatar Jul 21 '15 01:07 brentvatne

Is there any update here? :)

PhilippKrone avatar Sep 13 '15 18:09 PhilippKrone

Haven't heard back for like, forever (sorry I'm on a plane to LA, just embracing it), so I'm closing

brentvatne avatar Sep 13 '15 22:09 brentvatne

If you feel the same pain, please let the team know HERE. I still seem to face the same problem. Also I opened an issue here in case someone can provide a solution specifically to my problem.

SudoPlz avatar Mar 29 '16 12:03 SudoPlz

This issue should not be closed. Basically, if you use flexDirection : 'row', text wrapping doesn't work. There doesn't seem to be a clear workaround. Using flex : 1 now collapsed your content to nothing/invisible, so that workaround is gone. A very basic layout aspect is now a total time wasting nightmare. Please fix this!

mschipperheyn avatar Jan 12 '17 15:01 mschipperheyn