RTL 在行尾显示数字

发布于 2024-08-20 11:01:31 字数 228 浏览 4 评论 0原文

尝试显示以数字开头的希伯来语字符串,总是在字符串末尾显示数字,如下所示: 1. йום שושй בבוקר

但我需要将数字显示在文本的右侧 -

任何解决方案?

它发生在 UILabel & 上。 UITextField & UITextView

并尝试在左侧写入数字也会产生相同的结果。

使用 UITextAlignment 的组合不会有帮助。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

|煩躁 2024-08-27 11:01:31

您不需要更改 UILabel 上的任何设置,只需将 unicode 0x200F 的字符放在字符串之前即可。原因是:

在 Unicode 中,许多字符都有特定的方向性,这让系统知道它必须被写入,例如 LTR,如 সু。段落通常使用其第一个字符的方向。这就是为什么不带数字的字符串会自动从右到左输入。

现在,有些字符(例如数字)具有“弱”方向性,因此它们基本上采用周围环境的方向性。当您键入“1.בבוקר”时,系统首先看到 1,因此采用通常的 LTR 方向。更改对齐方式没有帮助,因为它只会将整个文本向右或居中移动。

为了解决这个问题,Unicode 有两个标记字符(LTR:0x200E,RTL:200F)。这些是看不见的,但决定了方向性。因此,虽然“1.בבוקר”是...

  1. בבוקר,

但如果您输入“#x200F”+“1.בבוקר”,它将显示如下:

‏1。呱呱啕姹

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...

  1. בבוקר

if you type "#x200F" + "1. בבוקר" it will display like this:

‏1. בבוקר

放飞的风筝 2024-08-27 11:01:31

以 Mo 的精彩答案为基础:

这是代码 Obj-C:

NSString *RTFstr = "1. בבוקר"; //This could be any right-to-left string
NSString *directionalString = [@"\u200F" stringByAppendingString:[note text]];
[someUITextView setString:directionalString];

它实际上有效......

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...

半﹌身腐败 2024-08-27 11:01:31

我遇到了一个稍微不同的问题,但莫的回答给了我线索。

我想要获得以 RTL 方向 ("dcba") 显示的 LTR 文本(如 "abcd"),而不必自己进行字符串反转。事实证明,将字符串括在 \u202E\u202C 之间就可以了。

我还建议阅读以下页面,因为它对所有这些 un​​icode 魔法提供了很好的解释:

http://www.iamcal .com/understanding-bi Direction-text/

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/

梦醒灬来后我 2024-08-27 11:01:31

斯威夫特有人吗?

extension String {
    func stringByForcingWritingDirectionLTR() -> String {
        return "\u{200E}".stringByAppendingString(self)
    }

    func stringByForcingWritingDirectionRTL() -> String {
        return "\u{200F}".stringByAppendingString(self)
    }
}

Swift anybody?

extension String {
    func stringByForcingWritingDirectionLTR() -> String {
        return "\u{200E}".stringByAppendingString(self)
    }

    func stringByForcingWritingDirectionRTL() -> String {
        return "\u{200F}".stringByAppendingString(self)
    }
}
没有你我更好 2024-08-27 11:01:31

不确定是否有更奇特的方法来做到这一点,但你可能想尝试这样的事情:

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."

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."
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文