ContactNameLabel.m 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2012-2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. #import "ContactNameLabel.h"
  21. #import "Contact.h"
  22. #import "UserSettings.h"
  23. #import "BundleUtil.h"
  24. @implementation ContactNameLabel
  25. - (void)setContact:(Contact*)contact {
  26. _contact = contact;
  27. [self updateColor];
  28. if (contact == nil) {
  29. self.text = [BundleUtil localizedStringForKey:@"me"];
  30. return;
  31. }
  32. BOOL nameAvailable = NO;
  33. NSMutableAttributedString *nameLabelStr = [[NSMutableAttributedString alloc] init];
  34. UIFontDescriptor *fontDescriptor = [UIFontDescriptor preferredFontDescriptorWithTextStyle:UIFontTextStyleHeadline];
  35. CGFloat size = fontDescriptor.pointSize;
  36. UIFont *normalFont = [UIFont systemFontOfSize:size];
  37. NSMutableDictionary *boldDict = [NSMutableDictionary dictionaryWithObject:[UIFont preferredFontForTextStyle:UIFontTextStyleHeadline] forKey:NSFontAttributeName];
  38. NSMutableDictionary *regularDict = [NSMutableDictionary dictionaryWithObject:normalFont forKey:NSFontAttributeName];
  39. if (contact.state.intValue == kStateInvalid) {
  40. regularDict[NSStrikethroughStyleAttributeName] = [NSNumber numberWithInt:NSUnderlineStyleThick];
  41. boldDict[NSStrikethroughStyleAttributeName] = [NSNumber numberWithInt:NSUnderlineStyleThick];
  42. }
  43. if ([self isBlacklisted]) {
  44. [nameLabelStr appendAttributedString:[[NSAttributedString alloc] initWithString:@"🚫 " attributes:regularDict]];
  45. }
  46. NSDictionary *firstNameDict = [UserSettings sharedUserSettings].sortOrderFirstName ? boldDict : regularDict;
  47. NSDictionary *lastNameDict = ![UserSettings sharedUserSettings].sortOrderFirstName ? boldDict : regularDict;
  48. if ([UserSettings sharedUserSettings].displayOrderFirstName) {
  49. if (contact.firstName != nil && contact.firstName.length > 0) {
  50. NSAttributedString *firstNameStr = [[NSAttributedString alloc] initWithString:contact.firstName attributes:firstNameDict];
  51. [nameLabelStr appendAttributedString:firstNameStr];
  52. nameAvailable = YES;
  53. }
  54. if (contact.lastName != nil && contact.lastName.length > 0) {
  55. NSAttributedString *lastNameStr = [[NSAttributedString alloc] initWithString:contact.lastName attributes:lastNameDict];
  56. if (contact.firstName != nil) {
  57. /* space */
  58. [nameLabelStr appendAttributedString:[[NSAttributedString alloc] initWithString:@" " attributes:firstNameDict]];
  59. }
  60. [nameLabelStr appendAttributedString:lastNameStr];
  61. nameAvailable = YES;
  62. }
  63. } else {
  64. if (contact.lastName != nil && contact.lastName.length > 0) {
  65. NSAttributedString *lastNameStr = [[NSAttributedString alloc] initWithString:contact.lastName attributes:lastNameDict];
  66. [nameLabelStr appendAttributedString:lastNameStr];
  67. nameAvailable = YES;
  68. }
  69. if (contact.firstName != nil && contact.firstName.length > 0) {
  70. NSAttributedString *firstNameStr = [[NSAttributedString alloc] initWithString:contact.firstName attributes:firstNameDict];
  71. if (contact.lastName != nil) {
  72. /* space */
  73. [nameLabelStr appendAttributedString:[[NSAttributedString alloc] initWithString:@" " attributes:lastNameDict]];
  74. }
  75. [nameLabelStr appendAttributedString:firstNameStr];
  76. nameAvailable = YES;
  77. }
  78. }
  79. if (!nameAvailable) {
  80. /* no name - use nickname or identity */
  81. if (contact.publicNickname.length > 0 && [contact.publicNickname isEqualToString:contact.identity] == NO) {
  82. NSString *nickname = [NSString stringWithFormat:@"~%@", contact.publicNickname];
  83. NSAttributedString *nicknameStr = [[NSAttributedString alloc] initWithString:nickname attributes:boldDict];
  84. [nameLabelStr appendAttributedString:nicknameStr];
  85. } else {
  86. NSAttributedString *identityStr = [[NSAttributedString alloc] initWithString:contact.identity attributes:boldDict];
  87. [nameLabelStr appendAttributedString:identityStr];
  88. }
  89. }
  90. self.attributedText = nameLabelStr;
  91. }
  92. - (BOOL)isBlacklisted {
  93. return [[UserSettings sharedUserSettings].blacklist containsObject:_contact.identity];
  94. }
  95. - (void)updateColor {
  96. if (_contact.isActive) {
  97. self.textColor = [Colors fontNormal];
  98. } else {
  99. self.textColor = [Colors fontVeryLight];
  100. }
  101. }
  102. - (NSString *)accessibilityLabel {
  103. NSString *appendix = @"";
  104. if ([self isBlacklisted]) {
  105. appendix = [BundleUtil localizedStringForKey:@"blocked"];
  106. } else if (_contact.isActive == NO) {
  107. appendix = [BundleUtil localizedStringForKey:@"inactive"];
  108. }
  109. return [NSString stringWithFormat:@"%@. %@", self.text, appendix];
  110. }
  111. @end