Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 rosiucristian10 · May 25, 2021 at 09:36 PM · 2duser interfaceinputfieldselectionrichtextbox

How to bold/un-bold selected characters from an InputField using rich text by pressing a button?

Hey guys!

Currently, I am working on a note-taking-like app and I am trying to emulate what google docs or any other word processor do (i.e. have a toolbar with bold, italics, etc.. buttons that can be pressed and the selected text changes accordingly). I have been trying to design this by using the TMP_InputField together with rich text.

Firstly, I want to give you a quick insight into what I used and what my current problem is. Getting the currently selected substring bolded was pretty easy to do. As I said, I want to achieve something like google docs therefore I decided to add the <b></b> tags to every character in the selection. The reason I'm doing this is that I would like the user to have the freedom to bold and unbold whatever combination of character he/she wants.

The way I keep track of the selected characters is by using selectionStringAnchorPosition and selectionStringFocusPosition which return the start and end indices of a selection. Therefore, I just do a for loop, and for each character in the selection, I add the tags.

Now, my main problems come when I try to undo the characters who are already bolded. My main idea was to use something like String.IndexOf and String.LastIndexOf to get some sort of reference to the start and end tags of a character. However, the biggest problem here is if for e.g. I have the string TE, the selected text would look like: T<b><b>E, or some sort of combination similar to this, depending on how the selection was done in the input field. This makes things a lot harder when I try to use the String methods mentioned above and adds unnecessary complexity (e.g. use Regex or something similar in order to correct the selection).

My question is if you guys have any ideas on how to approach this because I'm stuck. I have attached my current code in case anyone needs it.

Thank you in advance!

 using System;
 using TMPro;
 using UnityEngine;
 
 public class NotesController: MonoBehaviour
 {
     private TMP_InputField _inputField;
 
     private void Awake()
     {
         _inputField = transform.Find("Body/InputField").GetComponent<TMP_InputField>();
         _inputField.onFocusSelectAll = false;
 
     }
 
     /// <summary>
     /// Checks if there is any text selected. If it is then it either bolds or un-bolds the selected  characters based on
     /// what their current font/tags.
     /// </summary>
     public void MakeSelectionBold()
     {
         if (!IsTextSelected())
             return;
 
         var start = _inputField.selectionStringAnchorPosition; // Start index of selection
         var end = _inputField.selectionStringFocusPosition; // End index of selection 
 
         // If selection is in reverse, swap indices
         if (IsSelectionReversed())
         {
             var tmp = start;
             start = end;
             end = tmp;
         }
 
         // Bold every character in the selection
         for (var i = start; i < end + GetSelectedText().Length * 7; i += 8)
         {
             _inputField.text = _inputField.text.Insert(i, "<b>");
             _inputField.text = _inputField.text.Insert(i + 4, "</b>");
         }
     }
 
     /// <summary>
     /// Checks if the current selection is reversed or not.
     /// </summary>
     /// <returns>A boolean which is true if the selection was done from right to left</returns>
     private bool IsSelectionReversed()
     {
         return _inputField.selectionAnchorPosition > _inputField.selectionFocusPosition;
     }
 
     /// <summary>
     /// Checks if any characters in the <c>_inputField</c> are selected. 
     /// </summary>
     /// <returns>
     ///  A boolean which is true when start index is different than the end index and false otherwise
     /// </returns>
     private bool IsTextSelected()
     {
         return _inputField.selectionAnchorPosition != _inputField.selectionFocusPosition;
     }
 }
Comment
Add comment · Show 1
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 MrZipner · Jan 03 at 07:48 PM 0
Share

Hi, did you manage to solve this problem?

0 Replies

· Add your reply
  • Sort: 

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

122 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

Related Questions

Pointer Enter event doesn't fire Unity 5.5 1 Answer

How to scale according to different mobile resolutions? 0 Answers

Assets/Scripts/PlayerController.cs(32,49): error CS0126: An object of a type convertible to `float' is required for the return statement 1 Answer

Is NGUI suitable for Mobile Games ? 2 Answers

Selecting a part of string in input field 1 Answer


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