UILabel Demo Objective-C代码示例

UILabel 的代码示例挺适合刚上手 iOS 开发的你。项目里用 Objective-C 写了一堆实用的 UILabel 玩法:怎么创建、怎么设置字体、颜色、对齐方式,还有怎么让文字自动换行、加省略号这些。嗯,代码风格也清晰,响应也快。你还可以看到像adjustsFontSizeToFitWidth这种功能,挺适合动态适配文字的场景。

UILabel 的动态特性也讲得比较到位,比如自适应字体大小、多行文本显示这些。代码写法挺直观,比如:

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 200, 30)];
label.text = @"Hello, World!";
label.font = [UIFont systemFontOfSize:18];
label.textColor = [UIColor blackColor];
label.textAlignment = NSTextAlignmentCenter;
label.numberOfLines = 0;
label.lineBreakMode = NSLineBreakByTruncatingTail;

动画效果、触摸事件监听这类功能也有提到,比如用alpha实现淡入淡出,或者加点transform做旋转。你还可以通过touchesBegan:这些方法监听 UILabel 的交互,蛮适合做一些轻交互场景。

Storyboard 的玩法也没落下,用 Interface Builder 拖一个 UILabel 出来,再调属性也方便。不过我更推荐你多动手写代码,毕竟动态添加 UILabel 到视图中挺常见的。

如果你正在做 iOS UI 界面优化或者刚学 Objective-C,真挺建议你下来看一眼这个示例项目。用得顺手,改起来也方便。

zip
UILabelTest.zip 预估大小:44个文件
folder
UILabelTest 文件夹
file
.DS_Store 6KB
folder
UILabelTest 文件夹
file
ZYAppDelegate.m 2KB
folder
en.lproj 文件夹
file
ZYViewController.xib 6KB
file
InfoPlist.strings 45B
file
main.m 341B
file
UILabelTest-Prefix.pch 325B
file
UILabelTest-Info.plist 1KB
file
ZYViewController.h 267B
file
ZYViewController.m 1KB
file
Default-568h@2x.png 18KB
file
Default.png 6KB
file
Default@2x.png 16KB
file
ZYAppDelegate.h 373B
folder
UILabelTest.xcodeproj 文件夹
folder
project.xcworkspace 文件夹
file
contents.xcworkspacedata 156B
folder
xcuserdata 文件夹
folder
zhangyuc.xcuserdatad 文件夹
file
UserInterfaceState.xcuserstate 11KB
folder
xcuserdata 文件夹
folder
zhangyuc.xcuserdatad 文件夹
folder
xcschemes 文件夹
file
xcschememanagement.plist 483B
file
UILabelTest.xcscheme 3KB
file
project.pbxproj 13KB
folder
.git 文件夹
folder
info 文件夹
file
exclude 35B
file
index 1KB
folder
objects 文件夹
folder
pack 文件夹
folder
08 文件夹
file
91b7aabfcf3422423b109c8beed2bab838c607 2KB
folder
df 文件夹
file
a75dad0f8879dcfd66185143f03244447b14e1 2KB
folder
info 文件夹
folder
4c 文件夹
file
8ca6f693f96d511e9113c0eb59eec552354e42 2KB
folder
35 文件夹
file
b84cffeb4db607983c8383fa9199731dc3aba0 2KB
folder
da 文件夹
file
50d278d5054c3f6ce1fda8aa63c6c70df17f4e 480B
folder
be 文件夹
file
02b2c1dbe13bdf03576721733914e446ce87d8 3KB
folder
d3 文件夹
file
717a3190616309e3fe04915cd9f607b34c651e 321B
folder
97 文件夹
file
6afcac2656f67966c1b7a40583e4c057d306e5 178B
folder
0d 文件夹
file
816adc69b224c7540a977d110224b33b471ed1 233B
folder
47 文件夹
file
7b28ff8f86a3158a71c4934fbd3a2456717d7a 61B
folder
13 文件夹
file
25e01b65363450d582d3d42bbf11d3dd163a45 1KB
folder
ec 文件夹
file
8e0c262c7040a2788ee6e8e909bb1a11b80ec5 264B
folder
65 文件夹
file
8f3fe9392b32c957eac595f33f9c78b2f537b1 254B
file
HEAD 23B
file
config 138B
folder
refs 文件夹
folder
heads 文件夹
folder
tags 文件夹
folder
branches 文件夹
folder
hooks 文件夹
file
pre-applypatch.sample 398B
file
commit-msg.sample 896B
file
pre-commit.sample 2KB
file
applypatch-msg.sample 452B
file
prepare-commit-msg.sample 1KB
file
update.sample 4KB
file
post-update.sample 189B
file
pre-rebase.sample 5KB
file
description 73B
...
zip 文件大小:60.41KB