how to use TTAreaStyle to create label style with icon

 

TTAreaStyle is a utility style class depend on three20.(please follow this link ttareastyleforthree20 ) It enhances the style of three20 for it could create an independent rect for other styles drawn on it. You can create some more complex style for TTView .Such as , a label with a icon in the front of it or a view with separated segments with different style. for example ,look at the imgs showed below.



usually , we may be adopt the method  creating a view with a UIImageView and a Label added to it . when there is a single exception , for saving time ,we feel free to use it . Honestly and personally , I have to say , it is a ugly method .I was thinking , is there any cheap way to implement it . The style of Three20 make it achievable. All you want to do just is appending a style to a view inherited from TTView. Here , I used TTLable.



- (TTLabel*)authorLabel {
if (!_authorLabel) {
_authorLabel = [[TTLabel alloc] initWithFrame:CGRectMake(0, 0, self.width - 2 * BookSummaryViewMargin, 0)];
_authorLabel.style = TTSTYLE(bookSummaryAuthorStyle);
_authorLabel.backgroundColor = [UIColor clearColor];
[self addSubview:_authorLabel];
}
return _authorLabel;
}



In the Global stylesheet  , adding this,



-(TTStyle*) bookSummaryAuthorStyle{
CGFloat timeIconWidth = 10;
CGFloat timeIconHeight = 12;
return
[TTAreaStyle styleWithBlock:^(CGRect rect ,id data){
return CGRectMake(0, (CGRectGetHeight(rect) - [[data objectAtIndex:0] floatValue]) / 2 , 10, 12);
}
data:[NSArray arrayWithObjects:[NSNumber numberWithFloat:timeIconWidth],
[NSNumber numberWithFloat:timeIconHeight],nil]
style:
[TTImageStyle styleWithImageURL:@"bundle://time.png" next:nil]
next:
[TTBoxStyle styleWithPadding:UIEdgeInsetsMake(0, 20, 0, 0) next:
[TTTextStyle styleWithFont: [UIFont systemFontOfSize:14]
color: RGBCOLOR(128,128,128)
minimumFontSize: 0
shadowColor: RGBCOLOR(171,171,171)
shadowOffset: TTSTYLEVAR(photoCaptionTextShadowOffset)
textAlignment: UITextAlignmentLeft
verticalAlignment: UIControlContentVerticalAlignmentCenter
lineBreakMode: UILineBreakModeCharacterWrap
numberOfLines: 6
next: nil]]];
}


With TTAreaStyle ,  I creat a independent area which you could draw anything you like . Here , i added 


[TTImageStyle styleWithImageURL:@"bundle://time.png" next:nil]


And then , shrinked  the origin draw area  by using 


[TTBoxStyle styleWithPadding:UIEdgeInsetsMake(0, 20, 0, 0) next:


It's worth nothing that  the param rect in the block wil let you know the size of TTLabel which other style don't offer. It 's a amazing characteristic that help you calculate the position of  elements in the independent area . Off course . do nesting if necessary.


^(CGRect rect ,id data){ }


Share
Posted in iphone at August 29th, 2011. No Comments.

how to implement a slider switch (with some three20)

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
#define SliderWidth 116
#define SliderHeight 28

-------------------ScreenCapture.h-------------------

#import
@interface ScreenCapture : NSObject {

}
+(ScreenCapture*)capture;
- (UIImage *)captureView:(UIView *)view;
@end


-------------------ScreenCapture.m-------------------
#import "ScreenCapture.h"
@implementation ScreenCapture

+(ScreenCapture*)capture{
    return [[[ScreenCapture alloc] init] autorelease];
}

- (UIImage *)captureView:(UIView *)view {  
    CGRect screenRect = view.frame;
    UIGraphicsBeginImageContext(screenRect.size);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    [[UIColor whiteColor] set];
    CGContextFillRect(ctx, screenRect);
    [view.layer renderInContext:ctx];  
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}


-------------------GlobalStyleSheet.h-------------------
#import
#import "extThree20CSSStyle/extThree20CSSStyle.h"

@interface GlobalStyleSheet : TTDefaultCSSStyleSheet {

}
@end


-------------------GlobalStyleSheet.m-------------------
#import "GlobalStyleSheet.h"
#import "Three20Style/UIColorAdditions.h"

 

-(UIImage*)slideSwitchLeftImage{
    TTView *segmentView = [[[TTView alloc] initWithFrame:CGRectMake(0, 0, SliderWidth , SliderHeight)] autorelease];
    UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(10, 0, 80, 20)] autorelease];
    label.text = LocalizedString(@"B", @"B");
    label.textColor = [UIColor redColor];
    [segmentView addSubview:label];
    segmentView.backgroundColor = [UIColor blackColor];
    return [[[ScreenCapture capture] captureView:segmentView] stretchableImageWithLeftCapWidth:1 topCapHeight:0];
}

-(UIImage*)slideSwitchRightImage{
    TTView *segmentView = [[[TTView alloc] initWithFrame:CGRectMake(0, 0, SliderWidth  ,SliderHeight)] autorelease];
    UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(30, 0, 80, 20)] autorelease];
    label.text = LocalizedString(@"A", @"A");
    label.textColor = [UIColor redColor];
    [segmentView addSubview:label];
    segmentView.backgroundColor = [UIColor blueColor];
    return [[[ScreenCapture capture] captureView:segmentView] stretchableImageWithLeftCapWidth:1 topCapHeight:0];
}

