summaryrefslogtreecommitdiff
path: root/platform/darwin/src/NSString+MGLAdditions.m
diff options
context:
space:
mode:
authorMinh Nguyễn <mxn@1ec5.org>2017-01-09 15:26:03 -0800
committerMinh Nguyễn <mxn@1ec5.org>2017-01-10 07:56:06 -0800
commitdea1a3033256b9932fe4b0314f0e5533857b8b53 (patch)
treedebaf4bb035fcf87d8ead3e9dbd7e105786c684f /platform/darwin/src/NSString+MGLAdditions.m
parent9d55c0f479aad655566f3a9e7841d155463d51a1 (diff)
downloadqtlocation-mapboxgl-dea1a3033256b9932fe4b0314f0e5533857b8b53.tar.gz
[ios, macos] Title case attribution buttons
Also added a category method on NSString for title casing the string, plus tests.
Diffstat (limited to 'platform/darwin/src/NSString+MGLAdditions.m')
-rw-r--r--platform/darwin/src/NSString+MGLAdditions.m22
1 files changed, 22 insertions, 0 deletions
diff --git a/platform/darwin/src/NSString+MGLAdditions.m b/platform/darwin/src/NSString+MGLAdditions.m
index 04a65dc5e2..5c32f4b789 100644
--- a/platform/darwin/src/NSString+MGLAdditions.m
+++ b/platform/darwin/src/NSString+MGLAdditions.m
@@ -10,6 +10,28 @@
return self.length ? self : nil;
}
+- (NSString *)mgl_titleCasedStringWithLocale:(NSLocale *)locale {
+ NSMutableString *string = self.mutableCopy;
+ [string enumerateLinguisticTagsInRange:string.mgl_wholeRange scheme:NSLinguisticTagSchemeLexicalClass options:0 orthography:nil usingBlock:^(NSString * _Nonnull tag, NSRange tokenRange, NSRange sentenceRange, BOOL * _Nonnull stop) {
+ NSString *word = [string substringWithRange:tokenRange];
+ if (word.length > 3
+ || !([tag isEqualToString:NSLinguisticTagConjunction]
+ || [tag isEqualToString:NSLinguisticTagPreposition]
+ || [tag isEqualToString:NSLinguisticTagDeterminer]
+ || [tag isEqualToString:NSLinguisticTagParticle]
+ || [tag isEqualToString:NSLinguisticTagClassifier])) {
+ unichar firstLetter = [[word capitalizedStringWithLocale:locale] characterAtIndex:0];
+ NSString *suffix = [word substringFromIndex:1];
+ if (!([word hasPrefix:@"i"] && suffix.length
+ && [[NSCharacterSet uppercaseLetterCharacterSet] characterIsMember:[suffix characterAtIndex:0]])) {
+ word = [NSString stringWithFormat:@"%C%@", firstLetter, suffix];
+ }
+ }
+ [string replaceCharactersInRange:tokenRange withString:word];
+ }];
+ return string;
+}
+
@end
@implementation NSAttributedString (MGLAdditions)