Q:

Transform punctuations form half width to full width

I have a sentence below:

我今天去买菜,买了一个西瓜,花了1.2元,买了一个土豆,花了3.78元。还买了一个无花果,花了45.89，怎么办呢?好贵呀!贵的我不知道再买什么了。

The punctuations in it are half width. How to change them to fullwidth, like the following:

我今天去买菜，买了一个西瓜，花了1.2元，买了一个土豆，花了3.78元。还买了一个无花果，花了45.89，怎么办呢？好贵呀！贵的我不知道再买什么了。

Some punctuations to consider (not exhaustive):

, to ，
? to ？
! to ！
"" to “”
; to ；

A:

First, define the CharacterSet from which you want to transform your characters. So if you want only punctuation, the set could be CharacterSet.punctuationCharacters or CharacterSet.alphanumerics.inverted.
Then map each character from this set to its HalfwidthFullwidth transformation.
Swift 3 and 4
extension String {
    func transformingHalfwidthFullwidth(from aSet: CharacterSet) -> String {
        return String(characters.map {
            if String($0).rangeOfCharacter(from: aSet) != nil {
                let string = NSMutableString(string: String($0))
                CFStringTransform(string, nil, kCFStringTransformFullwidthHalfwidth, true)
                return String(string).characters.first!
            } else {
                return $0
            }
        })
    }
}

Usage
let string = ",?!\"\";abc012図書館 助け 足場が痛い 多くの涙"
let result = string.transformingHalfwidthFullwidth(from: CharacterSet.alphanumerics.inverted)

// it prints: ，？！＂＂；abc012図書館　助け　足場が痛い　多くの涙
print(result)

Objective-C
@implementation NSString (HalfwidthFullwidth)
- (NSString *)transformingHalfwidthFullwidth:(nonnull NSCharacterSet *)aSet {
    NSUInteger len = self.length;
    unichar buffer[len + 1];
    [self getCharacters:buffer range:NSMakeRange(0, len)];
    for (int i = 0; i < len; i++) {
        unichar c = buffer[i];
        NSMutableString *s = [[NSMutableString alloc] initWithCharacters:&c length:1];
        NSRange r = [s rangeOfCharacterFromSet:aSet];
        if (r.location != NSNotFound) {
            CFStringTransform((CFMutableStringRef)s, nil, kCFStringTransformFullwidthHalfwidth, true);
            [s getCharacters:buffer + i range:NSMakeRange(0, 1)];
        }
    }
    return [NSString stringWithCharacters:buffer length:len];
}
@end

Usage
NSString *string = @",?!\"\";abc012図書館 助け 足場が痛い 多くの涙";
NSString *result = [string transformingHalfwidthFullwidth:NSCharacterSet.alphanumericCharacterSet.invertedSet];

// it prints: ，？！＂＂；abc012図書館　助け　足場が痛い　多くの涙
NSLog(result);

