1天学习1个类 UIButton 类 示例

示例演示

1天学习1个类 UIButton 类 示例_第1张图片


下面是代码:

//
//  main.m
//  ControlDemo
//
//  Created by watsy0007 on 12-6-3.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController 

@property (nonatomic) NSInteger nDepth;

@end

@implementation ViewController

@synthesize nDepth = _nDepth;

- (id) init {
    if (self = [super init]) {
        _nDepth = 0;
    }
    return self;
}

- (void) BarButtonItemAction:(id) sender {

    if (_nDepth == 0) {
        ViewController *vc = [[ViewController alloc] init];
        vc.nDepth = self.nDepth + 1;
        [self.navigationController pushViewController:vc animated:YES];
        [vc release];
    }
}

- (UIBarButtonItem *) createNewItemWith {
    UIBarButtonItem *item;

    item = [[UIBarButtonItem alloc] initWithTitle:@"watsy0007" style:UIBarButtonItemStylePlain target:self action:@selector(BarButtonItemAction:)];
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 60, 30)];
    view.backgroundColor = [UIColor blueColor];
    
    item.target = self;
    item.action = @selector(BarButtonItemAction:);
    item.style = UIBarButtonItemStylePlain;
    item.tintColor = [UIColor colorWithRed:155.0 / 255.0 
                                     green:155.0 / 255.0 
                                      blue:155.0 / 255.0 
                                     alpha:0.7];
    [item setTitle:@"push"];

    //设置标题显示的位置
    [[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setBackButtonTitlePositionAdjustment:UIOffsetMake(20, 5) forBarMetrics:UIBarMetricsDefault];
    
    [view release];
    
    return [item autorelease];
}

- (UIButton *) createNewButton {
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.backgroundColor = [UIColor grayColor];
    btn.frame = CGRectMake(0, 260, 320, 50);
    [btn setTitle:@"UIButton Demo" forState:UIControlStateNormal];
    btn.titleLabel.backgroundColor = [UIColor lightTextColor];
    btn.titleLabel.font            = [UIFont systemFontOfSize: 20];
    btn.titleLabel.lineBreakMode   = UILineBreakModeTailTruncation;
    btn.titleLabel.numberOfLines = 2;
    btn.titleLabel.shadowOffset    = CGSizeMake (8.0, 3.0);
    btn.reversesTitleShadowWhenHighlighted = YES;
    btn.showsTouchWhenHighlighted = YES;
    [btn setTitleShadowColor:[UIColor redColor] forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
//    btn.contentEdgeInsets = UIEdgeInsetsMake(5, 10, 6, 5);
//    btn.titleEdgeInsets = UIEdgeInsetsMake(5, 10, 6, 5);
    
    NSLog(@"backgoundRectForBounds:%@\nContentRectForBounds:%@\ntitleRectForBounds:%@\n",
          NSStringFromCGRect([btn backgroundRectForBounds:self.view.bounds]),
          NSStringFromCGRect([btn contentRectForBounds:self.view.bounds]),
          NSStringFromCGRect([btn titleRectForContentRect:self.view.bounds]));
    
    [btn setTitle:@"123123\n456456" forState:UIControlStateNormal];
    

    
    return btn;
}

- (void) loadView {
    [super loadView];
    UILabel *label = [[UILabel alloc] initWithFrame:self.view.bounds];
    label.numberOfLines = 3;
    label.textAlignment = UITextAlignmentCenter;
    label.text = @"watsy0007 \n QQ:258841679\n群:125807534";
    [self.view addSubview:label];
    [label release];
    
    self.title = @"UIButton Class Reference Demo";

    [self.view addSubview:[self createNewButton]];
    
    if (self.nDepth == 0) {
        self.navigationItem.rightBarButtonItem = [self createNewItemWith];
    } 
}


- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
    return YES;
}

- (void) viewDidLoad {
    [super viewDidLoad];
}

@end


@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) UIViewController *viewController;

@end

@implementation AppDelegate

@synthesize window = _window;
@synthesize viewController = _viewController;

- (void) dealloc {
    [_window release];
    [_viewController release];
    
    [super dealloc];
}

- (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    
    self.viewController = [[ViewController alloc] init];
    
    UINavigationController *controller = [[UINavigationController alloc] initWithRootViewController:self.viewController]; 
    self.window.rootViewController = controller;
    [controller release];
    
    [self.window makeKeyAndVisible];
    return YES;
}

@end

int main(int argc, char *argv[])
{
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}


你可能感兴趣的:(1天学习1个类 UIButton 类 示例)