iOS中UILabel和UIButton的自定义设置
2013-05-26 12:44
288 查看
这是使用empty application 创建的工程,这里主要是在 didFinshLaunchingWithoptions:方法中操作。
这里可以看到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>>"
#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>>"
相关文章推荐
- 【iOS开发-8】UIButton类型属性简单归纳以及自定义按钮的设置
- iOS 使用storyboard设置UIButton 和UILabel 的圆角
- iOS设置改变UILabel、UIButton的多种颜色和字体大小(非常好用)
- 【iOS开发-8】UIButton类型属性简单归纳以及自定义按钮的设置
- Ios开发Swift自定义UIbutton增加一个能设置高亮状态下背景颜色的方法
- IOS开发笔记--UIButton类型属性简单归纳以及自定义按钮的设置
- iOS开发:关于UILabel、UIButton、UITextField文字下划线的设置方法(涉及到富文本的知识)
- ios-自定义UITextFiled的属性设置
- IOS成长之路-UIButton定义和设置圆角
- IOS 开发之设置UIButton的title
- iOS.UIKit.02.UIButton_UILabel
- iOS 设置字体 自定义字体
- iOS UILabel 设置 行间距 和 字间距 并计算高度
- iOS自定义键盘输入样式(设置成时间选择器等)
- iOS设置UIButton文字显示位置和字体大小、颜色的方法
- iOS的UILabel设置居上对齐,居中对齐,居下对齐
- iOS UILabel 中的文本设置固定宽度
- iOS 开发之给UILabel 或者 UIButton标题加下划线
- 设置UIButton中的文字和图片,设置UILabel的文在显示不同颜色
- iOS 把UILabel的text设置成斜体