Xcode mixing RTL and LTR (Hebrew and English) for strings - iphone

I would like to initialize a set of string with hebrew chars like this:
NSString *hebStr = #" <some hebrew> ";
The problem i am having is when I type the "some hebrew" in Xcode it comes out as "werbeh emos" Left-to-Right (LTR) instead of Right-to-Left (RTL).
How do I tell Xcode that what I want for that string only is RTL?

Related

Access index of a list in Flutter/Dart

I would like to access an index of a string (e.g. 'Hello') and print it to the app (e.g. letter 'o'). This string has to be the input of the user.
This can be easily done using Python but I have to use Flutter/Dart in order to do it (mobile development).
Here is just an example of how this would be solve using Python:
my_word = input("Insert your word here: ")
print(my_word[4])
Use TextField to retrieve user's input:
A text field lets the user enter text, either with hardware keyboard
or with an onscreen keyboard.
Use Text to display the string that you retrieve:
The Text widget displays a string of text with single style.
Use the [] (index) operator to get the character at the index you specify:
final str = "Hello World";
print(str[4]); // Prints o
You can try split the string's letters in an array first.
my_word.split('')[4]
main() {
String val = "hello";
print(val[4]);
}

How to construct a unicode glyph key in C#

I am using FontAwesome to display glyphs in my Xamarin Android application. If I hardcode the glyph like this, where everything works fine:
string iconKey = "\uf0a3";
var drawable = new IconDrawable(this.Context, iconKey, "Font Awesome 5 Pro-Regular-400.otf").Color(Xamarin.Forms.Color.White.ToAndroid()).SizeDp(fontSize);
However, if what I have is the four letter code "f0a3" from FontAwesome's cheatsheet, stored in a string variable, I don't know how to set my iconKey variable to a value that works. Just concatenating a "\u" onto the beginning doesn't work, which makes sense since that's a Unicode escape indicator, not part of a standard string, but I don't know what to do instead. I also tried converting to and from Unicode in various random ways - e.g.
iconKey = unicode.GetChars(unicode.GetBytes("/u" + myFourChar.ToString())).ToString();
but unsurprisingly that didn't work either.
The IconDrawable is from here. The value I send becomes an input there to the Paint.GetTextBounds method and the Canvas.DrawText method.
Thanks for any assistance!
Found the answer here. Here is the code I am using, based on that post but simplified since I have only one hexadecimal character to handle:
string myString = "f0a3";
var chars = new char[] { (char)Convert.ToInt32(myString, 16) };
string iconKey = new string(chars);
var drawable = new IconDrawable(this.Context, iconKey, "Font Awesome 5 Pro-Regular-400.otf").Color(Xamarin.Forms.Color.White.ToAndroid()).SizeDp(fontSize);

Can't unescape escaped string with ABAP

I want to escape this string in SAPUI5 like this.
var escapedLongText = escape(unescapedLongText);
String (UTF-8 quote, space, Unicode quote)
" “
Escaped string
%22%20%u201C
I want to unescape it with this method, but it returns empty. Any ideas?
DATA: LV_STRING TYPE STRING.
LV_STRING = '%22%20%u201C'.
CALL METHOD CL_HTTP_UTILITY=>UNESCAPE_URL
EXPORTING
ESCAPED = LV_STRING
RECEIVING
UNESCAPED = LV_STRING.
I changed the code in SAPUI5 to the following:
var escapedLongText = encodeURI(unescapedLongText);
This results in: (like andreas mentioned)
%22%20%e2%80%9c
If I want to decode it later in SAPUI5, it can be done like this:
var unescapedLongText = unescape(decodeURI(escapedLongText));
The unescape needs to be done, because commas (for example) don't seem to be decoded automatically.

Facebook Graph API - non English album names

