实用代码汇总


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];

2、旋转

   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 allocinitWithObjects:@"1"@"2"@"3"nilautorelease];

array = (NSMutableArray *)[[array reverseObjectEnumeratorallObjects];




                             横竖屏适配篇

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];




(持续更新中。。。。)



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值