Delete text in textbox without deleting the textbox on canvas in fabric.js - fabricjs

I'm using fabric.js and have IText textboxes on canvas. The user can freely move the textbox around and also edit the text within. How to avoid a textbox being deleted (removed from the canvas) when the user deletes the text using delete key and there's no more text?
Thanks for the response.
deleteText(activeObject) {
var startIndex = activeObject.selectionStart;
var endIndex = activeObject.selectionEnd;
if (endIndex > 0 && startIndex != endIndex) {
var text = activeObject.text;
var newText =
text.substring(0, startIndex) +
text.substring(endIndex + 1, text.length);
activeObject.set("text", newText);
this.canvas.fire("object:modified");
} else {
this.canvas.remove(activeObject);
}
}

have 2 versions of answer
1.Your are deleting textbox using this line of code , try commenting it and see what happens
this.canvas.remove(activeObject);
2.Textbox is not being deleted , you can't see it because of white canvas background and emptiness inside textbox, since there is no text , try mousedowning a canvas and select a big area , check if you are able to select your textbox this way

Related

How to make a button with dynamic text using Phaser?

What is the workaround to create 3 buttons with the same design but different texts (short text, medium length text, long text):
a) do I need 3 different images as a background ? What if i don’t know the length of the text ?
b) or I can use a single image and shrink/stretch it horizontally/vertically somehow when a text needs more/less space to fit in?
I hope i don’t need hundreds of images for each button :)
I’m just a beginner. What is the best practice ?
Here is how I create a responsive button and text:
buttonSprite2 = game.add.sprite(352, 76,'button');
buttonSprite2.position.set(200, 0);
buttonSprite2.inputEnabled = true;
buttonSprite2.events.onInputDown.add(listener2, this);
var style = { font: "32px Arial", fill: "#ffffff", wordWrap: true, align: "center", backgroundImage:'button'};
buttonText2 = game.add.text(0, 0, "stop text", style);
buttonText2.wordWrapWidth = game.world.width - 400;
// trying to center the text within the button
buttonText2.position.set(buttonSprite2.x + 100, buttonSprite2.y+15);
// trying to make the button responsive
if(buttonText2.width < game.world.width - 400){
buttonSprite2.width = buttonText2.width + 200;
}
else{
buttonSprite2.width = game.world.width - 300;
buttonSprite2.height = buttonText2.height + 30;
}
You need to load(in preload function) image only once. But you need to add(in create function) 3 images for 3 buttons.
For changing size of this button images according to text size. First you add image then add text. Now, after adding text, check for height or width of that text object and change size of the button image according to the height and width of text.
You may define seperate function to do this. So, you can get rid of redundant code if you have too many buttons.

How can I adjust the NavigationBar height to show an inserted SubView

I am trying to insert a sub view in the NavigationBar that needs to appear on all screens across the app. The code used to insert is below
ConnectionBarController statusBar = (ConnectionBarController)UIStoryboard.FromName("Main", null).InstantiateViewController("ConnectionBarController");
if (NavigationController != null)
{
this.NavigationController.View.Add(statusBar.View);
// var frame = this.NavigationController.View.Frame;
// frame.Height += ConnectionBarController.FrameHeight;
// this.NavigationController.View.Frame = frame;
}
It seems to appear, but the NavBar is cut off. Any suggestions on how to get this working?
I cant post a screen shot, as I am short of 2 reputations. Let me know if anyone needs to see the screenshot

How to align RIGHT the content of TextArea in LWUIT?

I want to align the text in a TextArea to the right. I tried the following code:
Form form = new Form();
TextArea textArea = new TextArea("Some Arabic text ...");
textArea.setRTL(true);
textArea.setAlignment(RIGHT);
form.addComponent(textArea);
The result was just moving the scroll to left,
But the text is still not aligned RIGHT,
check the image below:
So how to align the content to the RIGHT ?
It may sound crazy for the first instance :) but setting the alignment to TextArea.LEFT solved the issue and now it's RIGHT aligned !
Form form = new Form();
TextArea textArea = new TextArea("Some Arabic text ...");
textArea.setRTL(true);
textArea.setAlignment(TextArea.LEFT);
form.addComponent(textArea);
Setting it to LEFT makes the displayed text RIGHT aligned !
Or by removing the textArea.setRTL(true) which is mirroring the display
Form form = new Form();
TextArea textArea = new TextArea("Some Arabic text ...");
textArea.setAlignment(TextArea.RIGHT);
form.addComponent(textArea);
For those who are interested in more complicated details when it's set to RTL:
the paint method of TextArea class is
public void paint(Graphics g) {
UIManager.getInstance().getLookAndFeel().drawTextArea(g, this);
}
And drawTextArea method in DefaultLookAndFeel is as follows:
int align = ta.getAbsoluteAlignment();
// remaining code is here in initial source
switch(align) {
case Component.RIGHT:
x = ta.getX() + ta.getWidth() - rightPadding - f.stringWidth(displayText);
break;
// remaining code is here in initial source
}
g.drawString(displayText, x, y);
Unfortunately TextArea.RIGHT value is 3
But when calling ta.getAbsoluteAlignment() it returns 1 (despite that the object's alignment is set by code to TextArea.RIGHT !!)
Meanwhile TextArea.Left value is 1
That's why it matched the value in the switch and was aligned to RIGHT
BTW, if you set
textArea.setAlignment(Component.RIGHT);
it will also be wrong, because Component.RIGHT outside the paint method has the value 3 not 1 !
You only have to write 'TextArea.RIGHT' instead of 'RIGHT'
textArea.setAlignment(TextArea.RIGHT);
You can use the following line:
TextArea textArea = new TextArea("Some Arabic text ...");
textArea.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);

Label does not render the string "0" (Flex 4.5)

I was writing a custom chart component where. To add labels, I create a spark label and add it on screen. Although all other labels rendered, I noticed that the zero label does not render at all. Here's my code:
var invisibleTextField:TextField = new TextField();
var zeroLabel:spark.components.Label = new spark.components.Label();
zeroLabel.text = "0";
zeroLabel.name = "0Label";
invisibleTextField.text = " 0 ";
zeroLabel.width = invisibleTextField.textWidth;
zeroLabel.height = invisibleTextField.textHeight;
addChild(zeroLabel);
After multiple attempts, I figure that the label discards "0" as an empty string. I managed to workaround using spaces before and after the zero. Only one space would also do, but I needed center aligning. Anyone has any idea why this happens?
There's a delay between text is filled into a text field and when it's dimensions are updated. Try using TextExtent instead of an invisible text field to do what you're doing.
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/text/TextExtent.html

How to Change text color?

Hi
I am using following code to append text and I want to change the color of newly appended text how is it possible
if (strMessage.IndexOf("pvt|") == -1)
{
string[] temp = strMessage.Split(new string[] {"&^:^&"}, tringSplitOptions.None);
strMessage = temp[0] + "(" + DateTime.Now.ToString("HH:mm tt") + ")" + ":" + emp[1];
txtLog.AppendText(strMessage + "\r\n");
}
Please help..
Where is the text being displayed? It appears that it goes to a TextBox, but that's just an assumption.
Changing the color of individual chunks of text within a normal TextBox is not possible. You can change the entire TextBox's forecolor by setting its ForeColor property.
txtLog.ForeColor = Color.Red;
To change pieces of it individually, then you will need to use a different textbox, such as shown in the answer here.
Assuming that txtLog is a text file you would have to encode ascii control characters into stream and have an editor that read them. This link might help.

Resources