iOS中UILabel和UIButton的自定义设置

来源:互联网 发布:纽约爱乐 知乎 编辑:程序博客网 时间:2024/06/03 18:09

这是使用empty application 创建的工程,这里主要是在 didFinshLaunchingWithoptions:方法中操作。


#import <UIKit/UIKit.h>@interface AppDelegate : UIResponder <UIApplicationDelegate>@property (strong,nonatomic) UILabel *message;@property (strong, nonatomic) UIWindow *window;@end

#import "AppDelegate.h"@implementation AppDelegate//*************************ios版本是6.1*************************- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{    //设置应用的窗口大小,uiscreen()获取主屏幕的大小    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];        // Override point for customization after application launch.    self.window.backgroundColor = [UIColor grayColor];            //UIViewController *rootViewControler = [[UIViewController alloc]init];        //生成一个标签,通过frame的方式的设置坐标和大小    self.message  = [[UILabel alloc]initWithFrame:CGRectMake(70, 200, 200, 20)];        //设置uilabel的字体对齐方式,在ios6中使用NSTextAlignment,UITextAlignment不被支持    //这是UITextAlignment的定义    /*************************************************          enum {     UITextAlignmentLeft = 0,     UITextAlignmentCenter,     UITextAlignmentRight,                        } NS_DEPRECATED_IOS(2_0,6_0);     typedef NSTextAlignment UITextAlignment;     *************************************************/        //这是NSTextAlignment的定义    /**************************************************     enum {     NSTextAlignmentLeft      = 0,     NSTextAlignmentCenter    = 1,     NSTextAlignmentRight     = 2,     NSTextAlignmentJustified = 3,     NSTextAlignmentNatural   = 4,     };     typedef NSInteger NSTextAlignment;     *************************************************/    //message.textAlignment = 1等价于 = NSTextAlignmentCenter;并且3,4是不可用的    self.message.textAlignment = 1;        //设置标签上面的文本    self.message.text = @"Hello World!";        //设置标签上面的文本颜色    self.message.textColor = [UIColor blueColor];        //设置标签字体大小    self.message.font = [UIFont systemFontOfSize:23];        //将标签添加为window的子view    [self.window addSubview:self.message];        //设置自定义的一个按钮,type设置为圆角矩形    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect ];    button.frame = CGRectMake(130, 250, 90 , 40);    button.backgroundColor = [UIColor blackColor];    [button setTitle:@"Hello" forState:UIControlStateNormal];    [button setTitle:@"Hi,Go" forState:UIControlStateHighlighted];        //设置button的不同状态的图片样式    UIImage *normalImage = [UIImage imageNamed:@"屏幕快照 2013-05-26 上午10.45.15.png"];    UIImage *pressImage = [UIImage imageNamed:@"屏幕快照 2013-05-26 上午10.44.30.png"];    /*     UIControlState,<< 这种位操作符表示这几个选项可以叠加     typedef NS_OPTIONS(NSUInteger, UIControlState) {     UIControlStateNormal       = 0,     UIControlStateHighlighted  = 1 << 0,                  // used when UIControl isHighlighted is set     UIControlStateDisabled     = 1 << 1,     UIControlStateSelected     = 1 << 2,                  // flag usable by app (see below)     UIControlStateApplication  = 0x00FF0000,              // additional flags available for application use     UIControlStateReserved     = 0xFF000000               // flags reserved for internal framework use     };     */    [button setBackgroundImage:normalImage forState:UIControlStateNormal];    [button setBackgroundImage:pressImage forState:UIControlStateHighlighted];        //给button添加事件和操作方法,@selector()通过hash映射的方式将方法转换,方便系统查找,提高效率    [button addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];        [self.window addSubview:button];    //button.currentTitle = @"Hello";        //测试NSDate    NSDate *date = [NSDate date];    NSLog(@"[NSDate date] is %@",date);    NSDate *userDate = [[NSDate alloc]init];    NSLog(@"[NSDate userDate] is %@",userDate);    [date earlierDate:userDate];        NSLog(@"self.window.subviews : %@",self.window.subviews);    [self.window makeKeyAndVisible];    return YES;;    }#pragma mark buttonClickEvent- (void) click{    //静态局部变量做为控制开关    static BOOL flag;    if (flag == 0) {        self.message.text = @"Hello Xcode!";        flag =!flag;    } else {        self.message.text = @"Hello World!";        flag =!flag;    }    }

这里可以看到self.window.subviews的输出结果,有我们的label和button。

2013-05-26 12:41:10.233 emptyProject[10376:11303] self.window.subviews : (

    "<UILabel: 0x76791b0; frame = (70 200; 200 20); text = 'Hello World!'; clipsToBounds = YES; userInteractionEnabled = NO; layer = <CALayer: 0x7679290>>",

    "<UIRoundedRectButton: 0x767b5d0; frame = (130 250; 90 40); opaque = NO; layer = <CALayer: 0x767b740>>"


原创粉丝点击