forked from ggu/MFJLabel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMFJLabel.h
More file actions
105 lines (89 loc) · 4.68 KB
/
MFJLabel.h
File metadata and controls
105 lines (89 loc) · 4.68 KB
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
//
// MFJLabel.h
//
// Copyright (c) 2013 Malcolm Jarvis (github.com/mjarvis). All rights reserved.
//
// 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 <UIKit/UIKit.h>
@protocol MFJLabelDelegate;
extern NSString * const MFJLabelLinkAttributeName;
extern NSString * const MFJLabelPhoneNumberAttributeName;
extern NSString * const MFJLabelAddressAttributeName;
extern NSString * const MFJLabelDateAttributeName;
typedef NS_OPTIONS(uint64_t, MFJDataDetectorType)
{
MFJDataDetectorTypeDate = NSTextCheckingTypeDate,
MFJDataDetectorTypeAddress = NSTextCheckingTypeAddress,
MFJDataDetectorTypeLink = NSTextCheckingTypeLink,
MFJDataDetectorTypePhoneNumber = NSTextCheckingTypePhoneNumber,
};
/**
* MFJLabel is a UILabel subclass that adds support for automatically converting links to clickable items.
* Use `- (CGSize)sizeThatFits:(CGSize)size` to find the bounding size for the text if needed.
* Note: numberOfLines is not currently supported, and is assumed 0.
*/
@interface MFJLabel : UILabel
@property (nonatomic, weak) id <MFJLabelDelegate> delegate;
/**
* The drawn textStorage. Set internally when setting the text or attributedText properties.
*/
@property (nonatomic, copy) NSTextStorage *textStorage;
/**
* The orginal attributed string set on the label. Apple manipulates the attributed string on the label and we end up loosing attributes when setting `textColor` and `font` on the label.
*/
@property (nonatomic, copy) NSAttributedString *originalAttributedString;
/**
* Available NSTextCheckingType values are defined in MFJDataDetectorType.
* Defaults to all available types.
*/
@property (nonatomic, assign) MFJDataDetectorType dataDetectorTypes;
/**
* Attributes appled to detected links. Available keys can be found in <UIKit/NSAttributedString.h>
* NSBackgroundColorAttributeName is used for a link's highlighted state.
* Defaults to blue and underlined, with a grey highlight.
*/
@property (nonatomic, strong) NSDictionary *linkAttributes;
/**
* Used internally to create the text storage used to render the text. Can be used externally to pre-cache the text with detected links, as NSDataDetector can be slow for longer text.
*
* @param attributedString An NSAttributedString containing the text to detect links in.
*
* @return NSTextStorage with added attributes for detected links.
*/
- (NSTextStorage *)textStorageWithDetectedLinksForAttributedString:(NSAttributedString *)attributedString;
@end
@protocol MFJLabelDelegate <NSObject>
/**
* Called when the user touches and releases quickly on a detected link. Usually used for a primary action.
*
* @param label The MFJLabel instance that received the touch
* @param linkText The displayed text for the link at the tap point
* @param attributes NSAttributedString attributes for the link at the tap point. See the constants at the top of this file for NSTextCheckingResult component accessor keys.
*/
- (void)label:(MFJLabel *)label didTapLinkText:(NSString *)linkText withAttributes:(NSDictionary *)attributes;
/**
* Called when the user touches holds on a detected link. Usually used for a secondary action such as displaying an action sheet or menu controller with multiple user choices.
*
* @param label The MFJLabel instance that received the touch
* @param linkText The displayed text for the link at the hold point
* @param attributes NSAttributedString attributes for the link at the hold point. See the constants at the top of this file for NSTextCheckingResult component accessor keys.
*/
- (void)label:(MFJLabel *)label didLongPressLinkText:(NSString *)linkText withAttributes:(NSDictionary *)attributes;
@end