Skip to content

[TextInput] Nested Text Components not able to have different Colors [iOS] #29584

Closed
@borstessi

Description

@borstessi

Please provide all the information requested. Issues that do not follow this format are likely to stall.

Description

My project is a social network and I am currently encountering the following problem: I have a TextInput which is meant for comments, when the user enters his text he can mark other people. These are marked by an @username and are blue in color.

Inside the TextInput I render a text component which is divided into smaller parts which are also all text components to show the different colors.

Unfortunately this only works on Android and not on iOS.

React Native version:

Binaries:
Node: 10.18.1 - /usr/local/opt/node@10/bin/node
Yarn: 1.22.0 - /usr/local/bin/yarn
npm: 6.13.4 - /usr/local/opt/node@10/bin/npm
Watchman: 4.9.0 - /usr/local/bin/watchman
Managers:
CocoaPods: 1.9.3 - /usr/local/bin/pod
SDKs:
iOS SDK:
Platforms: iOS 13.6, DriverKit 19.0, macOS 10.15, tvOS 13.4, watchOS 6.2
Android SDK: Not Found
IDEs:
Android Studio: Not Found
Xcode: 11.6/11E708 - /usr/bin/xcodebuild
Languages:
Java: 13.0.2 - /usr/bin/javac
Python: 2.7.16 - /usr/bin/python
npmPackages:
@react-native-community/cli: Not Found
react: 16.11.0 => 16.11.0
react-native: 0.62.2 => 0.62.2

Steps To Reproduce

Provide a detailed list of steps that reproduce the issue.

  1. TextInput
  2. Nested Text Component with Nested Text Component inside
  3. Nested inside the TextInput like -> ()
  4. Change the value of input with variables that make the text change its color

Expected Results

The nested Text inside the TextInput should be able to have different colors on iOS like on Android.

Snack, code example, screenshot, or link to a repository:

Code Example:

convertTextToColors(_text) {
//define delimiter
const delimiter = /\s+/;

    let token,
        index,
        parts = [];
    while (_text) {
        delimiter.lastIndex = 0;
        token = delimiter.exec(_text);
        if (token === null) {
            break;
        }
        index = token.index;
        if (token[0].length === 0) {
            index = 1;
        }
        parts.push(_text.substr(0, index));
        parts.push(token[0]);
        index = index + token[0].length;
        _text = _text.slice(index);
    }
    parts.push(_text);

    //highlight hashtags
    let i = 0;
    parts = parts.map(text => {
        i++;
        if (/^@/.test(text)) {
            return (
                <Text key={text + i} style={{ color: Colors.blue }}>
                    {text}
                </Text>
            );
        } else if (/^#/.test(text)) {
            return (
                <Text key={text + i} style={{ color: Colors.yellow }}>
                    {text}
                </Text>
            );
        } else {
            return text;
        }
    });

    return parts;
}

<TextInput

                        ref={ref => (this.textInput = ref)}
                        style={{
                            backgroundColor: Colors.black,
                            height: this.state.inputHeight,
                            flex: 1,
                            fontSize: 15,
                          
                        }}
                        maxLength={1000}
                        onChangeText={text => this.updateText(text)}
                        multiline={false}
                        autoCapitalize={'none'}
                        underlineColorAndroid={'transparent'}
                        returnKeyType={'send'}
                        keyboardAppearance={'dark'}
                        blurOnSubmit={true}
                        onSubmitEditing={() => this.submit()}
                        onFocus={() => this.onFocus()}
                        onBlur={() => this.onBlur()}
                        onContentSizeChange={e =>
                            this.updateSize(
                                e.nativeEvent.contentSize.height
                            )
                        }
                        placeholder={App.trans('form.text.write_something')}
                        placeholderTextColor={Colors.grayLight}
                    >
                        <Text style={{ fontSize: 15 }}>
                            {this.convertTextToColors(
                                this.state.text
                            )}
                        </Text>
                    </TextInput>

Bildschirmfoto 2020-08-07 um 15 09 12
Bildschirmfoto 2020-08-07 um 15 12 02

Metadata

Metadata

Assignees

No one assigned

    Labels

    Component: TextInputRelated to the TextInput component.Platform: iOSiOS applications.StaleThere has been a lack of activity on this issue and it may be closed soon.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions