how to make a Text prevent the inputs more than 'x' number of digits? - textbox

My question is:
I want the Text field in Eclipse RCP to allow only the digits to be keyed in as inputs and the requirement is that it should allow only up to 'x' (let's say x=5) characters.
How can I accomplish it in RCP ?
I've tried the code like:
txtInput.addListener(SWT.Verify, new DecimalText(
txtInsuranceValue, 8, 1000.00));
where txtInputis a Text field. But this listener failed when I made repeated input into this field at run time.
Any alternatives please ?

This will set the maximum number of characters in your Text control to five and allow only digits to be entered:
txtInput.setTextLimit(5);
txtInput.addVerifyListener(new VerifyListener() {
public void verifyText(VerifyEvent event) {
// Assume we allow it
event.doit = true;
String text = event.text;
char[] chars = text.toCharArray();
//Don't allow if text contains non-digit characters
for (int i = 0; i < chars.length; i++) {
if (!Character.isDigit(chars[i])) {
event.doit = false;
break;
}
}
}
});
EDIT: I've modified my answer to allow a user to also set text with "setText(String string)" while still disallowing non-digit characters.

In general, solutions like adding verifyListeners/focusListeners are not complete and won't work with all situations. For example, what happens when the user copy-pastes some text into this text box?
If all you want is to get numerical inputs from the user, you should be using Spinner widget instead of Text widget. You could easily set the minimum and maximum limits on the Spinner.
See the snippets for more usage.

Related

Google Sheets formula that changes text only between parenthesis

I'm trying to format a sheet that creates a grid between names; the names are automatically mirrored on one axis using the = link method so that it's user-friendly.
However, linking cells via = doesn't copy any formatting and only uses the native formatting of the cell. I want half of the text to be a different color (lighter grey and not bold, ideally). Since you cannot adjust the text of a mirrored cell and it doesn't mirror formatting, I was thinking I could backdoor it through a formula.
The wise thing would be to either swallow that it doesn't work that way and go with the uglier formatting, or to unlink them and simply allow the user to manually enter the data for neatness. However, I figured it'd be a good learning exercise if I could figure out a way to selectively format only text within parentheticals.
Is there a way to use a formula to color text only within parenthesis?
To format the text in parentheses, try
function formatParenthesis() {
const range = SpreadsheetApp.getActiveRange()
const spec = { regex: /\(.*\)/gi, textColor: 'grey', bold: true, italic: true };
const values = range.getDisplayValues();
let match;
const formattedText = values.map(row => row.map(value => {
const richText = SpreadsheetApp.newRichTextValue().setText(value);
const format = SpreadsheetApp.newTextStyle()
.setForegroundColor(spec.textColor)
.setBold(spec.bold)
.setItalic(spec.italic)
.build();
while (match = spec.regex.exec(value)) {
richText.setTextStyle(match.index, match.index + match[0].length, format);
}
return richText.build();
}));
range.setRichTextValues(formattedText);
}
references
RichTextValueBuilder
setTextStyle

pdfmake problem displaying accurate page numbers

I'm using pdfmake to generate a document that contains a number of sub-documents, e.g. a PDF containing invoices; each invoice is a logical document inside the PDF document.
I have a requirement that in the footer or header of each page I show "Page of " where both those numbers are relative to a single invoice, and not the overall document.
The header and footer callbacks look good, but only specify the page number and count relative to the entire document, and the pageBreakBefore callback doesn't generate anything like the documented information, and in any case I can't figure out how I could use it here.
This doesn't seem like a unique requirement, so hopefully I'm just missing something obvious.
Thanks!
I believe that pdkmake's header and footer function's arguments can only contain the global pageCount.
There is however a way to reproduce the behavior that you want, if you handle manually the pageBreaks :
const realPageIndexes = [];
let currSubPdfIdx = 0;
let currPageCountForSubPdf = 0;
const getPageBreak = (newSubPdfIdx: number) => {
if (currSubPdfIdx !== newSubPdfIdx) {
currSubPdfIdx = newSubPdfIdx;
currPageCountForSubPdf = 0;
} else {
currPageCountForSubPdf += 1;
}
realPageIndexes.push(currPageCountForSubPdf);
return {text: '', pageBreak: 'after'}; // return elem causing the pageBreak
}
The footer function, filling the page numbers, is called once the document definition generation is done.
If you handle every overflow by yourself, by calling getPageBreak(currentSubPdfIndex) at the end of each page, you will end up knowing which sub-Pdf is displayed in each page :
I display part of the 1rst subPdf in page 1:
I display the end of 1rst subPdf in page 2
I display 2nd subPdf in page3
I display 3rd subPdf in page4 ....
.
let subPdfIdx = 0;
pdfContent.push(subPdf1FirstPart)
pdfContent.push(getPageBreak(subPdfIdx))
pdfContent.push(subPdf1SecondPart)
pdfContent.push(getPageBreak(subPdfIdx))
subPdfIdx++;
pdfContent.push(subPdf2)
pdfContent.push(getPageBreak(subPdfIdx))
subPdfIdx++;
pdfContent.push(subPdf3)
pdfContent.push(getPageBreak(subPdfIdx))
realPageIndexes[] then looks like :
[ 1, 1, 2, 3 ];
The only step left is to tell the footer function to use the page counts we just created instead of the global page count :
const documentDefinition = {
content: [YOUR CONTENT],
footer: (currPage, allPages) => 'subPdf index is ' + realPageIndexes[currPage];
}
Please note that this method will fail if you don't handle overflows correctly:
if for example a paragraph is too big to fit a page, and you call getPageBreak() after that, pdfmake will automatically create a new page for the end of the paragraph (page which is untracked in our realPageIndexes) and then add the page caused by your getPageBreak() right after the end of the text. So just make sure not to overflow the page :)

