Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by RePlexOfficial · Mar 26, 2018 at 05:09 PM · uiinputfieldcolor change

Color specific words in Input Field

I want my User to be able to write in the input field and specific words to be coloured differently - like Syntax Highlighting. However, if I do this in the OnValueChanged Method, this method gets executed again, because I add the attribute. This leads to a StackOverflow Exception.

Do you have any suggestions?

Thank you in advance!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Martin_Gonzalez · Mar 26, 2018 at 05:14 PM

Yes you can!

Have you tried this?

https://docs.unity3d.com/Manual/StyledText.html

Example

 We are  <color=#00ffffff>not</color> <b>amused</b

Comment
Add comment · Show 13 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image RePlexOfficial · Mar 26, 2018 at 05:18 PM 0
Share

Yeah,

 `int charIndex = myText.IndexOf ( "S" );
 string newText = myText.Replace ( myText [charIndex].ToString (), "" + myText [charIndex].ToString () + "" );`

but then the text gets changed again and the OnValueChange $$anonymous$$ethod gets executed which means that my code will insert another attribute which causes the error.

(my color attributes are not shown correctly, they are in the empty string)

avatar image Martin_Gonzalez RePlexOfficial · Mar 26, 2018 at 05:33 PM 0
Share

Yes, you will need in every OnValueChange replace again the text.

Perhaps you can create a method that you send the current text split it and check the words. You can have a dictionary with the word and the styled text you want.

Create a UI Text in the scene and add this script on it. Tell me if you understand it.

 using System.Collections;
 using System.Collections.Generic;
 using System.Linq;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class NewBehaviourScript : $$anonymous$$onoBehaviour {
     public Text myText;
     Dictionary<string, string> myStyledWords = new Dictionary<string, string> {
         {"word", "<b>word</b>"}
     };
 
     private string userInput = "This has a bold word";
 
     private void Start() {
         myText = GetComponent<Text>();
         var text = StyledText(userInput);
         myText.text = text;
     }
     
     public string StyledText(string userInput) {
         var splitted = userInput.Split(' ');
 
         return splitted.Aggregate((acc, word) => {
             var finalWord = word;
             if (myStyledWords.Contains$$anonymous$$ey(word))
                 finalWord = myStyledWords[word];
             
             return acc + ' ' + finalWord;
         });
     }
 }
 

avatar image RePlexOfficial Martin_Gonzalez · Mar 26, 2018 at 05:48 PM 1
Share

Apart from the aggregate, I understand it, but does it work when I change the text during runtime?

Show more comments
avatar image Martin_Gonzalez RePlexOfficial · Mar 26, 2018 at 05:47 PM 0
Share

I recently tested it but it will not work on InputFields (On Text yes will work) but Input Field do not support Rich Text :(

 The Text property of the Text control itself will change as the user types and the value can be retrieved from a script after editing. Note that Rich Text is intentionally not supported for editable Text controls; the field will apply any Rich Text markup instantly when typed but the markup essentially “disappears” and there is no subsequent way to change or remove the styling.
avatar image MacDx Martin_Gonzalez · Mar 26, 2018 at 05:56 PM 0
Share

I have an idea. What if you add an extra Text component to the input field. This text component will display your stylized text. The input field will write to its default text component that will be invisible (just set the color alpha to 0 or something) then with OnValueChanged you do whatever check you need to do but you don't change the actual value you just pass it along to the extra Text component that will display it, that way it, OnValueChanged won't be triggered again and you can stylize the text however you want.

Show more comments
Show more comments
avatar image Martin_Gonzalez · Mar 27, 2018 at 05:40 PM 0
Share

Great! :D Good luck!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

130 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Changing just the name of person in UI Text 0 Answers

Customize Inputfield Unity Plus/Pro 0 Answers

Hide Textselection in Scrollable Inputfield 1 Answer

How do I change "return"/"submit" button to "search" in native android keyboard in TMP_InputField? 1 Answer

Prevent focus from leaving inputfield 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges