summaryrefslogtreecommitdiff
path: root/Juick/Helpers/NSAttributedString_Entities.m
blob: 3f7159b0d43d1bb68a82abc565527153883fbb12 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//
//  NSAttributedString+NSAttributedString.h
//  Juick
//
//  Created by Vitaly Takmazov on 23.09.2020.
//  Copyright © 2020 com.juick. All rights reserved.
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@implementation NSAttributedString (Entities)

NSMutableParagraphStyle *quoteStyle;
UIFont *boldFont;
UIFont *italicFont;

__attribute__((constructor))
static void initialize_fonts() {
    quoteStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
    quoteStyle.firstLineHeadIndent = 12.0f;
    quoteStyle.headIndent = 12.0f;
    quoteStyle.paragraphSpacing = 6.0f;
    UIFontDescriptor* fontDescriptor = [UIFontDescriptor
                                        preferredFontDescriptorWithTextStyle:UIFontTextStyleBody];
    UIFontDescriptor* boldFontDescriptor = [fontDescriptor
                                            fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitBold];
    boldFont =  [UIFont fontWithDescriptor:boldFontDescriptor size: 0.0];
    UIFontDescriptor* italicFontDescriptor = [fontDescriptor
                                              fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitItalic];
    italicFont =  [UIFont fontWithDescriptor:italicFontDescriptor size: 0.0];
}

+(NSAttributedString *) attributedStringFromMessage:(Message *)msg {
    if (msg.text) {
    NSMutableAttributedString *txt = [[NSMutableAttributedString alloc]
                                      initWithString:msg.text
                                      attributes:@{NSFontAttributeName:[UIFont preferredFontForTextStyle:UIFontTextStyleBody],
                                                   NSForegroundColorAttributeName:[UIColor colorNamed:@"Text"]
                                      }];
    [txt beginEditing];
    for (Entity *entity in msg.entities) {
        NSUInteger start = entity.start ? [entity.start unsignedIntegerValue] : 0;
        NSUInteger end =  entity.end ? [entity.end unsignedIntegerValue] : 0;
        NSString *text = entity.text ? entity.text : @"";
        NSRange currentRange = NSMakeRange(start, end - start);
        [txt addAttribute:@"displayText" value:text range:currentRange];
        if ([entity.type isEqualToString:@"a"]) {
            [txt addAttribute:NSLinkAttributeName value:entity.link range:currentRange];
        }
        if ([entity.type isEqualToString:@"q"]) {
            [txt addAttribute:NSForegroundColorAttributeName value:[UIColor colorNamed:@"Muted"] range:currentRange];
            [txt addAttribute:NSParagraphStyleAttributeName value:quoteStyle range:currentRange];
        }
        if ([entity.type isEqualToString:@"u"]) {
            [txt addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:currentRange];
        }
        
        if ([entity.type isEqualToString:@"b"]) {
            [txt addAttribute:NSFontAttributeName value:boldFont range:currentRange];
        }
        if ([entity.type isEqualToString:@"i"]) {
            [txt addAttribute:NSFontAttributeName value:italicFont range:currentRange];
        }
    }
    [txt enumerateAttribute:@"displayText" inRange:NSMakeRange(0, [txt length]) options:0 usingBlock:^(id  _Nullable value, NSRange range, BOOL * _Nonnull stop) {
        if (value) {
            [txt replaceCharactersInRange:range withString:value];
        }
    }];
    if ([msg.tags count] > 0) {
        NSString *tagsList = [NSString stringWithFormat:@"%@\n", [msg.tags componentsJoinedByString:@", "]];
        [txt insertAttributedString:[[NSAttributedString alloc]
                                     initWithString:tagsList
                                     attributes:@{
                                                  NSFontAttributeName:italicFont,
                                                  NSForegroundColorAttributeName:[UIColor colorNamed:@"Muted"]
                                                  }] atIndex:0];
    }
    [txt endEditing];
    return txt;
    } else {
        return [[NSAttributedString alloc] initWithString:@""];
    }
}

@end

NS_ASSUME_NONNULL_END