how to check if a text label is greater than another text label in Xcode

I am relatively new to xCode and I have 2 integer text labels Text1 and Text2 text.
I'm looking for some code that would compare if Text1.text is greater than Text2.text and then if another text field Text3.text would be equal to the value of Text1.text.
Appreciate any help.
In the attributes inspector I assume. Select the text, go to attributes inspector (top right corner at the utility area, forth in a row. Looks like a square head with ears) and check the size.
Well, if you want xcode to check the size, you'll need to make an if or switch statement. Use enums like .fontSize
The text fields are just views that display a string.
You can access the string that they contain via the text property:
NSString *text1 = _text1.text;
Now you have the string that is displayed in the textview ( "34" for example)
You can use the NSString method intValue to turn this string into an integer:
int text1Value = [text1 intValue];
You can now compare the value of this to another integer:
if (text1Value > 42) {
_textLargest.text = [NSString stringWithFormat:"%d", textValue1];
}
Main points:
Text fields are just views; you need to work with the data they are displaying rather than treat them as objects
You need to use the correct type for your comparison. The text field contains text but you need to convert it to a number to make it meaningful
You need to convert the number back into a string if you want it to appear in the text field's text property [NSString stringWithFormat:"%d", textValue1]
You can take and compare the int values as follows
if([Text1.text intValue] > [Text2.text intValue] )
{
Text3.text=[NSString stringWithFormat:"%#", Text1.text];
}

Why are the ranges for my formatted text shifting in iOS 7?

