How to get the number of lines displayed in a Textmeshpro - text (UI)? - unity3d

So in my script I want to know how many lines are displayed in a Textmeshpro - text (UI).
public TextMeshProUGUI commentaryText;
Debug.Log("number of lines " + (commentaryText.text.Split('\n').Length - 1));
I tried something like above but it will only show the number of lines with a \n newline character, however I also want to know about lines that are caused by wraparound when they reach the limit of the textbox. Is there a way to get the number of lines that the user sees?
Any advice appreciated. Thanks

Basically you can access commentaryText.textInfo.lineCount to get the number of lines. What I did is that on a button click it logs the number of lines for a TextMeshPro I created, so I was able to test it:
public TextMeshProUGUI textMeshPro;
public void Click()
{
Debug.Log(textMeshPro.textInfo.lineCount);
}
Update for what you'd like from the comments:
You need to set the TMP to align to bottom and set the overflow to masking. Then you need a parent that has a mask component (I used the basic panel). The text that would go outside the size of the parent is hidden.
I'm not sure what you'd like with the line count, but this will count the hidden lines as well. So in my example if you use the lineCount it will log 6.

Related

How to render a "View More" link if text in a widget is too long to fit in a specified nr of lines

I am working on an app with a chat feature that shows messages in a ListView. The size of each "message" depends on the amount of text in that particular message.
I would like to put a limit on the number of lines, say 10 lines max. If a particular message happens to take up more than the limit, the text should be truncated near the end of the 10th line and a "FlatButton" with the text 'more...' be provided, which allows the user to open up that message to see all the text. For messages that fit, this button will not be shown.
The part I am struggling with is being able to truncate the text at the right point.
Currently I am thinking that guessing the number of lines a message might take up based on the number of characters and the font size would be the best approach, with the limitation that sometimes it might be off by a couple of lines. That is probably acceptable in this application.
As a bonus I also would like to render the "more..." button "inline" at the end of the text, but that is probably a separate question.
Did you try the maxLines property of Text widget? This in combination with overflow should solve your problem.
Reference
maxLine property
overflow property

CSSearchableItemAttributeSet: can't define third line content

So, is there a table anywhere explaining how each itemContentType will be rendered on the final search?
For now I only got:
kUTTypeMovie will render the duration on the third line.
kUTTypeMessage will render the contentCreationDate on the top right corner
I wanted to get a custom text on the third line, is it possible?
If not, how to only draw a year there, like this:

How to color text in private message in mIRC

am tring to input my each text line in private message colored, i use this code it is showing colored text but adding a space before text. how can i remove that space
on *:INPUT:?:{
say 4 $strip($1-)
halt
}
and also now commands like
/clear
is not working .. showing in read but its not working
on input events are sensible
if you dont know how it works, dont mess with it, since it may mess with all your scripts
try this
on *:input:?:{
if (!$inpaste) && (!$ctrlenter) && ($left($1,1) != /) {
command here
haltdef
}
}
by the way, this event must be on top of all on input events to work

How to make a very long non-stop string (without any space in it) break into many new lines when showing in HTMLPanel?

