English 中文(简体)
你如何察觉到UIlabel在可可触摸中 含有右左语言?
原标题:How do you detect that a UILabel contains a right-to-left language in Cocoa Touch?

我以两个“http:// developmenter.apple.com/library/ios/#documentation/uikit/referation/UILabel_Class/Reference/UILabel.html” rel=“Nofolt noreferr”>>UIULabels 显示用户的评论,一个是用户名,另一个是信息。两个标签都有相同的 X,Y 坐标,但信息标签的预设空间与用户名的宽度相等。这一技术有助于我轻松地支持多线信息。

当用户写阿拉伯信件时会出现问题。 阿拉伯语是左方语言的右侧。 这意味着, 我必须在阿拉伯语字符串的 < em> end 上添加空格, 而不是开始。 正如您在下面的截图中看到的, 我并没有做出这种区分, 阿拉伯信件也进入了用户名 。 因此, 我如何检测到 UILabel 的文本属性( https:// developer. apple.com/ library/ mac/ # documentation/ Cocoa/ Reference/ Foundation/ Classes/ NSString_ Class/ Reference/ NSstring. html )? rel= “ nofolvey noreferr” >NSString

"https://i.sstatic.net/QrKcB.png" alt="arabic"/>

Fail attempt 1: Regex

我试过使用",http://developmenter.apple.com/library/mac/#document/Foundation/Referation/Reference/NSRegularExpression_Class/Reference/Reference.html" rel=“不跟随 noreferrer”>regexes ,但以下函数总是返回每个字符串的错误, 包括阿拉伯语。

@implementation UILabel (position)

- (BOOL) containsArabic
{
    NSError* error;
    NSRegularExpression* regex = [NSRegularExpression
        regularExpressionWithPattern:@"p{Arabic}" 
        options:0
        error:&error
    ];
    return error == nil 
        && [regex 
            numberOfMatchesInString:self.text 
            options:0 
            range:NSMakeRange(0, self.text.length)] > 0;
}

@end

Fail attempt 2: NSLocale

- (BOOL) containsArabic
{
    NSString *isoLangCode = (NSString*)CFStringTokenizerCopyBestStringLanguage(
       (CFStringRef)self.text, 
       CFRangeMake(
           0, 
           self.text.length
       )
    );
    NSLocaleLanguageDirection direction = [NSLocale lineDirectionForLanguage:isoLangCode];
    NSLog(@"++++++");
    NSLog(@"%@", self.text);
    NSLog(@"%d", direction);
    return direction == NSLocaleLanguageDirectionRightToLeft;
}

输出无效 :

2012-05-28 10:59:11.454 [2110:707] MANCHESTER CITY VS MANCHESTER UNITED LIVE HD
2012-05-28 10:59:11.461 [2110:707] 0
2012-05-28 10:59:11.474 [2110:707] ++++++
2012-05-28 10:59:11.477 [2110:707] tak
2012-05-28 10:59:11.484 [2110:707] 0
2012-05-28 10:59:11.511 [2110:707] ++++++
2012-05-28 10:59:11.513 [2110:707] ونهارك سعيد
2012-05-28 10:59:11.520 [2110:707] 3
2012-05-28 10:59:11.704 [2110:707] ++++++
2012-05-28 10:59:11.712 [2110:707] نهارك سعيد
2012-05-28 10:59:11.740 [2110:707] 3
2012-05-28 10:59:11.763 [2110:707] ++++++
2012-05-28 10:59:11.767 [2110:707] i think i am close
2012-05-28 10:59:11.772 [2110:707] 3
最佳回答

此答案假设您使用 ARC :

NSString *stringToTest = @"Some Foreign Language";

NSString *isoLangCode = (__bridge_transfer NSString*)CFStringTokenizerCopyBestStringLanguage((__bridge CFStringRef)stringToTest, CFRangeMake(0, stringToTest.length));

NSLocaleLanguageDirection direction = [NSLocale characterDirectionForLangauge:isoLangCode];

然后您可以使用 NS locale API s 线条 DirectForLanguage: 确定语言线条方向的方法。

问题回答

在以下函数中添加以下代码:

- (BOOL)textView:(UITextView *)aTextView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
      NSString *isoLangCode = (__bridge_transfer NSString*)CFStringTokenizerCopyBestStringLanguage((__bridge CFStringRef)text, CFRangeMake(0, text.length));

      if([isoLangCode isEqualToString:@"ar"])
      {
         //Arabic
      }
      else
      {
         //Other language
      }
}
- (BOOL)Direction:(NSString *)text
{
    NSString *isoLangCode = (__bridge_transfer NSString*)CFStringTokenizerCopyBestStringLanguage((__bridge CFStringRef)text, CFRangeMake(0, text.length));

    if([isoLangCode isEqualToString:@"ar"])
    {
        return YES;
    }
    return NO;
}




相关问题
Uncommon regular expressions [closed]

Recently I discovered two amazing regular expression features: ?: and ?!. I was curious of other neat regex features. So maybe you would like to share some tricky regular expressions.

regex to trap img tag, both versions

I need to remove image tags from text, so both versions of the tag: <img src="" ... ></img> <img src="" ... />

C++, Boost regex, replace value function of matched value?

Specifically, I have an array of strings called val, and want to replace all instances of "%{n}%" in the input with val[n]. More generally, I want the replace value to be a function of the match ...

PowerShell -match operator and multiple groups

I have the following log entry that I am processing in PowerShell I m trying to extract all the activity names and durations using the -match operator but I am only getting one match group back. I m ...

Is it possible to negate a regular expression search?

I m building a lexical analysis engine in c#. For the most part it is done and works quite well. One of the features of my lexer is that it allows any user to input their own regular expressions. This ...

regex for four-digit numbers (or "default")

I need a regex for four-digit numbers separated by comma ("default" can also be a value). Examples: 6755 3452,8767,9865,8766,3454 7678,9876 1234,9867,6876,9865 default Note: "default" ...

热门标签