常用代码整理:Xcode6,引入viewController代码:1.在Appdelegate先引入ViewController头文件2.ViewControllervc=(ViewControlleralloc)init;Self.window.rootViewController=vc;然后vc就代表ViewController对象,就可以调用其属性和方法了。一般用通知来取代上面的写法://发送通知[[[[NSNotificationCenterdefaultCenter]]postNotificationName:通知名object::nil];//接受通知//IB创建的对象调用initWithCoder要复写方法;-(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,100,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=[UIColorwhiteColor];//文字居中显示label.textAlignment=UITextAlignmentCenter;//自动折行设置label.lineBreakMode=UILineBreakModeWordWrap;label.numberOfLines=0;iOS的UILabel设置居上对齐,居中对齐,居下对齐在iOS中默认的UILabel中的文字在竖直方向上只能居中对齐,博主参考国外网站,从UILabel继承了一个新类,实现了居上对齐,居中对齐,居下对齐。具体如下:typedefenum{VerticalAlignmentTop=0,//defaultVerticalAlignmentMiddle,VerticalAlignmentBottom,}VerticalAlignment;@interfacemyUILabel:UILabel{@privateVerticalAlignment_verticalAlignment;}@property(nonatomic)VerticalAlignmentverticalAlignment;@end@implementationmyUILabel@synthesizeverticalAlignment=verticalAlignment_;-(id)initWithFrame:(CGRect)frame{if(self=[superinitWithFrame:frame]){self.verticalAlignment=VerticalAlignmentMiddle;}returnself;}-(void)setVerticalAlignment:(VerticalAlignment)verticalAlignment{verticalAlignment_=verticalAlignment;[selfsetNeedsDisplay];}-(CGRect)textRectForBounds:(CGRect)boundslimitedToNumberOfLines:(NSInteger)numberOfLines{1.CGRecttextRect=[supertextRectForBounds:boundslimitedToNumberOfLines:numberOfLines];2.switch(self.verticalAlignment){caseVerticalAlignmentTop:textRect.origin.y=bounds.origin.y;break;caseVerticalAlignmentBottom:textRect.origin.y=bounds.origin.y+bounds.size.height-textRect.size.height;break;caseVerticalAlignmentMiddle://Fallthrough.default:textRect.origin.y=bounds.origin.y+(bounds.size.height-textRect.size.height)/2.0;}returntextRect;}-(void)drawTextInRect:(CGRect)requestedRect{CGRectactualRect=[selftextRectForBounds:requestedRectlimitedToNumberOfLines:self.numberOfLines];[superdrawTextInRect:actualRect];}@endlbl_mylabel=[[myUILabelalloc]initWithFrame:CGRectMake(20,50,150,600)];UIColor*color=[UIColorcolorWithPatternImage:[UIImageimageNamed:@"halfTransparent.png"]];//使用半透明图片作为label的背景色lbl_mylabel.backgroundColor=color;lbl_mylabel.textAlignment=UITextAlignmentLeft;lbl_mylabel.textColor=UIColor.whiteColor;lbl_mylabel.lineBreakMode=UILineBreakModeWordWrap;lbl_mylabel.numberOfLines=0;[lbl_mylabelsetVerticalAlignment:VerticalAlignmentTop];[selfaddSubview:lbl_mylabel];iosUILabel变量名不能为title-[UILabelcopyWithZone:]:unrecognizedselectorsenttoinstance遇到了这样一个错误,找了半天没找到是什么错误,于是,Google搜索,打开第一个链接http://stackoverflow.com/questions/10784207/uilabel-copywithzone-unrecogni...