I am trying to do a simple thing - get all my albums.
the problem is that the album names are non-English ( they are in Hebrew ).
The code that retrieves the albums :
string query = "https://graph.facebook.com/me/albums?access_token=...";
string result = webClient.DownloadString(query);
And this is how one of the returned albums looks like :
{
"id": "410329886431",
"from": {
"name": "Noam Levinson",
"id": "500786431"
},
"name": "\u05ea\u05e2\u05e8\u05d5\u05db\u05ea \u05d2\u05de\u05e8 \u05e9\u05e0\u05d4 \u05d0",
"location": "\u05e9\u05e0\u05e7\u05e8",
"link": "http://www.facebook.com/album.php?aid=193564&id=500786431",
"count": 27,
"type": "normal",
"created_time": "2010-07-18T06:20:27+0000",
"updated_time": "2010-07-18T09:29:34+0000"
},
As you can see the problem is in the "name" property. Instead of Hebrew letters
I get those codes (These codes are not garbage, they are consistent - each code probably represents a single Hebrew letter).
The question is , how can I convert those codes to a non-English language ( In my case, Hebrew).
Or maybe the problem is how I retrive the albums with the webClient object. maybe change webclient.Encoding somehow?
what can I do to solve this problem ?
Thanks in advance.
That's how Unicode is represented in JSON (see the char definition in the sidebar). They are escape sequences in which the four hex digits are the Unicode code point of the character. Note that since there's only four hex digits available, only Unicode characters from the BMP can be represented in JSON.
Any decent JSON parser will transform these Unicode escape sequences into properly encoded characters for you - provided the target encoding supports the character in the first place.
I had the same problem with Facebook Graph Api and escaped unicode Romanian characters. I have used PHP but, you probably can translate the regexp method into javascript.
Method 1 (PHP):
$str = "\u05ea\u05e2\u05e8\u05d5\u05db\u05ea";
function esc_unicode2html($string) {
return preg_replace('/\\\\u([0-9a-z]{4})/', '&#x$1;', $string);
}
echo esc_unicode2html($str);
Method 2 (PHP) and probaby it works also if u declare the charset directly in the html:
header('content-type:text/html;charset=utf-8');
These are Unicode character codes. The \u sequence tells the parser that the next 4 characters are actually form a unicode character number. What these characters look like will depend on your font, if someone does not have the correct font they may just appear as a lot of square boxes.
That's about as much as I know, Unicode is complicated.
For Hebrew texts, this code in PHP will solve the problem:
$str = '\u05ea\u05e2\u05e8\u05d5\u05db\u05ea \u05d2\u05de\u05e8 \u05e9\u05e0\u05d4 \u05d0';
function decode_encoded_utf8($string){
return preg_replace_callback('#\\\\u([0-9a-f]{4})#ism', function($matches) { return mb_convert_encoding(pack("H*", $matches[1]), "UTF-8", "UCS-2BE"); }, $string);
}
echo decode_encoded_utf8($str); // will show (תערוכת גמר שנה א) text
For Arabic texts use this:
$str = '\u00d8\u00ae\u00d9\u0084\u00d8\u00b5';
function decode_encoded_utf8($string){
return preg_replace_callback('#\\\\u([0-9a-f]{4})#ism', function($matches) { return mb_convert_encoding(pack("H*", $matches[1]), "UTF-8", "UCS-2BE"); }, $string);
}
echo iconv("UTF-8", "ISO-8859-1//TRANSLIT", decode_encoded_utf8($str));

RTL shows numbers at the end of lines

Trying to display a hebrew string that starts with a number, always displays the number at the end of the string like so: 1. יום שישי בבוקר
but I need the number to be displayed at the right side of the text-
any solution to that?
It happens with UILabel & UITextField & UITextView
and trying to write the number at the left side also produce the same resault.
Playing with combinations of UITextAlignment will doesn't help.
You don't need to change any setting on UILabel, just put the character with unicode 0x200F before your string. This is the reason:
In Unicode many characters have a specific directionality, which lets the system know it has to be written, say LTR, like سلام. The paragraph usually uses the direction of its first character. That's why your string without the number is typed from right to left automatically.
Now some characters, like numbers, have "weak" directionality, so they basically take that of their surrounding. When you type "1. בבוקר", the system first sees 1, so takes the usual LTR direction. Changing the alignment won't help, as it just shifts the whole text to right, or center.
To solve this issue, Unicode has two marker characters (LTR: 0x200E, RTL:200F). These are invisible, but dictate the directionality. So while "1. בבוקר" is...
בבוקר
if you type "#x200F" + "1. בבוקר" it will display like this:
‏1. בבוקר
Building on Mo's great answer:
This is the code Obj-C:
NSString *RTFstr = "1. בבוקר"; //This could be any right-to-left string
NSString *directionalString = [#"\u200F" stringByAppendingString:[note text]];
[someUITextView setString:directionalString];
And it actually works...
I had a slightly different problem but Mo's answer gave me the clue.
I wanted to get a LTR text (like "abcd") displayed in RTL direction ("dcba") without having to do myself the string reversing. Turns out enclosing the string between \u202E and \u202C does the trick.
I also recommend reading the following page as it gives a very good explanation of all these unicode magic:
http://www.iamcal.com/understanding-bidirectional-text/
Swift anybody?
extension String {
func stringByForcingWritingDirectionLTR() -> String {
return "\u{200E}".stringByAppendingString(self)
}
func stringByForcingWritingDirectionRTL() -> String {
return "\u{200F}".stringByAppendingString(self)
}
}
not sure if there's fancier way to do this but you might want to try something like this:
NSString *test = #"12. just a teststring";
NSString *number = [test substringToIndex: [test rangeOfString: #" "].location];
NSString *text = [test substringFromIndex: [test rangeOfString: #" "].location];
test = [NSString stringWithFormat: #"%# %#", text, number];
// test == "just a teststring 12."