AS3 TextField.textWidth ignore whitespace at end - whitespace

I'm trying to calculate the textWidth, but it seems to ignore the white spaces at the end. The text
"Hello"
"Hello "
returns the same text width. A string with " " returns 0. How do I calculate the width with the space?

You could always get the length of the string.
text.length
And then multiply that by how wide your characters are.
For example:
var aChar:String = "A";
var textWidth:int = (text.length) * (width of aChar);

Choose a terminator character, for Example "|".
Calculate Width(Text+Termainator)-Width(Terminator).

Related

In InDesign, is there a way to bold a whole word that has one bold character?

I'm working on an index in InDesign. Some of the page numbers are in bold, others are in italics or regular. During editing, somehow the first numbers of some of the bold page numbers got changed. I've figured out how to highlight those page numbers by coloring the bold numbers and recoloring the page numbers that are correct using a GREP search for bold words (\b\w+\b). What I can't figure out is how to select the "bad" page numbers that have only some numbers and make the entire "word" bold. Any ideas? It would be nice not to have to fix them manually.
I just tried this on a document and added a few numbers that were only partially bold.
I was able to fix it by doing a search for only digits with (\b\d+\b), changing all to $1. I left find format blank and change format to regular font. This changed all numbers to regular with no mixed bold and regular.
After that you can run the same find and replace again but switching format to bold. This will change all numbers to be fully bold.
It heavily depends on the text you have. If it's just one first digit that need to change, if you don't use character styles, if you have no digits in your body text, if the font you're using has the common names for styles, if ... there is a lot of 'if's, actually. I'd recommend to share a sample of your file (IDML).
So, here is the script that could do the job (if all of those "if"'s are true):
var doc = app.activeDocument;
var styles = doc.characterStyles;
// STEP 1 -- apply style1 (regular) to all regular numbers \d\d+
var style1 = styles.add();
style1.name = 'digits_regular';
style1.fontStyle = 'Regular';
app.findGrepPreferences = NothingEnum.nothing;
app.findGrepPreferences.findWhat = '\\b\\d\\d+'; // two or more digits
app.findGrepPreferences.fontStyle = 'Regular';
app.changeGrepPreferences.changeTo = '$0';
app.changeGrepPreferences.appliedCharacterStyle = style1;
doc.changeGrep();
// STEP 2 -- apply style2 (italic) to all italic numbers \d\d+
var style2 = styles.add();
style2.name = 'digits_italic';
style2.fontStyle = 'Italic';
app.findGrepPreferences = NothingEnum.nothing;
app.findGrepPreferences.findWhat = '\\b\\d\\d+';
app.findGrepPreferences.fontStyle = 'Italic';
app.changeGrepPreferences.changeTo = '$0';
app.changeGrepPreferences.appliedCharacterStyle = style2;
doc.changeGrep();
// STEP 3 -- apply style3 (bold) to all unstyled numbers
var style3 = styles.add();
style3.name = 'digits_bold';
style3.fontStyle = 'Bold';
app.findGrepPreferences = NothingEnum.nothing;
app.findGrepPreferences.findWhat = '\\b\\d\\d+';
app.findGrepPreferences.appliedCharacterStyle = styles[0]; // syle '[None]'
app.changeGrepPreferences.changeTo = '$0';
app.changeGrepPreferences.appliedCharacterStyle = style3;
doc.changeGrep();
// clean prefs
app.findGrepPreferences = NothingEnum.nothing;
Input:
Result:
Then you can remove the character styles you don't need them. But I'd recommend to use styles. They make the life easier exactly in such cases.
It's much easier to use the Find/Change interface in Indesign.

display "≤" character into a VB6 Label

I am trying to set ≤ character into a VB6 Label.
Looking at https://en.wikipedia.org/wiki/List_of_Unicode_characters
The code would be 2264 .
Label.Text = Chr(2264) generates an error
Label.Text = ChrW$(2264) sets a question mark "?"
Does anyone know how to get this ≤ character
Label.Text = ChrW(&H2264) ' <-- &H for Hexadecimal
2264 is the Hexadecimal code, you can see it here.
In Decimal, it is ChrW(8804).
The stock Symbol font contains that symbol at an ansi codepoint and so works without requiring unicode awareness, as a contrived example with 3 autosizing labels:
lbl_left.Caption = "999"
lbl_middle.Font.Name = "Symbol"
lbl_middle.Caption = ChrW$(&HA3)
lbl_middle.Left = lbl_left.Left + lbl_left.Width + Me.TextWidth(" ")
lbl_right.Caption = "1000"
lbl_right.Left = lbl_middle.Left + lbl_middle.Width + Me.TextWidth(" ")

ZPL - zebra: print justified text block without overwriting last line

I'm using the following command to print a justified text:
^FB1800,3,0,J^FT100,200^A0B,26,26^FH\^FDLONG TEXT TO BE PRINTED, WHICH DOESNT FIT IN ONLY 3 LINES...^FS
The command ^FB1800,3,0,J prints a field block in a width of 1800 dots, maximum 3 lines, justified.
The problem is that if the text exceeds the maximum number of lines, it overwrites the last line! :( That of course makes the text of the last line unreadable.
How can I avoid that? Does anybody know if is there a way to cut the exceeding text?
The documentation says exactly that this happens:
Text exceeding the maximum number of lines overwrites the last line. Changing the font size automatically increases or decreases the size of the block.
For reference: I'm using printer Zebra 220Xi4.
Any help would be appreciated. Thank you!
Take a look at the ^TB command. It is preferred over the ^FB command and truncates if the text exceeds the size defined in the TB params
I had just about the same problem, what fixed it in my case - although not the most elegant way - is to specify a higher number of maximum lines, and then formatting it in a way that only the first 3 are in the visible area.
In your case it would be for example ^FB1800,7,0,J instead of ^FB1800,3,0,J
This at least fixed it for me right away, because I print this text at the bottom of the label. If you need to have it somewhere in the middle or top, there might be some tricks with putting a (white) box on top of the overflow-area, since the Zebra printers seem to render before printing. Hope it helps.
Depending on the higher-level programming language you're using (assuming that you are), you could accomplish the same thing (truncate the text to be printed to a specified number of characters) with code like this (C# shown here):
public void PrintLabel(string price, string description, string barcode)
{
const int MAX_CAPS_DESC_LEN = 21;
const int MAX_LOWERCASE_DESC_LEN = 32;
try
{
bool descAllUpper = HHSUtils.IsAllUpper(description);
if (descAllUpper)
{
if (description.Length > MAX_CAPS_DESC_LEN)
{
description = description.Substring(0, MAX_CAPS_DESC_LEN);
}
}
else // not all upper
{
if (description.Length > MAX_LOWERCASE_DESC_LEN)
{
description = description.Substring(0, MAX_LOWERCASE_DESC_LEN);
}
}
. . .
This is what I'm using; is there any reason to prefer the "raw" ^TB command over this?

"New Scope" Macro for Visual Studio

I'm trying to create a new macro that takes the currently selected text and puts curly braces around it (after making a newline), while, of course, indenting as needed.
So, for example, if the user selects the code x = 0; and runs the macro in the following code:
if (x != 0) x = 0;
It should turn into:
if (x != 0)
{
x = 0;
}
(Snippets don't help here, because this also needs to work for non-supported source code.)
Could someone help me figure out how to do the indentation and the newlines correctly? This is what I have:
Public Sub NewScope()
Dim textDoc As TextDocument = _
CType(DTE.ActiveDocument.Object("TextDocument"), TextDocument)
textDoc.???
End Sub
but how do I figure out the current indentation and make a newline?
Sub BracketAndIndent()
Dim selection = CType(DTE.ActiveDocument.Selection, TextSelection)
' here's the text we want to insert
Dim text As String = selection.Text
' bracket the selection;
selection.Delete()
' remember where we start
Dim start As Integer = selection.ActivePoint.AbsoluteCharOffset
selection.NewLine()
selection.Text = "{"
selection.NewLine()
selection.Insert(text)
selection.NewLine()
selection.Text = "}"
' this is the position after the bracket
Dim endPos As Integer = selection.ActivePoint.AbsoluteCharOffset
' select the whole thing, including the brackets
selection.CharLeft(True, endPos - start)
' reformat the selection according to the language's rules
DTE.ExecuteCommand("Edit.FormatSelection")
End Sub
textDoc.Selection.Text = "\n{\n\t" + textDoc.Selection.Text + "\n}\n"
Of course the amount of \t before the { and } and Selection depend on the current indention.
Since there is a difference between selected Text and Document data, it's hard to find out where the cursor is within the document's data (at least in Outlook it is).
The only way I figured out how to do this in Outlook is to actually move the selection backwards until I got text that I needed, but that resulted in undesirable effects.
Try taking the selection start, and using that position in the document text, looking at that line and getting the amount of tabs.
I would think there wouldn't be formatting characters in VStudio.

Algorithm help: Fit a text blurb to its textbox by length

I have a rather unusual problem, and it is hurting my brain.
Problem: Given a textbox of known length, and the text that will go inside it, make the text "fit" by truncating it with room for "..." to fit inside the box. (Context : This is for ASP.NET C#, but I think the algorithm is language agnostic.)
Example : [_________]
Text : The big brown dog jumped over the red fence.
Solution :[The bi...]
Example : [_________]
Text : Ferret
Solution :[Ferret___]
Given:
// Returns the number of px (as an int) that the arg text is in length
public int textLength(String theText, float emSize)
Question: What is the simplest and fastest way to do this?
I am afraid to do it by hacking off one character at a time, adding "..." and then checking the length because some of the strings to fit are veeeeery long.
You could do a binary search on the correct length instead, which means you only have to try log(n) sizes.
Oh, also if the text is monospaced (every character is given the width of an em) then it's pretty easy to figure this out programatically:
if str.length * emWidth < textBoxWidth
tb.text = str
else
tb.text = substring(str, 0, round_down(textBoxWidth / emWidth) - 3) + "..."
Why start from end? Start from the beginning and add letters (and ...) until it no longer fits. :)

Resources