Hide certain characters in a text field in Flutter - flutter

I have a text field which I need to style for example with bold or italics parts.
I tried overridding the TextEditingController's buildTextSpan and formatting the text using annotation ranges with custom styles but the edge cases were too much and I really couldn't get it to work.
So, thought about using a formatter where before every change in format I'll add a custom character, like this:
This text is |bBOLD and this is |iITALICS. Would get me this:
This text is BOLD and this is ITALICS. So I override the buildTextSpan to build the TextSpan from a parse function where I split the text by the special characters and check the initial letter of each text for formatting info.
This works well except for the fact that when I press the right arrow to go the next character after the "This text is ", the cursor will stay fixed as it thinks there are two characters but being only for formatting, they aren't there on the render.
Is there any way I could tell the textfield to ignore certain characters when selecting, moving selection or typing?

I think this would work!
static const kCharToBEIgnored = 0x2C;
// Here 0x2C means ',' comma
// For complete list visit https://api.flutter.dev/flutter/charcode/charcode-library.html
String get text {
return String.fromCharCodes(
_value.text.codeUnits.where((ch) => ch != kCharToBEIgnored),
);
}

Related

TextMeshPro Input field - can't set placeholders' wrapping to enabled?

I am trying to have a TMP Input Field with multiline (wrapped) placeholder. The input field has its content type set to Integer Number. Placeholder text has some string as text and has wrapping set to "enabled". In prefab editor, it looks ok (text is wrapped). However, when the scene loads, placeholder text immediately has its wrapping reset to "disabled".
The problem: I assume Input field resets the wrapping to disabled on child text, because its content is not multiline, as specified here. I can't set the input field to accept multiline - the option is not in inspector; I can see it if I switch to Debug mode, but as soon as I set it to multiline it switches back to single line. So, how do I set the wrapping on placeholder to enabled without it getting switched back to disabled in runtime? Or, alternatively, how do I set my InputFiled to be multiline (I assume that will fix the wrapping on the placeholder)?
What I have for now:
I have an input field that works like this: when it's not focused, there is a multiline text, for example: "Write your\nnumber here!". When focused, it's replaced by empty line and user can enter an integer number. On finishing input (OnEndEdit) I use the input to spawn some GameObjects and then replace the text again with "Write your\nnumber here!". Editor screenshot:
Right now it works as I expect. However, now I want to dynamically change the placeholder text, so I don't know where the newline will be and I would like for TMP to wrap the text for me, same as it does in Text component with Wrapping enabled.
So: how do I use TMP's wrapping - enabled on InputField without it getting reset to disabled? Either both the user input and placeholder, or just on placeholder itself - doesn't matter.
MultiLine is only available if the Content Type is set to Standard or Auto Corrected.
As soon as you set the input type to Integer Number it basically behaves just like an integer field in the Unity Inspector: No line wrapping but rather overfloating and scrolling to the right.
Alternatively if you need multi lines you could set the Content Type to Custom and then fully flexible adjust it to your needs like e.g.
LineType : Multi Line Newline
Line Limit: 0
Input Type: Standard
Keyboard Type: Default
Character Validation: Integer
And in general: Of course you also can't set a text to an input field that only accepts integer numbers ;) Rather set your placeholder text in the Placeholder object!
Anything that is applied to the Placeholder will be displayed automatically as long as there is no value typed in the input field!

How can I balance text lines in flutter?

I am trying to achieve something similar to this in flutter.
Take the following text for example;
Some long text that should be displayed in
two lines.
I want it to be displayed like this;
Some long text that should
be displayed in two lines.
I can't simply do this by putting a 'new line character' \n to the string manually because it will be a dynamic string.
You can use RichText, or try to add "\n" operator. So it should look like "Some long text that should\nbe displayed in two lines."

How to add a small straight line (I mean like this: a̅ b̅ X̅) onto a character inside a string?

