summaryrefslogtreecommitdiff
path: root/platform/darwin
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
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')
-rw-r--r--platform/darwin/src/NSString+MGLAdditions.h9
-rw-r--r--platform/darwin/src/NSString+MGLAdditions.m22
-rw-r--r--platform/darwin/test/MGLNSStringAdditionsTests.m42
3 files changed, 73 insertions, 0 deletions
diff --git a/platform/darwin/src/NSString+MGLAdditions.h b/platform/darwin/src/NSString+MGLAdditions.h
index 45fea25588..246dc084f4 100644
--- a/platform/darwin/src/NSString+MGLAdditions.h
+++ b/platform/darwin/src/NSString+MGLAdditions.h
@@ -10,6 +10,15 @@ NS_ASSUME_NONNULL_BEGIN
/** Returns the receiver if non-empty or nil if empty. */
- (nullable NSString *)mgl_stringOrNilIfEmpty;
+/**
+ Returns a title-cased representation of the receiver using the specified
+ locale.
+
+ @param The locale. For strings presented to users, pass in the current locale
+ (`+[NSLocale currentLocale]`). To use the system locale, pass in `nil`.
+ */
+- (NSString *)mgl_titleCasedStringWithLocale:(NSLocale *)locale;
+
@end
@interface NSAttributedString (MGLAdditions)
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)
diff --git a/platform/darwin/test/MGLNSStringAdditionsTests.m b/platform/darwin/test/MGLNSStringAdditionsTests.m
new file mode 100644
index 0000000000..0c8a9f8143
--- /dev/null
+++ b/platform/darwin/test/MGLNSStringAdditionsTests.m
@@ -0,0 +1,42 @@
+#import <XCTest/XCTest.h>
+
+#import "NSString+MGLAdditions.h"
+
+@interface MGLNSStringAdditionsTests : XCTestCase
+
+@end
+
+@implementation MGLNSStringAdditionsTests
+
+- (void)testTitleCasedString {
+ NSLocale *locale = [NSLocale currentLocale];
+
+ XCTAssertEqualObjects([@"© OpenStreetMap" mgl_titleCasedStringWithLocale:locale], @"© OpenStreetMap");
+ XCTAssertEqualObjects([@"© OSM" mgl_titleCasedStringWithLocale:locale], @"© OSM");
+
+ XCTAssertEqualObjects([@"Improve this map" mgl_titleCasedStringWithLocale:locale], @"Improve This Map");
+ XCTAssertEqualObjects([@"Improve This Map" mgl_titleCasedStringWithLocale:locale], @"Improve This Map");
+
+ XCTAssertEqualObjects([@"Improve the map" mgl_titleCasedStringWithLocale:locale], @"Improve the Map");
+ XCTAssertEqualObjects([@"Improve The Map" mgl_titleCasedStringWithLocale:locale], @"Improve The Map");
+
+ XCTAssertEqualObjects([@"Improve a map" mgl_titleCasedStringWithLocale:locale], @"Improve a Map");
+ XCTAssertEqualObjects([@"Improve A Map" mgl_titleCasedStringWithLocale:locale], @"Improve A Map");
+
+ XCTAssertEqualObjects([@"Improve for the map" mgl_titleCasedStringWithLocale:locale], @"Improve for the Map");
+ XCTAssertEqualObjects([@"Improve For The Map" mgl_titleCasedStringWithLocale:locale], @"Improve For The Map");
+
+ XCTAssertEqualObjects([@"Improve and map" mgl_titleCasedStringWithLocale:locale], @"Improve and Map");
+ XCTAssertEqualObjects([@"Improve And Map" mgl_titleCasedStringWithLocale:locale], @"Improve And Map");
+
+ XCTAssertEqualObjects([@"Improve while mapping" mgl_titleCasedStringWithLocale:locale], @"Improve While Mapping");
+ XCTAssertEqualObjects([@"Improve While Mapping" mgl_titleCasedStringWithLocale:locale], @"Improve While Mapping");
+
+ XCTAssertEqualObjects([@"Improve with the map" mgl_titleCasedStringWithLocale:locale], @"Improve With the Map");
+ XCTAssertEqualObjects([@"Improve With The Map" mgl_titleCasedStringWithLocale:locale], @"Improve With The Map");
+
+ XCTAssertEqualObjects([@"Improve this iPhone" mgl_titleCasedStringWithLocale:locale], @"Improve This iPhone");
+ XCTAssertEqualObjects([@"Improve This iPhone" mgl_titleCasedStringWithLocale:locale], @"Improve This iPhone");
+}
+
+@end