UIKit

  • 调整UILabel的行间距

    1
    2
    3
    4
    5
    6
    7
    8
    9
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];      
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:_placeHolderLable.text];

    [paragraphStyle setLineSpacing:20]; // 调整行间距

    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [_placeHolderLable.text length])];
    _placeHolderLable.attributedText = attributedString;
    [self.textView addSubview:_placeHolderLable];
    [_placeHolderLable sizeToFit];
  • 隐藏状态栏

    1
    [[UIApplication shareApplication] setStatusBarHidden: YES animated:NO];
  • 圆角按钮

    1
    UIButton *scaleUpButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
  • 取消UITableView分割线左边的15像素

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
    {
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
    [cell setSeparatorInset:UIEdgeInsetsZero];
    }

    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
    [cell setLayoutMargins:UIEdgeInsetsZero];
    }
    }

    // 并且 在ViewDidLoad里面调用下面方法即可 此种去间距操作仅适用与iOS8 以上 iOS7 只需设置setSeparatorInset:UIEdgeInsetsZero 就能将空白去掉

    -(void)cancelMarginsForTableView
    {
    if ([self.menageTable respondsToSelector:@selector(setSeparatorInset:)]) {
    [self.menageTable setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([self.menageTable respondsToSelector:@selector(setLayoutMargins:)]) {
    [self.menageTable setLayoutMargins:UIEdgeInsetsZero];
    }
    }
  • 新增IB辅助线

    • 选中某个需要作为参照物的控件
    • 点击菜单栏 Editor -> Guides -> Add vertical guides (或者 Add Horizonal guides)。
  • 代码定义constraints时,需要设置translatesAutoresizingMaskIntoConstraints为YES。

    1
    -setTranslatesAutoresizingMaskIntoConstraints:

UIImage

  • UIView 生成 UIImage

    1
    2
    3
    4
    5
    6
    7
    8
    9
    - (UIImage *)imageFromView: (UIView *) theView
    {
    UIGraphicsBeginImageContext(theView.frame.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [theView.layer renderInContext:context];
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return theImage;
    }
  • 从文件获取图片

    1
    2
    3
    NSString *thumbnailFile = [NSString stringWithFormat:@"%@/%@.png", [[NSBundle mainBundle] resourcePath], fileName];
    UIImage *thumbnail = [UIImage imageWithContentsOfFile:thumbnailFile];
    UIImage *image = [[UIImage alloc] initWithContentsOfFile:filePath];
  • 生成纯色图

    1
    2
    3
    4
    5
    6
    CGSize imageSize = CGSizeMake(100, 100);  
    UIGraphicsBeginImageContextWithOptions(imageSize, 0, [UIScreen mainScreen].scale);
    [[UIColor colorWithRed:0 green:0 blue:0 alpha:1.0] set];
    UIRectFill(CGRectMake(0, 0, imageSize.width, imageSize.height));
    UIImage *pressedColorImg = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
  • 图片合成

    1
    2
    3
    4
    5
    6
    7
    8
    - (UIImage *)addImage:(UIImage *)image1 toImage:(UIImage *)image2 {  
    UIGraphicsBeginImageContext(image1.size);
    [image1 drawInRect:CGRectMake(0, 0, image1.size.width, image1.size.height)];
    [image2 drawInRect:CGRectMake(0, 0, image2.size.width, image2.size.height)];
    UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return resultingImage;
    }
  • 截取部分UIImage

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    -(UIImage *)clipImageFromImage:(UIImage *)orgImage  Rect:(CGRect)clipRect{  
    CGImageRef imageRef = orgImage.CGImage;
    CGImageRef subImageRef = CGImageCreateWithImageInRect(imageRef, clipRect);
    CGSize size;
    size = clipRect.size;
    UIGraphicsBeginImageContext(size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextDrawImage(context, clipRect, subImageRef);
    UIImage* clipImage = [UIImage imageWithCGImage:subImageRef];
    CGImageRelease(subImageRef);
    UIGraphicsEndImageContext();
    return clipImage;
    }
  • 图片模糊处理
    注意:此方法需要导入 CoreImage的framework

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    #import <CoreImage/CoreImage.h>

    // 图片模糊化处理 边缘模糊
    - (UIImage*) blur:(UIImage*)theImage{
    CIContext *context = [CIContext contextWithOptions:nil];
    CIImage *inputImage = [CIImage imageWithCGImage:theImage.CGImage];
    CIFilter *blurFilter = [CIFilter filterWithName:@"CIGaussianBlur"];
    [blurFilter setValue:inputImage forKey:kCIInputImageKey];
    [blurFilter setValue:[NSNumber numberWithFloat:10.0f] forKey:@"inputRadius"]; CIImage *result = [blurFilter valueForKey:kCIOutputImageKey]; CGImageRef cgImage = [context createCGImage:result fromRect:[inputImage extent]]; UIImage *returnImage = [UIImage imageWithCGImage:cgImage]; CGImageRelease(cgImage);
    return returnImage;
    }
    // 全部模糊
    - (UIImage*) blur:(UIImage*)theImage
    {
    CIContext *context = [CIContext contextWithOptions:nil];
    CIImage *inputImage = [CIImage imageWithCGImage:theImage.CGImage];
    CIFilter *affineClampFilter = [CIFilter filterWithName:@"CIAffineClamp"];
    CGAffineTransform xform = CGAffineTransformMakeScale(1.0, 1.0);
    [affineClampFilter setValue:inputImage forKey:kCIInputImageKey];
    [affineClampFilter setValue:[NSValue valueWithBytes:&xform objCType:@encode(CGAffineTransform)] forKey:@"inputTransform"];
    CIImage *extendedImage = [affineClampFilter valueForKey:kCIOutputImageKey];
    CIFilter *blurFilter = [CIFilter filterWithName:@"CIGaussianBlur"];
    [blurFilter setValue:extendedImage forKey:kCIInputImageKey];
    [blurFilter setValue:[NSNumber numberWithFloat:20.0] forKey:@"inputRadius"];
    CIImage *result = [blurFilter valueForKey:kCIOutputImageKey];
    CGImageRef cgImage = [context createCGImage:result fromRect:[inputImage extent]];
    UIImage *returnImage = [UIImage imageWithCGImage:cgImage];
    CGImageRelease(cgImage); // 一定要释放 凡是调用C语言的库实例化的对象 到最后都需要手动进行释放,否则会有内存问题 即便是ARC模式下
    return returnImage;
    }

UIWebview

  • UIWebview打开doc,pdf文件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    UIWebView *webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 55, 320, 300)];
    webView.delegate = self;
    webView.multipleTouchEnabled = YES;
    webView.scalesPageToFit = YES;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *docPath = [documentsDirectory stringByAppendingString:@"/doc2003_1.doc"];
    NSLog(@"#######%@",docPath);
    NSURL *url = [NSURL fileURLWithPath:docPath];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [webView loadRequest:request];
    [self.view addSubview:webView];

调用系统API

  • 拨打电话

    1
    2
    3
    4
    - (IBAction)onClickCallPhone:(id)sender {    
    NSString *url = [NSString stringWithFormat:@"telprompt://%@",self.phoneNumField.text];
    [self openUrl:url];
    }
  • 发送短信

    1
    2
    3
    4
    - (IBAction)onClickSendMsg:(id)sender {  
    NSString *url = [NSString stringWithFormat:@"sms://%@",self.phoneNumField.text];
    [self openUrl:url];
    }
  • 发送邮件

    1
    2
    3
    4
    - (IBAction)onClickEmail:(id)sender {    
    NSString *url = [NSString stringWithFormat:@"mailto://%@",self.phoneNumField.text];
    [self openUrl:url];
    }
  • 浏览网页

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    - (IBAction)onClickBrowser:(id)sender {  
    NSString *url = @"http://www.baidu.com";
    [self openUrl:url];
    }

    -(void)openUrl:(NSString *)urlStr{
    // 注意url中包含协议名称,iOS根据协议确定调用哪个应用,例如发送邮件是“sms://”其中“//”可以省略写成“sms:”(其他协议也是如此)
    NSURL *url = [NSURL URLWithString:urlStr];
    UIApplication *application = [UIApplication sharedApplication];
    if (![application canOpenURL:url]) {
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"无法打开" delegate:self cancelButtonTitle:@"返回" otherButtonTitles:nil];
    [alert show];
    return;
    }
    [[UIApplication sharedApplication]openURL:url];
    }

Foundation

  • NSDate NSString 互转

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    // NSDate -> NSString
    + (NSString *)stringFromDate:(NSDate *)date {
    // [NSDateFormatter new]效率较低,如果调用次数很多,应使用一个静态实例
    NSDateFormatter *formatter = [NSDateFormatter new];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS"];
    NSString *strDate = [formatter stringFromDate:date];
    return strDate;
    }

    // NSString -> NSDate
    + (NSDate *)dateFromString:(NSString *)string {
    NSDateFormatter *formatter = [NSDateFormatter new];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS"];
    NSDate *date = [formatter dateFromString:string];
    return date;
    }
  • 给文字添加下划线

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    /**
    * 给文字添加下划线
    * @param aString 要添加下划线的字符串
    * @return 属性字符串
    */

    -(NSAttributedString *)addUnderLineForLabel:(NSString *)aString
    {
    NSMutableAttributedString *content = [[NSMutableAttributedString alloc] initWithString:aString];
    NSRange contentRange = {0, [content length]};
    [content addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:contentRange];
    [content
    addAttribute:NSUnderlineColorAttributeName value:[UIColor blueColor] range:contentRange];
    return content;
    }

其它

  • 把CGRect结构转变为格式化字符串

    1
    NSStringFromCGRect(someCG);
  • 由字符串恢复出矩形

    1
    CGRectFromString(aString)
  • 读取plist文件并转化为NSDictionary

    1
    2
    3
    NSString *documentsPath = [self getDocumentsDirectory];
    NSString *fullPath = [documentsPath stringByAppendingPathComponent:@"save.plist"];
    NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:fullPath];
  • 播放音效

    1
    2
    3
    4
    5
    6
    NSString *musicFilePath = [[NSBundle mainBundle] pathForResource:fileName ofType:@"wav"];       // 创建音乐文件路径
    NSURL *musicURL = [[NSURL alloc] initFileURLWithPath:musicFilePath];
    AVAudioPlayer* musicPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:musicURL error:nil];
    [musicPlayer prepareToPlay];
    [musicPlayer setVolume:1]; // 设置音量大小
    musicPlayer .numberOfLoops = -1; // 设置音乐播放次数 -1为一直循环

iOS 开发路线

iOS 开发路线