20150629_UI之坐标系及UILabel

来源:互联网 发布:lp网络公共广播系统 编辑:程序博客网 时间:2024/05/22 12:36

在IOS中系统的坐标系如下图:


坐标的操作:

////  AppDelegate.m//  IOS150629_UI(01)_坐标系////  Created by PengJunlong on 15/6/29.//  Copyright (c) 2015年 Peng Junlong. All rights reserved.//#import "AppDelegate.h"@interface AppDelegate ()@end@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    // Override point for customization after application launch.    //window的默认颜色是透明色clearColor;    self.window.backgroundColor = [UIColor whiteColor];        NSLog(@"window:x = %.2f,y = %.2f,height = %.2f,width = %.2f",          self.window.frame.origin.x,          self.window.frame.origin.y,          self.window.frame.size.height,          self.window.frame.size.width);    //IOS坐标系    //frame坐标系:相对应父视图的坐标,是相对坐标    //bound坐标:IOS坐标(起始点从(0,0)开始),左上角,绝对坐标    //状态栏的高度是20        //屏幕大小    CGRect frame1 = [[UIScreen mainScreen] bounds];    NSLog(@"screen:x = %.2f,y = %.2f,height = %.2f,width = %.2f",          frame1.origin.x,frame1.origin.y,          frame1.size.height,          frame1.size.width);        UIWindow *window1 = [[UIWindow alloc] initWithFrame:CGRectMake(20, 20, self.window.frame.size.width-40, self.window.frame.size.height-40)];    window1.backgroundColor = [UIColor cyanColor];    [self.window addSubview:window1];    //是window1在最上层显示    [window1 makeKeyAndVisible];        //通常一个应用程序有一个Window    //相当于一个容器    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(window1.frame.origin.x, 100, window1.frame.size.width-40, 50)];    view1.backgroundColor = [UIColor redColor];    [window1 addSubview:view1]; //window相当于一个数组,view1的retainCount会加1    self.window.rootViewController = nil;    return YES;}- (void)applicationWillResignActive:(UIApplication *)application {    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.}- (void)applicationDidEnterBackground:(UIApplication *)application {    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.}- (void)applicationWillEnterForeground:(UIApplication *)application {    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.}- (void)applicationDidBecomeActive:(UIApplication *)application {    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.}- (void)applicationWillTerminate:(UIApplication *)application {    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.}@end

UILabel的使用:

////  AppDelegate.m//  IOS150629_UI(02)_UILabel////  Created by PengJunlong on 15/6/29.//  Copyright (c) 2015年 Peng Junlong. All rights reserved.//#import "AppDelegate.h"@interface AppDelegate ()@end@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    // Override point for customization after application launch.    //UILabel继承UIView    //用来显示文字,不可以手动编辑    self.window.backgroundColor = [UIColor whiteColor];    UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(20, 100, self.window.frame.size.width-40, 50)];        //修改背景颜色    label1.backgroundColor = [UIColor cyanColor];    //把label1添加到window上显示    [self.window addSubview:label1];    label1.text = @"用户名";    //对齐方式    label1.textAlignment = NSTextAlignmentCenter;    label1.textColor = [UIColor redColor];    //文本阴影颜色及偏移    label1.shadowColor = [UIColor grayColor];    label1.shadowOffset = CGSizeMake(5, 5);    //设置文本高亮状态及颜色    label1.highlighted = YES;   //设置文字的高亮状态,若为YES则处在高亮状态,                                //否则highlightedTextColor不起作用    label1.highlightedTextColor = [UIColor blueColor];        UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(20,200,self.window.frame.size.width-40,100)];    [self.window addSubview:label2];    label2.backgroundColor = [UIColor yellowColor];    label2.text = @"Hello China Hello China Hello China Hello China China Hello China China Hello China China Hello China China Hello China China Hello China China Hello China Hello China Hello China Hello China Hello China China Hello China China Hello China China Hello China China Hello China China Hello China China Hello China 你好中国";    label2.textAlignment = NSTextAlignmentCenter;    //NSLineBreakByTruncatingHead,/* Truncate at head of line: "...wxyz" */    //NSLineBreakByTruncatingTail,/* Truncate at tail of line: "abcd..." */    //NSLineBreakByTruncatingMiddle/* Truncate middle of line:  "ab...yz" */    //label2.lineBreakMode = NSLineBreakByTruncatingMiddle;//text中文本显示不完全时,使用截断,设置显示格式,Hello China...llo China    //NSLineBreakByCharWrapping,以字符截断换行/* Wrap at character boundaries */    //NSLineBreakByWordWrapping,    以单词截断换行/* Wrap at word boundaries, default */    //NSLineBreakByClipping,显示不了的文字剪掉/* Simply clip */    label2.numberOfLines = 0;   //value =0时,显示多行,根据文本内容多少和label的高度显示;为1,2...则显示相应行数    //label2.lineBreakMode = NSLineBreakByWordWrapping;        //自适应字体大小,以使在label中能够完全显示所有文字    //A Boolean value indicating whether the font size should be reduced    //in order to fit the title string into the label’s bounding rectangle.    label2.adjustsFontSizeToFitWidth = YES; //不能和lineBreakMode同时使用        UILabel *label3 = [[UILabel alloc] initWithFrame:CGRectMake(20, 350, self.window.frame.size.width-40, 50)];    [self.window addSubview:label3];    label3.backgroundColor = [UIColor cyanColor];    label3.text = @"中国上海Hello China";    label3.textAlignment = NSTextAlignmentCenter;    //默认字体大小是17    //设置字体大小    //label3.font = [UIFont systemFontOfSize:33];    //遍历字体库    //获取所有的字体簇    for (NSString *familyName in [UIFont familyNames])    {        //获取字体簇中所有的字体        for (NSString *font in [UIFont fontNamesForFamilyName:familyName])        {            NSLog(@"Fonts=%@",font);        }    }    //设置字体类型及大小    label3.font = [UIFont fontWithName:@"Cochin-Italic" size:26];    self.window.rootViewController = nil;    return YES;}- (void)applicationWillResignActive:(UIApplication *)application {    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.}- (void)applicationDidEnterBackground:(UIApplication *)application {    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.}- (void)applicationWillEnterForeground:(UIApplication *)application {    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.}- (void)applicationDidBecomeActive:(UIApplication *)application {    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.}- (void)applicationWillTerminate:(UIApplication *)application {    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.}@end


0 0
原创粉丝点击