Don't explode when missing first name

// FREEBIE
pull/1/head
Michael Kirk 9 years ago
parent 753c445bfc
commit 8211d4584f

@ -33,6 +33,6 @@
- (BOOL)nameExistsForPhoneIdentifier:(NSString * _Nullable)identifier; - (BOOL)nameExistsForPhoneIdentifier:(NSString * _Nullable)identifier;
- (UIImage * _Nullable)imageForPhoneIdentifier:(NSString * _Nullable)identifier; - (UIImage * _Nullable)imageForPhoneIdentifier:(NSString * _Nullable)identifier;
+ (NSComparator)contactComparator; + (NSComparator _Nonnull)contactComparator;
@end @end

@ -40,39 +40,55 @@ NS_ASSUME_NONNULL_BEGIN
} }
- (NSAttributedString *)attributedStringForContact:(Contact *)contact { - (NSAttributedString *)attributedStringForContact:(Contact *)contact {
NSMutableAttributedString *fullNameAttributedString =
[[NSMutableAttributedString alloc] initWithString:contact.fullName];
UIFont *firstNameFont; NSDictionary<NSString *, id> *boldFontAttributes = @{
UIFont *lastNameFont; NSFontAttributeName : [UIFont ows_mediumFontWithSize:self.nameLabel.font.pointSize],
NSForegroundColorAttributeName : [UIColor blackColor]
};
if (ABPersonGetSortOrdering() == kABPersonCompositeNameFormatFirstNameFirst) { NSDictionary<NSString *, id> *normalFontAttributes = @{
firstNameFont = [UIFont ows_mediumFontWithSize:self.nameLabel.font.pointSize]; NSFontAttributeName : [UIFont ows_regularFontWithSize:self.nameLabel.font.pointSize],
lastNameFont = [UIFont ows_regularFontWithSize:self.nameLabel.font.pointSize]; NSForegroundColorAttributeName : [UIColor ows_darkGrayColor]
};
NSAttributedString *_Nullable firstName, *_Nullable lastName;
if (ABPersonGetSortOrdering() == kABPersonSortByFirstName) {
if (contact.firstName) {
firstName = [[NSAttributedString alloc] initWithString:contact.firstName attributes:boldFontAttributes];
}
if (contact.lastName) {
lastName = [[NSAttributedString alloc] initWithString:contact.lastName attributes:normalFontAttributes];
}
} else { } else {
firstNameFont = [UIFont ows_regularFontWithSize:self.nameLabel.font.pointSize]; if (contact.firstName) {
lastNameFont = [UIFont ows_mediumFontWithSize:self.nameLabel.font.pointSize]; firstName = [[NSAttributedString alloc] initWithString:contact.firstName attributes:normalFontAttributes];
}
if (contact.lastName) {
lastName = [[NSAttributedString alloc] initWithString:contact.lastName attributes:boldFontAttributes];
}
} }
[fullNameAttributedString addAttribute:NSFontAttributeName
value:firstNameFont NSAttributedString *_Nullable leftName, *_Nullable rightName;
range:NSMakeRange(0, contact.firstName.length)]; if (ABPersonGetCompositeNameFormat() == kABPersonCompositeNameFormatFirstNameFirst) {
[fullNameAttributedString addAttribute:NSFontAttributeName leftName = firstName;
value:lastNameFont rightName = lastName;
range:NSMakeRange(contact.firstName.length + 1, contact.lastName.length)];
[fullNameAttributedString addAttribute:NSForegroundColorAttributeName
value:[UIColor blackColor]
range:NSMakeRange(0, contact.fullName.length)];
if (ABPersonGetSortOrdering() == kABPersonCompositeNameFormatFirstNameFirst) {
[fullNameAttributedString addAttribute:NSForegroundColorAttributeName
value:[UIColor ows_darkGrayColor]
range:NSMakeRange(contact.firstName.length + 1, contact.lastName.length)];
} else { } else {
[fullNameAttributedString addAttribute:NSForegroundColorAttributeName leftName = lastName;
value:[UIColor ows_darkGrayColor] rightName = firstName;
range:NSMakeRange(0, contact.firstName.length)]; }
NSMutableAttributedString *fullNameString = [NSMutableAttributedString new];
if (leftName) {
[fullNameString appendAttributedString:leftName];
} }
return fullNameAttributedString; if (leftName && rightName) {
[fullNameString appendAttributedString:[[NSAttributedString alloc] initWithString:@" "]];
}
if (rightName) {
[fullNameString appendAttributedString:rightName];
}
return fullNameString;
} }
@end @end

Loading…
Cancel
Save