本文学习自千锋教学视频: iOS开发视频教程《UI-第1季-2013年新版》
UILabel是ios中的文字组件,主要用于文字的显示。
一、截图效果
分别为字体颜色、斜体、字体类型、阴影效果。(高亮效果未给出)
二、代码:
WPAppDelegate.m
#import "WPAppDelegate.h"
@implementation WPAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
// 设置文字位置和大小
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 100, 300, 50)];
// 设置文字内容
label.text = @"Hello iPhone";
// 设置文字颜色
label.textColor = [UIColor blueColor];
// 设置文字背景颜色
label.backgroundColor = [UIColor clearColor];
// 设置文字对齐方式(UITextAlignmentCenter)
label.textAlignment = NSTextAlignmentCenter;
// 设置粗体
//label.font = [UIFont boldSystemFontOfSize:20];
// 设置斜体
//label.font = [UIFont italicSystemFontOfSize:20];
// 获得系统支持字体
NSArray *names = [UIFont familyNames];
for (NSString *name in names) {
// 在调试输出栏输出信息
NSLog(@"%@",name);
}
// 设置文字字体和字号
label.font = [UIFont fontWithName:@"Courier New" size:30];
// 设置文字阴影颜色
label.shadowColor = [UIColor grayColor];
// 设置文字阴影偏移量
label.shadowOffset = CGSizeMake(3, 3);
// 设置是否高亮
label.highlighted = true;
// 设置文字高亮颜色
label.highlightedTextColor = [UIColor greenColor];
// 设置文字大小
//[label setFont:[UIFont systemFontOfSize:40]];
// 设置粗体
//[label setFont:[UIFont boldSystemFontOfSize:30]] ;
// 将label加入到屏幕显示上
[self.window addSubview:label];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
@end
代码除了第一个函数需要编辑,其它的都不需要修改,这里没有贴其它函数的代码。注释写得很清楚,就不用解释了。
三、总结
1.平台使用技巧
撤销 = cmd + z (cmd键在文字编辑时相当与Windows的Ctrl键)
复制 = cmd + c ;粘贴 = cmd + v ;同理cmd+x;cmd+a
重命名文件 = 选中文件,在回车;或者选中文件,1秒后再点击(避免识别为双击)
换输入法 = cmd + 空格
2.报错
模拟器正在使用…… = 重启XCode(使用时注意用完手动停止运行,可以减少此情况的发生)
Tread中断…… = 由于ios的版本问题引发的不定时错误,重新运行一次就好了,不行就重启(建议使用最新的4寸屏幕模拟器,可减少版本引起的问题)
3.知识
-调试常用,oc中字符串表示方法为 @“String” ,对象替换赋值方式为 %@,value
// 在调试输出栏输出信息
NSLog(@"%@",name);
-用label.font 设置字体格式会覆盖掉之前的字体格式,如果需要混合字体格式则需要通过
[label setFont:[UIFont systemFontOfSize:40]];的方式设置字体属性同时setFont中有更多的格式方法选用