欢迎来到天天文库
浏览记录
ID:37325673
大小:372.46 KB
页数:29页
时间:2019-05-21
《OC常用入门代码》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、常用代码整理:Xcode6,引入viewController代码:1.在Appdelegate先引入ViewController头文件2.ViewControllervc=(ViewControlleralloc)init;Self.window.rootViewController=vc;然后vc就代表ViewController对象,就可以调用其属性和方法了。一般用通知来取代上面的写法://发送通知[[[[NSNotificationCenterdefaultCenter]]postNotificationName:通知名object::nil];//接受通知//IB创建的对象调用in
2、itWithCoder要复写方法;-(instancetype)initWithCoder(NSCoder*)coder{self=[superinitWithCoder:coder];if(self){[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(method)name:@”….”object:nil];}returnself;}UILabel多行文字自动换行(自动折行)UIView*footerView=[[UIViewalloc]initWithFrame:CGRectMake(10,10
3、0,300,180)];UILabel*label=[[UILabelalloc]initWithFrame:CGRectMake(10,100,300,150)];label.text=@"whereareyou?whereareyou?whereareyou?whereareyou?whereareyou?whereareyou?whereareyou?whereareyou?whereareyou?whereareyou?";//清空背景颜色label.backgroundColor=[UIColorclearColor];//设置字体颜色为白色label.textColor=[UI
4、ColorwhiteColor];//文字居中显示label.textAlignment=UITextAlignmentCenter;//自动折行设置label.lineBreakMode=UILineBreakModeWordWrap;label.numberOfLines=0;iOS的UILabel设置居上对齐,居中对齐,居下对齐在iOS中默认的UILabel中的文字在竖直方向上只能居中对齐,博主参考国外网站,从UILabel继承了一个新类,实现了居上对齐,居中对齐,居下对齐。具体如下:typedef enum { VerticalAlignmentTop = 0, //
5、default VerticalAlignmentMiddle, VerticalAlignmentBottom, } VerticalAlignment; @interface myUILabel : UILabel { @private VerticalAlignment _verticalAlignment; } @property (nonatomic) VerticalAlignment verticalAlignment; @end @implementation myUILabel @synthesize verticalAli
6、gnment = verticalAlignment_; - (id)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { self.verticalAlignment = VerticalAlignmentMiddle; } return self; } - (void)setVerticalAlignment:(VerticalAlignment)verticalAlignment { verticalAlignment_ =
7、 verticalAlignment; [self setNeedsDisplay]; } - (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines { 1. CGRect textRect = [super textRectForBounds:bounds limited
此文档下载收益归作者所有