Lua substring with word part (protection/skip) - lua

Need function which can do this stuff or something like that:
local text = "\affffffffEXPLOIT \aff404080CHARGING";
text = fnCutProtection(text, 0.5); -- "\affffffffEXPLOIT \aff404080";
text = fnCutProtection(text, 0.3); -- "\affffffffEXPL\aff404080";
Tried to do substring and then just check the length of cut off and do a replace with colored text, but still not good for me

Related

How to map many items

I'm trying to make a function that maps letters of the alphabet to other letters and I'm wondering if there is a simple way to do this or do I need to make a dictionary for each individual letter
Not sure what language but you could convert the character to it's unicode value and do some arithmetic function on to it e.g. in JS
var letter = "a";
var code = letter.charCodeAt(0);
// should do some checking to make sure you stay in letter range
code = code + 4;
return String.fromCharCode(code);

How to calculate the spaces between texts according to rectangle width?

I would like to ask you, how to calculate the text position, more like the spaces between texts.
Ive got an array with texts text1, text2, text3, text4... And I can call a function to get the length of the text in pixels and I also know the length of the rectangle and I would like to calculate the spaces between each text to entirely fill the rectangle and only keep 10px from both sides.
Function to get the text length is dxGetTextWidth and rectangle width is specified in a variable called rWidth.
How can I calculate it?
A Text Justification Algorithm can do what your looking for.
https://www.rose-hulman.edu/class/csse/csse221/200910/Projects/Markov/justification.html
Here is an example based on your question:
local output_width = 0
local line_width = box.rWidth() - 20 --10px from each side
local line = {}
for text in ipairs(texts) do
text_width = text.dxGetTextWidth()
if (output_width + text_width <= line_width) then
output_width = output_width + text_width
line[#line + 1] = text
else
remaining_space = line_width - output_width
space_width = remaining_space / #line --space evenly spread over words in the line
for text in ipairs(line) do
--now add the space between each word
end
end
end

How to wrap text without showing hyphenation characters (while using the same word split rules)?

I am trying to achieve word wrapping with hyphenation but do not want to see the hyphenation character in the uitextview.
In viewDidLoad;
textFieldParagraphStyle.lineBreakMode = NSLineBreakMode.ByWordWrapping
textFieldParagraphStyle.alignment = NSTextAlignment.Center
textFieldParagraphStyle.hyphenationFactor = 1.0
Everything works well but is it possible to get rid of hyphenation character with preserving the correct syllable wrapping?
To illustrate, suppose we have the word understand. If I use word wrapping with hypenation factor set 1.0, the uitextview divided the word like;
under-
stand
What I want to achieve is;
under
stand
Here is a code snippet. Read about CFStringGetHyphenationLocationBeforeIndex here to understand how the method works. Hope it helps. If your text view width allows to have multiple words in a line, then probably you should define at what syllable in the word you should make hyphenation, you can change location and limitRange to get other hyphens, not only in the word's "middle", like in my example.
- (NSString*)hyphenatedWordFromWordString:(NSString*)word
{
CFStringRef strRef = (__bridge CFStringRef)word;
CFIndex location = _textView.text.length;
CFRange limitRange = CFRangeMake(0, location);
CFOptionFlags flags = 0;
CFLocaleRef locale = (__bridge CFLocaleRef)[NSLocale currentLocale];
CFIndex index = CFStringGetHyphenationLocationBeforeIndex(strRef, location, limitRange, flags, locale, NULL);
if (index != kCFNotFound) {
word = [NSString stringWithFormat:#"%#\n%#", [word substringToIndex:index], [word substringFromIndex:index]];
}
return word;
}

Move Cursor One Word(including language like Chinese) at a Time in UTextView

I have read this,but it can only work well in English for it just use white-space and something like NewlineCharacterSet as separator.
I want to add a left arrow and a right arrow in the accessory input view to move the cursor in UITextView by words.
And I am wondering how to support that feature for some Asian languages like Chinese
PS:I will added an example that CFStringTokenizer failed to work with when there are both English Characters and Chinese characters
test string:
Happy Christmas! Text view test 云存储容器测试开心 yeap
the expected boundaries:
Happy/ Christmas!/ Text/ view/ test/ 云/存储/容器/测试/开心/ yeap/
the boundaries show in reality:
Happy/ Christmas!/ Text/ view/ test/ 云存储容器测试开心/ yeap/
I don't speak Chinese, but according to the documentation,
CFStringTokenizer is able to find word boundaries in many languages,
including Asian languages.
The following code shows how to advance from one word ("world" at position 6)
to the next word ("This" at position 13):
// Test string.
NSString *string = #"Hello world. This is great.";
// Create tokenizer
CFStringTokenizerRef tokenizer = CFStringTokenizerCreate(NULL,
(__bridge CFStringRef)(string),
CFRangeMake(0, [string length]),
kCFStringTokenizerUnitWordBoundary,
CFLocaleCopyCurrent());
// Start with a position that is inside the word "world".
CFIndex position = 6;
// Goto current token ("world")
CFStringTokenizerTokenType tokenType;
tokenType = CFStringTokenizerGoToTokenAtIndex(tokenizer, position);
if (tokenType != kCFStringTokenizerTokenNone) {
// Advance to next "normal" token:
tokenType = CFStringTokenizerAdvanceToNextToken(tokenizer);
while (tokenType != kCFStringTokenizerTokenNone && tokenType != kCFStringTokenizerTokenNormal) {
tokenType = CFStringTokenizerAdvanceToNextToken(tokenizer);
}
if (tokenType != kCFStringTokenizerTokenNone) {
// Get the location of next token in the string:
CFRange range = CFStringTokenizerGetCurrentTokenRange(tokenizer);
position = range.location;
NSLog(#"%ld", position);
// Output: 13 = position of the word "This"
}
}
There is no CFStringTokenizerAdvanceToPreviousToken() function, so to move to
the previous word you have to start at the beginning of the string and advance forward.
Finnally I use UITextInputTokenizer to realized the function

How can I put string in other string by position

My string is 'Hllo'.
I want to put inside it 'e' after the 'H' by its position, this case, position number 2.
local str = 'Hllo'
str = str:gsub('()',{[2]='e'})
You can simply cut contents until position you want to place your character on, then add the character and finally concat the characters on and after position.
src = "Hllo"
result = string.sub(src, 1, string.find(src, "H")) .. "e" .. string.sub(src, string.find(src, "H")+1)
The first part of code gets position of 'H' andf cuts the start (in this case 'H' only).
Second part adds character you want to insert. Third part adds every character after 'H' in source string to result.
you can try this out
$arr = str_split('hllo',1);
$result=$arr[0].'e'.$arr[1].$arr[2].$arr[3]

Resources