forked from enormego/UIKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUILabel.m
159 lines (129 loc) · 3.87 KB
/
UILabel.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
//
// UILabel.m
// UIKit
//
// Created by Shaun Harrison on 6/24/09.
// Copyright 2009 enormego. All rights reserved.
//
#import "UILabel.h"
#import "UITextField.h"
#import <QuartzCore/QuartzCore.h>
@implementation UILabel
@synthesize text=_text, font=_font, textColor=_textColor, shadowColor=_shadowColor, shadowOffset=_shadowOffset;
@synthesize textAlignment=_textAlignment, lineBreakMode=_lineBreakMode;
@synthesize highlighted=_highlighted, highlightedTextColor=_highlightedTextColor;
- (id)initWithFrame:(NSRect)frame {
if (self = [super initWithFrame:frame]) {
self.layer = [CATextLayer layer];
self.textColor = [UIColor blackColor];
self.font = [NSFont systemFontOfSize:17.0f];
self.textAlignment = UITextAlignmentLeft;
self.lineBreakMode = UILineBreakModeTailTruncation;
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
if((self = [super initWithCoder:aDecoder])) {
self.layer = [CATextLayer layer];
self.textColor = [UIColor blackColor];
self.font = [NSFont systemFontOfSize:17.0f];
self.textAlignment = UITextAlignmentLeft;
self.lineBreakMode = UILineBreakModeTailTruncation;
}
return self;
}
- (void)setText:(NSString *)text {
[_text release];
_text = [text copy];
((CATextLayer*)self.layer).string = _text;
}
- (void)setFont:(NSFont *)font {
[_font release];
_font = [font retain];
((CATextLayer*)self.layer).font = _font;
((CATextLayer*)self.layer).fontSize = [_font pointSize];
}
- (void)setTextColor:(UIColor *)textColor {
[_textColor release];
_textColor = [textColor retain];
if(![self isHighlighted]) {
((CATextLayer*)self.layer).foregroundColor = _textColor.CGColor;
}
}
- (void)setShadowColor:(UIColor *)shadowColor {
[_shadowColor release];
_shadowColor = [shadowColor retain];
self.layer.shadowColor = _shadowColor.CGColor;
}
- (void)setShadowOffset:(CGSize)shadowOffset {
_shadowOffset = shadowOffset;
self.layer.shadowOffset = _shadowOffset;
}
- (void)setTextAlignment:(UITextAlignment)textAlignment {
_textAlignment = textAlignment;
NSString* alignmentMode = nil;
switch(_textAlignment) {
case UITextAlignmentCenter:
alignmentMode = kCAAlignmentCenter;
break;
case UITextAlignmentRight:
alignmentMode = kCAAlignmentRight;
break;
case UITextAlignmentJusitifed:
alignmentMode = kCAAlignmentJustified;
break;
case UITextAlignmentNatural:
alignmentMode = kCAAlignmentNatural;
break;
default:
alignmentMode = kCAAlignmentLeft;
}
((CATextLayer*)self.layer).alignmentMode = alignmentMode;
}
- (void)setLineBreakMode:(UILineBreakMode)lineBreakMode {
_lineBreakMode = lineBreakMode;
NSString* truncationMode = nil;
switch(_lineBreakMode) {
case UILineBreakModeWordWrap:
case UILineBreakModeCharacterWrap:
((CATextLayer*)self.layer).wrapped = YES;
truncationMode = kCATruncationNone;
break;
case UILineBreakModeClip:
((CATextLayer*)self.layer).wrapped = NO;
truncationMode = kCATruncationNone;
break;
case UILineBreakModeHeadTruncation:
truncationMode = kCATruncationStart;
break;
case UILineBreakModeMiddleTruncation:
truncationMode = kCATruncationMiddle;
break;
default:
truncationMode = kCATruncationEnd;
}
((CATextLayer*)self.layer).truncationMode = truncationMode;
}
- (void)setHighlightedTextColor:(UIColor *)highlightedTextColor {
[_highlightedTextColor release];
_highlightedTextColor = [highlightedTextColor retain];
if([self isHighlighted]) {
((CATextLayer*)self.layer).foregroundColor = _highlightedTextColor.CGColor;
}
}
- (void)setHighlighted:(BOOL)highlighted {
_highlighted = highlighted;
if(_highlighted && self.highlighted) {
((CATextLayer*)self.layer).foregroundColor = self.highlightedTextColor.CGColor;
} else {
((CATextLayer*)self.layer).foregroundColor = self.textColor.CGColor;
}
}
- (void)dealloc {
[_text release];
[_font release];
[_textColor release];
[_shadowColor release];
[super dealloc];
}
@end