My app includes a text formatting tool that offers buttons for things like bold, italic and color and shows the formatted text by generating an NSAttributedString and setting that to the attributedText property of a UITextView. After the user selects text and taps a button, I get the selectedRange property of the UITextView, then get the current attributedText property of the UITextView, add another attribute to the text based on the selected range, and then assign it back to the attributedText property of the UITextView again.
Starting with iOS 7, my text formatting started displaying at the wrong location in the text, usually shifted a couple characters forward. After some testing I noticed that this only happened after an empty line (e.g., a paragraph of text with two line breaks after it) and the formatting was offset by one character for each empty line proceeding it.
After more testing I found that when I set the attributedText property for the first time, any sequence of two line breaks is changed to a line break, then a "line separator" character (Unicode 8232) and then the second line break. The new characters are definitely added by the attributedText assignment, as I can see from outputting the integer value of each character immediately before and immediately after that action. However, the selectedRange property of the UITextView ignores the line separator characters, so any range that it returns is now incorrect.
I've already found a workaround, which I'll add as an answer in a moment. I'm mainly posting this in case anyone else is having problems with it. Also, I've reported this to Apple as bug 15349335.
I wrote this method to adjust ranges returned by the selectedRange property to account for these extra line separator characters:
- (NSRange)adjustRangeForEmptyLines:(NSRange)range inText:(NSAttributedString *)text byChars:(int)chars {
int emptyLinesBeforeRange = 0;
int emptyLinesWithinRange = 0;
for (int i=0; i<(range.location + range.length); i++) {
int thisCharacter = [text.string characterAtIndex:i];
//NSLog(#"thisCharacter: %i", thisCharacter);
if (thisCharacter == 8232) {
if (i < range.location) {
emptyLinesBeforeRange++;
} else {
emptyLinesWithinRange++;
}
}
}
//NSLog(#"found %i + %i empty lines", emptyLinesBeforeRange, emptyLinesWithinRange);
range.location += (emptyLinesBeforeRange * chars);
range.length += (emptyLinesWithinRange * chars);
return range;
}
I can set the byChars argument to 1 or -1 depending on which way I want to adjust. This has been working for me for a few weeks now, but if anyone has an alternate solution, I'd be curious to see it.

conditional formatting not working

(am using PHPExcel 1.7.8)
I create a sheet and populate it with a table of numeric data. Then Excel refuses to apply conditional formatting of the 3-color-transition type to the numeric data.
That means, too:
1) I can use conditional formatting of the "top 10 values" kind
2) If I populate right next to the generated table few cells with numbers then I can use the cond formatting of transitional kind
3) If I copy two cells from the generated table somewhere else in a simple copy/paste fashion still can't use the transitional formatting
4) If I copy two cells from the generated table somewhere else using the "just the value"-pasting I do can use the transitional formatting
Another important observation here is, that when I select a cell of the generated table and click into the value bar - right at that moment - the cell changes its color to what it should be regarding the conditional formatting!
This is somewhat similar to another phenomena I observed with PHPExcel generated spreadsheets. Sometimes it happens that, when I double click into a cell to get into editing mode - the cell turns pit black. But still I can change the value.
There seems to be something wrong with how a cell is represented in the Excel-file I guess. Something related to the control of the coloring ... !?
I could of course copy/(value-)paste everything. But maybe I am just using PHPExcel the wrong way? Or there is a quick way to convert an Excel file at once in a useful way?
The full code I use is this:
$excelWorkbook = null;
if(file_exists($filename)) {
$reader = PHPExcel_IOFactory::createReader("Excel2007");
$excelWorkbook = $reader->load($filename);
} else {
$excelWorkbook = new PHPExcel();
}
$sheet = $excelWorkbook->getSheetByName($tabName);
if ($sheet !== null) {
$excelWorkbook->removeSheetByIndex($excelWorkbook->getIndex($sheet));
}
$sheet = new PHPExcel_Worksheet($excelWorkbook, $tabName);
$sheet = $excelWorkbook->addSheet($sheet);
$columns = array_keys($targetArray);
$rows = array_keys($targetArray[$columns[0]]);
for($i = 0; $i < count($columns); $i++){
$sheet->setCellValueByColumnAndRow($i+1,1,$columns[$i]);
}
for($i = 0; $i < count($rows); $i++){
$sheet->setCellValueByColumnAndRow(0,$i+2,$rows[$i]);
}
for($i = 0; $i < count($columns); $i++){
for($j = 0; $j < count($rows); $j++) {
$sheet->setCellValueByColumnAndRow($i+1, $j+2, $targetArray[$columns[$i]][$rows[$j]]);
}
}
$excelWorkbook->setActiveSheetIndex($excelWorkbook->getIndex($sheet));
$xlsx = new PHPExcel_Writer_Excel2007($excelWorkbook);
$xlsx->save($filename);
The workaround-type solution is to select all cells in the respective sheet and choose for background color 'none'.
Apparently the cells are colored white and this coloring overlays the conditional coloring.
This may be a bug due to the fact that for solid fills in cells Excel reverses the meaning of foreground and background colours but in conditional (dxf) formats it doesn't.
Or, to put it another way background and foreground are stored differently for cell and conditional formats.
I encountered this issue in the Perl Excel::Writer::XLSX module.

Resources