Reputation: 1573
I have been scratching my head over this.
I want to combine two Korean characters into a single one.
ㅁ + ㅏ = 마
How would I go about doing this with NSString?
Edit:
zaph's solution works with two characters. But I am stumped on how to combine more than 2 .
ㅁ + ㅏ + ㄴ = 만
But
NSString *s = @"ㅁㅏㄴ";
NSString *t = [s precomposedStringWithCompatibilityMapping];
NSLog(@"%@", t);
prints out
마ㄴ
Edit 2:
I looked around a bit more and it seems a bit more involved. A character like '만' is made up of 3 parts. The initial jamo, medial jamo and a final jamo. These need to be combined to map to a code point in the Hangul Syllables, using the equation below.
((initial * 588) + (medial * 28) + final) + 44032
This blog post has a very good explanation.
Upvotes: 4
Views: 1016
Reputation: 112855
Use '- (NSString *)precomposedStringWithCompatibilityMapping'.
NSString *tc = @"ㅁㅏ";
NSLog(@"tc: '%@'", tc);
NSString *cc = [tc precomposedStringWithCompatibilityMapping];
NSLog(@"cc: '%@'", cc);
NSLog output:
tc: 'ㅁㅏ'
cc: '마'
See Apple's Technical Q&A QA1235: Converting to Precomposed Unicode
Upvotes: 4
Reputation: 237110
They're actually different Unicode characters. ㅁ (\u3141) is part of the "Hangul compatibility jamo" block, and those characters are meant to appear on their own (say, when you want to illustrate an individual jamo). The actual character you want is \u1106. For example, here is \u1106 followed by \u1161, individually copied and pasted from a Unicode table: 마. As you can see, those compose into the character you want.
Upvotes: 2
Reputation: 33369
It's simple:
NSString *first = @"ㅁ";
NSString *second = @"ㅏ";
NSString *combinedStr = [first stringByAppendingString:second];
NSLog(@"%@", combinedStr); // ㅁㅏ
Upvotes: -4