I have an HTMLPanel inside a FlowPanel which is inside a ScrollPanel like this.
<g:ScrollPanel>
<g:FlowPanel>
<g:HTMLPanel ui:field="showMessageHTMLPanel" width="600px"/>
</g:FlowPanel>
</g:ScrollPanel>
The showMessageHTMLPanel is used to hold the text that user enter a TextArea.
I want that the showMessageHTMLPanel should show exactly like how it was displayed in TextArea.
Ex,if user types in many sentences in new lines in TextArea, then the showMessageHTMLPanel should show similar like this:
This is text1
This is text2.
So here is what I did. I uses new SafeHtmlBuilder().appendEscapedLines(message).toSafeHtml().
HTML showMessageHTML = new HTML(new SafeHtmlBuilder().appendEscapedLines(message).toSafeHtml());
getView().getShowMessageHTMLPanel().add(showMessageHTML);
The result is that It breaks the lines quite OK, no problem.
However, When I type a non-stop very long string ( a String that doesn't have any space on it) into a TextArea. Ex, see this non-stop string:
"Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa."
As you saw, even this non-stop string has no space, but when typing in TextArea then the string will automatically fall into new lines.
Ok, now when I show that non-stop string in getView().getShowMessageHTMLPanel(), it showed the text as one straight line without any line break:
Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.
The user has to scroll the scrollbar to see the complete text. This is unacceptable since it is too hard to see the whole line. Also many urls are non-stop string without spaces. So the user may not be able to copy the url.
How to make a very long non-stop string (without any space in it) break into many new lines when showing in HTMLPanel?
Or
Do you know any other widget that can handle this?
I am sure, a simple css property should help.
word-wrap:break-word;
Detail here

Auto scroll to bottom with a textbox

I have an mdb file made by ms access. It got a form inside and inside the form there are one large textbox.
The intention of making this textbox is to show the progress of some work by adding messages inside the textbox:
txtStatus.value = txtStatus.value & "Doing something..." & vbCrLf
txtStatus.value = txtStatus.value & "Done." & vbCrLf
But the problem is, when the height of the text > height of the textbox, the new message is not displayed automatically. The textbox has a scroll bar, but I have to scroll it manually. I would like to auto scroll to the bottom whenever new text pop up.
I tried to add this code(copied from internet) in the On Change property, but the code failed, it does nothing:
Private Sub txtStatus_Change()
txtStatus.SelStart = Len(txt) - 1
End Sub
I wish there would be some simple and beautiful way to achieve this. I don't want to add some code which only work on some computers due to its dependence on the windows platform's kernel/etc.
You can do it via a call to a sub;
AppendText "Bla de bla bla."
.
.
sub AppendText(strText As String)
with txtStatus
.setfocus '//required
.value = .value & strText & vbNewLine
.selstart = len(.Value)
end with
end sub
There is a workaround to the design flaw mentioned by steve lewy in his comment on the original post. It is possible to have a text box that appears to do both of the following:
When the contents are too large for the box, and the box does not
have the focus, the box displays the last part of its contents,
rather than the first part of it.
When the box has the focus, it can scroll to any part of the text,
but it initially shows only the last part of the text, with the
cursor at the end of the text.
This is accomplished by actually having two identically-sized, overlaid text boxes, where one is visible only when the focus is elsewhere, while the other is visible only when it has the focus.
Here’s an example of how to do it in Access 2010.
Create a new Access database, and create a memo field named LongNote in its only table. Fill LongNote with some examples of long text. Create a form for editing that table.
Create a text box called BackBox with the desired size and font, too small to completely show a typical value of its data source, LongNote. (Instead of creating this box, you can rename the default text box created on the form.)
Make an exact copy of that box called FrontBox. Set the data source of FrontBox to be either the entire contents of BackBox or the last part of the contents, as shown below. The size of the last part, measured in characters, depends on the size of the box and its font, as well as on the kind of text to be displayed. It needs to be chosen by trial and error to reliably allow that many characters to be displayed in the box. For instance, here’s the formula for a box that can reasonably hold only 250 characters:
=iif(Len([BackBox])>=250,"... " & Right([BackBox],246),[BackBox])
If the whole value is too large to be shown, three dots precede the part that is shown to indicate that it’s incomplete.
Create another text box called OtherBox, just to have somewhere you can click besides the two boxes already mentioned, so neither of them has the focus. Also create a tiny (0.0097 x 0.0097) text box called FocusTrap, which is used to avoid selecting the entire contents of whatever text box gets the focus when the form is displayed (because text selected that way is hard to read).
Enter the following event-handling VBA code:
' Prevent all text boxes from being focused when a new record becomes
' current, because the focus will select the whole text and make it ugly
Private Sub Form_Current()
FocusTrap.SetFocus
End Sub
Private Sub Form_Open(Cancel As Integer)
FocusTrap.SetFocus
End Sub
' When FrontBox receives focus, switch the focus to BackBox,
' which can display the entire text
Private Sub FrontBox_GotFocus()
BackBox.SetFocus
FrontBox.Visible = False
End Sub
' When BackBox receives the focus, set the selection to
' the end of the text
Private Sub BackBox_GotFocus()
BackBox.SelStart = Len([LongNote])
BackBox.SelLength = 0
End Sub
' When BackBox loses focus, re-display FrontBox – if the text in
' BackBox has changed, then FrontBox will follow the change
Private Sub BackBox_LostFocus()
FrontBox.Visible = True
End Sub
Test the form. When you click on FrontBox, it should disappear, letting you work on BackBox. When you click in OtherBox to remove the focus from BackBox, FrontBox should reappear. Any edits made in BackBox should be reflected in FrontBox.
Back in design mode, move FrontBox so it exactly covers BackBox, and click Position | Bring to Front to ensure that it covers BackBox. Now test the form again. It should appear that a single text box switches between display-the-last-few-lines mode and edit-the-entire-contents mode.
Simply put the following code after linefeed or on Change event txtStatus
txtStatus.SelStart = Len(txtStatus) - 1