forked from TTTAttributedLabel/TTTAttributedLabel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDetailViewController.m
161 lines (127 loc) · 7.51 KB
/
DetailViewController.m
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// EspressoViewController.m
//
// Copyright (c) 2011 Mattt Thompson (http://mattt.me)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#import "DetailViewController.h"
#import "TTTAttributedLabel.h"
static CGFloat const kEspressoDescriptionTextFontSize = 17.0f;
static inline NSRegularExpression * NameRegularExpression() {
static NSRegularExpression *_nameRegularExpression = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_nameRegularExpression = [[NSRegularExpression alloc] initWithPattern:@"^\\w+" options:NSRegularExpressionCaseInsensitive error:nil];
});
return _nameRegularExpression;
}
static inline NSRegularExpression * ParenthesisRegularExpression() {
static NSRegularExpression *_parenthesisRegularExpression = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_parenthesisRegularExpression = [[NSRegularExpression alloc] initWithPattern:@"\\([^\\(\\)]+\\)" options:NSRegularExpressionCaseInsensitive error:nil];
});
return _parenthesisRegularExpression;
}
@interface DetailViewController () <TTTAttributedLabelDelegate, UIActionSheetDelegate>
@property (nonatomic, copy) NSString *espressoDescription;
@property (nonatomic) TTTAttributedLabel *attributedLabel;
@end
@implementation DetailViewController
@synthesize espressoDescription = _espresso;
@synthesize attributedLabel = _attributedLabel;
- (id)initWithEspressoDescription:(NSString *)espresso {
self = [super initWithNibName:nil bundle:nil];
if (!self) {
return nil;
}
self.espressoDescription = espresso;
return self;
}
#pragma mark - UIViewController
- (void)loadView {
[super loadView];
self.view.backgroundColor = [UIColor whiteColor];
self.attributedLabel = [[TTTAttributedLabel alloc] initWithFrame:CGRectInset(self.view.bounds, 10.0f, 70.0f)];
self.attributedLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:self.attributedLabel];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.title = NSLocalizedString(@"Espresso", nil);
self.attributedLabel.delegate = self;
UIFont *f = [UIFont systemFontOfSize:kEspressoDescriptionTextFontSize];
self.attributedLabel.font = f;
self.attributedLabel.textColor = [UIColor darkGrayColor];
self.attributedLabel.lineBreakMode = NSLineBreakByWordWrapping;
self.attributedLabel.lineSpacing = -100;
self.attributedLabel.maximumLineHeight = f.lineHeight;
self.attributedLabel.minimumLineHeight = f.lineHeight;
self.attributedLabel.numberOfLines = 0;
self.attributedLabel.linkAttributes = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:(NSString *)kCTUnderlineStyleAttributeName];
self.attributedLabel.highlightedTextColor = [UIColor whiteColor];
self.attributedLabel.shadowColor = [UIColor colorWithWhite:0.87f alpha:1.0f];
self.attributedLabel.shadowOffset = CGSizeMake(0.0f, 1.0f);
self.attributedLabel.verticalAlignment = TTTAttributedLabelVerticalAlignmentTop;
[self.attributedLabel setText:self.espressoDescription afterInheritingLabelAttributesAndConfiguringWithBlock:^NSMutableAttributedString *(NSMutableAttributedString *mutableAttributedString) {
NSRange stringRange = NSMakeRange(0, [mutableAttributedString length]);
NSRegularExpression *regexp = NameRegularExpression();
NSRange nameRange = [regexp rangeOfFirstMatchInString:[mutableAttributedString string] options:0 range:stringRange];
UIFont *boldSystemFont = [UIFont boldSystemFontOfSize:kEspressoDescriptionTextFontSize];
CTFontRef boldFont = CTFontCreateWithName((__bridge CFStringRef)boldSystemFont.fontName, boldSystemFont.pointSize, NULL);
if (boldFont) {
[mutableAttributedString removeAttribute:(NSString *)kCTFontAttributeName range:nameRange];
[mutableAttributedString addAttribute:(NSString *)kCTFontAttributeName value:(__bridge id)boldFont range:nameRange];
CFRelease(boldFont);
}
[mutableAttributedString replaceCharactersInRange:nameRange withString:[[[mutableAttributedString string] substringWithRange:nameRange] uppercaseString]];
regexp = ParenthesisRegularExpression();
[regexp enumerateMatchesInString:[mutableAttributedString string] options:0 range:stringRange usingBlock:^(NSTextCheckingResult *result, __unused NSMatchingFlags flags, __unused BOOL * stop) {
UIFont *italicSystemFont = [UIFont italicSystemFontOfSize:kEspressoDescriptionTextFontSize];
CTFontRef italicFont = CTFontCreateWithName((__bridge CFStringRef)italicSystemFont.fontName, italicSystemFont.pointSize, NULL);
if (italicFont) {
[mutableAttributedString removeAttribute:(NSString *)kCTFontAttributeName range:result.range];
[mutableAttributedString addAttribute:(NSString *)kCTFontAttributeName value:(__bridge id)italicFont range:result.range];
CFRelease(italicFont);
[mutableAttributedString removeAttribute:(NSString *)kCTForegroundColorAttributeName range:result.range];
[mutableAttributedString addAttribute:(NSString*)kCTForegroundColorAttributeName value:(id)[[UIColor grayColor] CGColor] range:result.range];
}
}];
return mutableAttributedString;
}];
NSRegularExpression *regexp = NameRegularExpression();
NSRange linkRange = [regexp rangeOfFirstMatchInString:self.espressoDescription options:0 range:NSMakeRange(0, [self.espressoDescription length])];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://en.wikipedia.org/wiki/%@", [self.espressoDescription substringWithRange:linkRange]]];
[self.attributedLabel addLinkToURL:url withRange:linkRange];
}
#pragma mark - TTTAttributedLabelDelegate
- (void)attributedLabel:(__unused TTTAttributedLabel *)label
didSelectLinkWithURL:(NSURL *)url
{
[[[UIActionSheet alloc] initWithTitle:[url absoluteString] delegate:self cancelButtonTitle:NSLocalizedString(@"Cancel", nil) destructiveButtonTitle:nil otherButtonTitles:NSLocalizedString(@"Open Link in Safari", nil), nil] showInView:self.view];
}
#pragma mark - UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet
clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == actionSheet.cancelButtonIndex) {
return;
}
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:actionSheet.title]];
}
@end