summary refs log tree commit diff stats
path: root/libs/cocos2d/CCLabelTTF.m
diff options
context:
space:
mode:
Diffstat (limited to 'libs/cocos2d/CCLabelTTF.m')
-rwxr-xr-xlibs/cocos2d/CCLabelTTF.m138
1 files changed, 138 insertions, 0 deletions
diff --git a/libs/cocos2d/CCLabelTTF.m b/libs/cocos2d/CCLabelTTF.m new file mode 100755 index 0000000..24602ec --- /dev/null +++ b/libs/cocos2d/CCLabelTTF.m
@@ -0,0 +1,138 @@
1/*
2 * cocos2d for iPhone: http://www.cocos2d-iphone.org
3 *
4 * Copyright (c) 2008-2010 Ricardo Quesada
5 * Copyright (c) 2011 Zynga Inc.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
24 *
25 */
26
27
28#import <Availability.h>
29
30#import "CCLabelTTF.h"
31#import "Support/CGPointExtension.h"
32#import "ccMacros.h"
33
34#ifdef __IPHONE_OS_VERSION_MAX_ALLOWED
35#import "Platforms/iOS/CCDirectorIOS.h"
36#endif
37
38@implementation CCLabelTTF
39
40- (id) init
41{
42 NSAssert(NO, @"CCLabelTTF: Init not supported. Use initWithString");
43 [self release];
44 return nil;
45}
46
47+ (id) labelWithString:(NSString*)string dimensions:(CGSize)dimensions alignment:(CCTextAlignment)alignment lineBreakMode:(CCLineBreakMode)lineBreakMode fontName:(NSString*)name fontSize:(CGFloat)size;
48{
49 return [[[self alloc] initWithString: string dimensions:dimensions alignment:alignment lineBreakMode:lineBreakMode fontName:name fontSize:size]autorelease];
50}
51
52+ (id) labelWithString:(NSString*)string dimensions:(CGSize)dimensions alignment:(CCTextAlignment)alignment fontName:(NSString*)name fontSize:(CGFloat)size
53{
54 return [[[self alloc] initWithString: string dimensions:dimensions alignment:alignment fontName:name fontSize:size]autorelease];
55}
56
57+ (id) labelWithString:(NSString*)string fontName:(NSString*)name fontSize:(CGFloat)size
58{
59 return [[[self alloc] initWithString: string fontName:name fontSize:size]autorelease];
60}
61
62
63- (id) initWithString:(NSString*)str dimensions:(CGSize)dimensions alignment:(CCTextAlignment)alignment lineBreakMode:(CCLineBreakMode)lineBreakMode fontName:(NSString*)name fontSize:(CGFloat)size
64{
65 if( (self=[super init]) ) {
66
67 dimensions_ = CGSizeMake( dimensions.width * CC_CONTENT_SCALE_FACTOR(), dimensions.height * CC_CONTENT_SCALE_FACTOR() );
68 alignment_ = alignment;
69 fontName_ = [name retain];
70 fontSize_ = size * CC_CONTENT_SCALE_FACTOR();
71 lineBreakMode_ = lineBreakMode;
72
73 [self setString:str];
74 }
75 return self;
76}
77
78- (id) initWithString:(NSString*)str dimensions:(CGSize)dimensions alignment:(CCTextAlignment)alignment fontName:(NSString*)name fontSize:(CGFloat)size
79{
80 return [self initWithString:str dimensions:dimensions alignment:alignment lineBreakMode:CCLineBreakModeWordWrap fontName:name fontSize:size];
81}
82
83- (id) initWithString:(NSString*)str fontName:(NSString*)name fontSize:(CGFloat)size
84{
85 if( (self=[super init]) ) {
86
87 dimensions_ = CGSizeZero;
88 fontName_ = [name retain];
89 fontSize_ = size * CC_CONTENT_SCALE_FACTOR();
90
91 [self setString:str];
92 }
93 return self;
94}
95
96- (void) setString:(NSString*)str
97{
98 [string_ release];
99 string_ = [str copy];
100
101 CCTexture2D *tex;
102 if( CGSizeEqualToSize( dimensions_, CGSizeZero ) )
103 tex = [[CCTexture2D alloc] initWithString:str
104 fontName:fontName_
105 fontSize:fontSize_];
106 else
107 tex = [[CCTexture2D alloc] initWithString:str
108 dimensions:dimensions_
109 alignment:alignment_
110 lineBreakMode:lineBreakMode_
111 fontName:fontName_
112 fontSize:fontSize_];
113
114 [self setTexture:tex];
115 [tex release];
116
117 CGRect rect = CGRectZero;
118 rect.size = [texture_ contentSize];
119 [self setTextureRect: rect];
120}
121
122-(NSString*) string
123{
124 return string_;
125}
126
127- (void) dealloc
128{
129 [string_ release];
130 [fontName_ release];
131 [super dealloc];
132}
133
134- (NSString*) description
135{
136 return [NSString stringWithFormat:@"<%@ = %08X | Label = %@, FontName = %@, FontSize = %.1f>", [self class], self, string_, fontName_, fontSize_];
137}
138@end