根据iOS语言显示数字

时间:2019-03-03 09:19:26

标签: ios uilabel

我需要使从后端返回的数字本地化在App中的所有UILabel中,所以我需要在UILabel在App中设置英文数字时 阿拉伯语模式下,英文数字以阿拉伯语显示,但是当访问label.text时,它返回带有英文数字的原始文本,还需要在不使用UILabel类别的情况下使它不成为UILabel的子类的情况下,

您可以检查我的代码以尝试解决此问题,我在UILabel类别中编写了以下代码,我试图提供一种解决方法,以在MainThread中绘制文本并将文本设置为background中的text属性,因此该值将为English,在屏幕上绘制的将是阿拉伯语,但在layoutSubviews方法中,它将在几秒钟后自动将UILabel的英文值映射

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
// listening to the new value that have "text" keyPath 
    [self convertingEnglishNumbersToArabic:[change valueForKey:@"new"]];
}

- (void)convertingEnglishNumbersToArabic:(NSString *)oldText {

    NSString *newText = oldText;
    NSNumberFormatter *formatter = [NSNumberFormatter new];
    formatter.locale = [NSLocale localeWithLocaleIdentifier:@"ar"];
    for (NSInteger i = 0; i < 10; i++) {
        NSNumber *num = @(i);
        newText = [newText stringByReplacingOccurrencesOfString:num.stringValue withString:[formatter stringFromNumber:num]];
    }
    [self setText:[NSString stringWithFormat:@“Arabic  %@",newText]];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.15 * NSEC_PER_SEC)), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            @try{    
           [self removeObserver:self forKeyPath:@"text"];
        }@catch(id anException){
            self.text = @"engish";
        }
        [self addObserver:self forKeyPath:@"text" options:NSKeyValueObservingOptionNew context:nil];

    });,

0 个答案:

没有答案