I want to add small straight line onto some desired characters/numbers inside a string inside textview. I couldn't find a solution. Maybe using NSMutableAttributedString. Meanwhile, I mean doing this programmatically. There is strikethrough style, but not overstrike style. Or maybe adding the letters "a" and "_" with different .baseline values. But how to add both characters onto each other then?
Is it possible?
EDIT: Due to make a try for the helpful answers below, I think to make the line at a spesific height is needed. "A\u{0305}" makes the up line very close to the character, as if it sticks. Is there a way to make it at specific height? For example, if we assume that all the keyboard-inputted characters are written inside every single boxes, the ceiling side of these boxes could be lined?
So this (note: see edit below) appears to be an "a tilde ogonek" (it's Lithuanian).
You can write it for instance as follows using these two Unicode characters:
let atildeogonek = "\u{0105}\u{0303}"
let title = "How to add a small straight line (I mean like this: \(atildeogonek)) onto a character inside a string?"
The first character is the a with an ogonek, the second one is the tilde.
EDIT: The initial question specifically asked about the character ą̃ ("a tilde ogonek") in the title, and I used this code to demonstrate how to use Unicode characters in a Swift string. After posting this answer, the question was edited to be more general about "a line above a character".
Programmatically, you could use a function like this:
func overline(character: Character) -> Character? {
return "\(character)\u{0305}".first
}
That will take a character as input and return a new character (glyph) that has had the Unicode combining overline character added to it. It will return nil if adding the combining overline character fails.
The code print(overline(character:"A")!), for example, returns "A̅"
Or, if you want to add an overline to every character in a string, you could use a function like this:
func overline(characters: String) -> [Character?] {
return Array(characters).map { return "\($0)\u{0305}".first
}
}
(I'm not sure if there are any characters for which the above will fail, so I'm not sure if force-unwrapping the result is safe. Thus I left the result of both functions to be optional Character/Array of Character.)
You can easily find the unicodes of ā or ą̃ by using the xcode's own Character Viewer. Just follow the following steps :
hit : Control + Command + SpaceBar
If you get a compact one like this, click the upper right corner icon to expand it.
When expanded, Click the settings gear in the corner . Select customize list.
select Enclosed Characters
Go down to the bottom and open Code tables then add Unicode.
Now, just search for your required Character and you can check its unicode value. here i am searching ā
to print unicode's value :
print("\u{0101}")

How to indent if Text is softWrapped in Flutter

I'm working on an app that displays lyrics for several hymns in my native language.
Each line of the hymn is an individual Text Widget. I want the text to indent if it got softWrapped if user increases the font size to indicate that its not the next line. How can I achieve this effect?
Adding one or more tab escape characters "\t" to your strings may do the trick for you:
Text("\t this is a text string that will have an indentation",),

gtk turn on italic, bold, etc in textview

[Revised]
I am trying GTK3 TextView, in C. I know how to apply tags, such as italics, etc., to a region of text.
But how do I "turn on" a style (italics, bold, etc) while typing in the buffer? For example, suppose I am typing text and there are no styles set. Then I want to type in italics. I want to apply the italic tag to the insert point, after which whatever I typed would be in italics.
I notice that when you place the insert cursor to the right of any existing styled text, the tags of the previous character are inherited, but only if the style is in effect before and after the insert point. Interestingly, the trailing newline can be stylized, so when the insert point is at the end of a line and the last visible character has a style, then that style is inherited.
I've looked at some methods that use a key-press signal and then apply a text tag to the region of the untagged character just typed, but that is slow and looks odd on screen -- the plain char is displayed and then replaced by the tagged char.
I've also tried:
gtk_text_buffer_insert_markup(buf, &iter, "<i>", -1);
but that gets a Warning: Invalid markup string: ... Element 'markup' was closed, but the currently open element is 'i' and has no effect on the displayed text. Using <i></i> gets no warning but also has no effect. I finally resorted to:
gtk_text_buffer_insert_markup(buf, &iter, "<i> </i>", -1); // 2 spaces
gtk_text_iter_backward_char (&iter);
gtk_text_buffer_place_cursor(buf, &iter);
which lets me start typing in italics between 2 new spaces -- that's not so bad.
But clicking an "I" button to start typing in italics -- that is a basic editing function that has been around for decades. It must be possible in GTK somehow?
Any ideas?
thanks