-(UIImage*)slideSwitchThumbImage{
    TTView *thumbView = [[[TTView alloc] initWithFrame:CGRectMake(0, 0, SliderHeight, SliderHeight)] autorelease];
    thumbView.backgroundColor = [UIColor redColor];
    return  [[ScreenCapture capture] captureView:thumbView] ;
}

@end

---------------------------------testController.m----------------------

-(void)viewDidLoad{
    [super viewDidLoad];
   
    CGRect frame = CGRectMake(10.0, 50.0, 116, 28);
    CGRect thumb = CGRectMake(0.0, 0.0, 28, 28);
    UISlider *slideSwitch = [[UISlider alloc] initWithFrame:frame];
    [slideSwitch addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventTouchUpInside];
    slideSwitch.backgroundColor = [UIColor clearColor];
    [slideSwitch setThumbImage:TTSTYLEVAR(slideSwitchThumbImage) forState:UIControlStateNormal];
    [slideSwitch setMinimumTrackImage:TTSTYLEVAR(slideSwitchLeftImage) forState:UIControlStateNormal];
    [slideSwitch setMaximumTrackImage:TTSTYLEVAR(slideSwitchRightImage) forState:UIControlStateNormal];
    [slideSwitch thumbRectForBounds: thumb trackRect: frame value: slideSwitch.value];
    slideSwitch.minimumValue = 0.0;
    slideSwitch.maximumValue = 1.0;
    slideSwitch.continuous = YES;
    slideSwitch.value = 0.0;
    [self.view addSubview:slideSwitch];
}
Share
Posted in Uncategorized at July 14th, 2011. No Comments.

TTStyle的缩进实现

在写一些小的应用时候遇见一个需求,需要在view前面做一个list-type,显然需要一个小缩进,不知到three20有没有其他的解决方案,方正我处理方式是写另一个style。使用方法是这样的。

 [TTSolidFillStyle styleWithColor:color next: [TTAreaStyle styleWithRect:CGRectMake(0, 0, Indent, 18.0) style: [TTLinearGradientFillStyle styleWithColor1:RGBCOLOR(255, 255, 255) color2:RGBCOLOR(0, 0, 0) next:nil] next: [TTBoxStyle styleWithPadding:UIEdgeInsetsMake(0, Indent + Gap, 0, 0) next: [TTTextStyle styleWithFont:nil color:TTSTYLEVAR(linkTextColor) minimumFontSize:14 shadowColor:[UIColor colorWithWhite:255 alpha:0.4] shadowOffset:CGSizeMake(0, -1) textAlignment:UITextAlignmentLeft verticalAlignment:UIControlContentVerticalAlignmentCenter lineBreakMode:UILineBreakModeWordWrap numberOfLines:0 next:nil ]]]]; 

代码如下

//
//  TTAreaStyle.h
//  infzm
//
//  Created by lin waiwai on 1/19/11.
//  Copyright 2011 __waiwai__. All rights reserved.
//

@interface TTAreaStyle : TTStyle {
	CGRect _rect;
	TTStyle *_style;
}

@property (nonatomic, retain) TTStyle*  style;
@property (nonatomic) CGRect rect;

+(TTAreaStyle*)styleWithRect:(CGRect)rect style:(TTStyle*)stylez next:(TTStyle*)next;

@end
//
//  TTAreaStyle.m
//  infzm
//
//  Created by lin waiwai on 1/19/11.
//  Copyright 2011 __waiwai__. All rights reserved.
//

#import "TTAreaStyle.h"

// Core
#import "Three20Core/TTCorePreprocessorMacros.h"

@implementation TTAreaStyle

@synthesize rect  = _rect;
@synthesize style = _style;

///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)dealloc {
	TT_RELEASE_SAFELY(_style);
	[super dealloc];
}

///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark Class public

///////////////////////////////////////////////////////////////////////////////////////////////////
+(TTAreaStyle*)styleWithRect:(CGRect)rect style:(TTStyle*)stylez next:(TTStyle*)next; {
	TTAreaStyle* style = [[[self alloc] initWithNext:next] autorelease];
	style.rect = rect;
	style.style = stylez;
	return style;
}

///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark TTStyle

///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)draw:(TTStyleContext*)context {

	CGContextRef ctx = UIGraphicsGetCurrentContext();
	CGRect rect = context.frame;
	CGContextSaveGState(ctx);

	[context.shape addToPath:rect];
	CGContextClip(ctx);
	CGContextClipToRect(ctx,self.rect);
	[self.style draw:context];

	CGContextRestoreGState(ctx);

	return [self.next draw:context];
}

@end
Share
Posted in iphone at January 18th, 2011. 2 Comments.

稀缺资源的管理

在objective-c中,对稀缺资源的管理,如果文件描述符,网络链接,缓冲/缓存,有一些建议,如果你使用一个特别设计的类进行管理的话,那么你在释放这些资源的时候,也许假设这些资源的释放应该在dealloc中。但是实际上dealloc可能的调用可能被由于bug或者程序崩溃,延迟或者根本没有执行。

将这些东西的释放设计在dealloc中,有几点会让你蛋疼。

1,如果期望顺序的崩溃机制,这是不可能,只要在对象图中有一个进入个自动释放内存池中,那么崩溃的顺序改变了。

2,稀缺资源无法回收

3,如果有一个对象陷入内存池被释放,那么清理的逻辑无法正常执行,而其他线程可能已经在访问,这些都是致命行为。

所以最好的办法是,你设计的管理对象能够知道稀缺资源已经不在被使用了而马上将资源释放,接着释放对象自己。dealloc也会被正确的调用。

Share
Posted in iphone at December 4th, 2010. No Comments.