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

Support background colors on <Text> components #4155

Closed
KAnder425 opened this issue Feb 20, 2020 · 3 comments · Fixed by #6300
Closed

Support background colors on <Text> components #4155

KAnder425 opened this issue Feb 20, 2020 · 3 comments · Fixed by #6300

Comments

@KAnder425
Copy link
Contributor

Proposal: Support background colors on <Text> components

Summary

Both iOS and Android RN support the ability to set the background color of a <Text> element. Without this support in RNW, we have to resort to wrapping Text in Views in order to get the same effect. This approach quickly runs into complex Yoga/Flexbox layout issues that prevent its use in all but the most basic scenarios.

This is not a new issue. See #698 and #1949 for background.

Motivation

I'm implementing a search highlighting feature in our app. I need to apply a highlight background on specific runs of text in several elements of the app. If I use nested <Text> elements to apply styling like 'underline' to the various runs of text, everything looks stellar. When I switch to nested <View> elements to apply a background color I run into seemingly insurmountable issues with Flexbox/Yoga layout and all those nested Views.

I'm (somewhat) sure I'll surmount those issues at some point using the View/Text workaround, but it would be so much better if I could just do what iOS and Android already support.

Basic example

Here's an example of what I'd like to do....

import React, { Component } from 'react';
import { View, Text } from 'react-native';

// This is the version using Text runs that works great in iOS and Android
export default class FlexDirectionBasics extends Component {
  render() {
    return (
      <View style={{flex: 1, height: 20, maxWidth: 250, paddingTop: 200, flexDirection: 'row'}} >
        <Text numberOfLines={1}  ellipsizeMode={'tail'}>
          <Text style={{fontWeight: 'bold'}}>
            I am bold&nbsp;
          </Text>
          <Text style={{backgroundColor: 'yellow', color: 'red', fontWeight: 'bold'}}>
            and super glowing red (this should trunctate at 250)&nbsp;
          </Text>
          <Text style={{fontWeight: 'bold'}}>
            and bold again forever.
          </Text>
        </Text>
      </View>
    );
  }
}

Open Questions

I realize that this issue is due to a lack of support for background color in XAML's TextBlock but perhaps we can find a creative way to workaround that in RNW native code?

@ghost ghost added the Needs: Triage 🔍 New issue that needs to be reviewed by the issue management team (label applied by bot) label Feb 20, 2020
@harinikmsft harinikmsft added this to the MVP+ (vNext M5) milestone Feb 20, 2020
@harinikmsft harinikmsft added Needs: PM Design New feature or request and removed Needs: Triage 🔍 New issue that needs to be reviewed by the issue management team (label applied by bot) labels Feb 20, 2020
@harinikmsft harinikmsft modified the milestones: MVP+ (vNext M5), Backlog Feb 20, 2020
@harinikmsft
Copy link
Contributor

This is a good topic to discuss during an upcoming checkpoint review, I will queue it up.

The most elegant way to solve this may be to improve the XAML TextBlock to be able to take a background which will likely take a while (needs WinUI3+).

@harinikmsft
Copy link
Contributor

Per @kmelmon, TextBlock.TextHighlighters property in XAML might be the right native property that helps address this scenario.

I looked out of curiosity - Android has an analogous TextView.textColorHighlight property and the scenario appears to be possible to accomplish in iOS as well by applying a background color to an NSAttributedString combined with UITextView's attributedText props.

This may be a good candidate to surface as a cross-platform prop for react-native.

@rectified95
Copy link
Contributor

FYI @KAnder425 I've just submitted a PR implementing what you requested - backgroundColor for arbitrarily nested <Text> elements. I hope to have it merged soon.

@rectified95 rectified95 linked a pull request Oct 22, 2020 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants