UI控件篇
一、UILabel
1、调整UILabel的行间距
UILabel *noDataLabel = [[UILabel alloc]initWithFrame:rect];//创建此处省略
. . .
NSMutableAttributedString *attributedString1 = [[NSMutableAttributedStringalloc] initWithString:noDataLabel.text];
NSMutableParagraphStyle *paragraphStyle1 = [[NSMutableParagraphStylealloc]init];
[paragraphStyle1setLineSpacing:8];//调整行间距
[attributedString1addAttribute:NSParagraphStyleAttributeNamevalue:paragraphStyle1 range:NSMakeRange(0, [noDataLabel.textlength])];
[noDataLabelsetAttributedText:attributedString1];
noDataLabel.font = [UIFontsystemFontOfSize:17];
[noDataLabelsizeToFit];
2、给label的内容中的特定字符添加颜色,可以调整大小
具体内容 传送门
二、UIButton
1、文字左对齐
[button setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
button.transform =CGAffineTransformRotate(button.transform,-M_PI/4.0);
3、添加阴影
addBtn.layer.shadowColor = [UIColorblackColor].CGColor;
addBtn.layer.shadowOffset =CGSizeMake(5.0f,5.0f);
addBtn.layer.shadowOpacity =0.5f;
三、UIView
1、加渐变色,在图片上加一层渐变色
UIView *imageForView = [[UIViewalloc]initWithFrame:defaulTopImageView.bounds];
imageForView.backgroundColor = [UIColorclearColor];
CAGradientLayer *gradient = [CAGradientLayerlayer];
gradient.frame = imageForView.frame;
gradient.colors = [NSArrayarrayWithObjects:(id)[UIColorclearColor].CGColor,
(id)[UIColorcolorWithWhite:0alpha:0].CGColor,
(id)[UIColorcolorWithWhite:0alpha:0].CGColor,(id)[UIColorcolorWithWhite:0alpha:0.5].CGColor,nil];
[imageForView.layerinsertSublayer:gradientatIndex:0];
[defaulTopImageViewaddSubview:imageForView];
2、简单的抖动(登录时出错的效果)
CAKeyframeAnimation *shakeAnim = [CAKeyframeAnimationanimation];
shakeAnim.keyPath =@"transform.translation.x";
shakeAnim.duration =0.15;
CGFloat delta =10;
shakeAnim.values =@[@0,@(-delta),@(delta),@0];
shakeAnim.repeatCount =2;
[self.loginView.layeraddAnimation:shakeAnimforKey:nil];
四、UITextField
1、在内容区域的左边加段空白即leftView属性
self.numberField.leftView = [[UIViewalloc]initWithFrame:CGRectMake(0,0,8,0)];
self.numberField.leftViewMode = UITextFieldViewModeAlways;
五、 NSString + 计算高度
/**
* 计算文字尺寸
*
* @param text 需要计算尺寸的文字
* @param font 文字的字体
* @param maxSize 文字的最大尺寸
*/
- (CGSize)sizeWithText:(NSString *)text font:(UIFont *)font maxSize:(CGSize)maxSize
{
NSDictionary *attrs =@{NSFontAttributeName : font};
return [textboundingRectWithSize:maxSizeoptions:NSStringDrawingUsesLineFragmentOriginattributes:attrscontext:nil].size;
}
手势篇
一 、 长按手势,注意防止调用两次,通过长按的状态
UILongPressGestureRecognizer * longPressGr = [[UILongPressGestureRecognizeralloc]initWithTarget:selfaction:@selector(longPressToDo:)];
longPressGr.minimumPressDuration =1.0;
[imageViewaddGestureRecognizer:longPressGr];
-(void)longPressToDo:(UILongPressGestureRecognizer *)gesture
{
if(gesture.state ==UIGestureRecognizerStateBegan)
{
}
}
基本数据类型篇
NSMutableArray *array = [[[NSMutableArray alloc] initWithObjects:@"1", @"2", @"3", nil] autorelease];
array = (NSMutableArray *)[[array reverseObjectEnumerator] allObjects];
1、通过代码设置控件的frame,取屏幕的宽高为参照物时,记得添加属性
dock.autoresizingMask =UIViewAutoresizingFlexibleHeight;
2 、监听横竖屏变化的函数
/**
* 当屏幕即将旋转的时候调用
*
* @param toInterfaceOrientation 旋转完毕后的最终方向
* @param duration 旋转动画所花费的时间
*/
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) {// 是否横屏
self.dock.width = MJDockLanscapeW; // 取横屏时的宽度
}else {
self.dock.width = MJDockPortraitW; // 取竖屏时的宽度
}
}
UIImage篇
1.ios 后让tabBar上的图片保持自身颜色的代码
childVc.tabBarItem.selectedImage = [[UIImageimageNamed:selectedImageName]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
沙盒篇
1.沙盒路径
NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
(持续更新中。。。。)