2010-09-06 53 views
0

將數字轉換爲字符串不成問題,但創建的字符串使用ascii數字。我將這個數字插入日語句子中。例如,字符本身是正確的,例如3,但在輸入日文時,寬度和位置應該有點不同。結果,這個數字看起來受到以下角色的限制。是否有任何巧妙的非手動方式將1轉換爲1,2轉換爲2,3轉換爲3等?Cocoa/Objective C:使用日語Unicode字符將數字轉換爲字符串

回答

0

由於缺乏更好的想法,我寫了一個不那麼優雅的類別解決方案,手動轉換每個字符。如果有人有更好的解決方案,請讓我知道。

#import "NSNumber+LocalizedNumber.h" 

@interface NSNumber() 
-(NSString*)convertRepresentation:(unichar)character; 
@end 

@implementation NSNumber (LocalizedNumber) 

-(NSString *)localizedNumber { 
    NSString *languageCode = [[NSLocale preferredLanguages] objectAtIndex:0]; 
    if([languageCode isEqualToString:@"ja"]) { 
     NSString *numberString = [self stringValue]; 
     NSUInteger length = [numberString length]; 
     NSMutableString *convertedString = [[NSMutableString alloc] initWithCapacity:length]; 
     for(NSUInteger index = 0; index < length; index++) { 
      unichar character = [numberString characterAtIndex:index]; 
      [convertedString appendString:[self convertRepresentation:character]]; 
     } 
     return [convertedString autorelease]; 
    } 
    return [self stringValue]; 
} 

-(NSString*)convertRepresentation:(unichar)character { 
    switch (character) { 
     case '1': 
      return @"1"; 
     case '2': 
      return @"2"; 
     case '3': 
      return @"3"; 
     case '4': 
      return @"4"; 
     case '5': 
      return @"5"; 
     case '6': 
      return @"6"; 
     case '7': 
      return @"7"; 
     case '8': 
      return @"8"; 
     case '9': 
      return @"9"; 
     case '0': 
      return @"0"; 
     default: 
      return nil; 
    } 
} 
+0

り我是日本人,而且我討厭**全寬數字!請僅使用日文句子中的半角數字。所有你需要的是用空格填充它們。ここに10個林檎があります看起來不錯,但ここに10個林檎があります看起來完全沒問題!ここに10個林檎があります看起來很可怕。 – Yuji 2010-09-06 16:32:12

+0

感謝您的輸入。由此產生的文本實際上只是一個帶有計數器的數字,例如1個,2個等。由於將會有英文和日文版本,所以我總是可以將額外的空格放在日文本地化的字符串文件中。我認爲內心深處我知道這可能更好,但我認爲嘗試不會有什麼傷害。 – Chase 2010-09-07 01:12:57

相關問題