iOS常用小方法

1.UITextView垂直居中显示文字,先设置好布局。因为UITextView集成自UIScrollView,所以设置偏移量就可以了

//textView的contentSize属性
    CGSize contentSize = textView.contentSize;
    //如果文字内容高度没有超过textView的高度
    if(contentSize.height <= textView.frame.size.height) {
        //textView的高度减去文字高度除以2就是Y方向的偏移量,也就是textView的上内边距
        CGFloat offsetY = (textView.frame.size.height - contentSize.height)/2;
        UIEdgeInsets offset = UIEdgeInsetsMake(offsetY, 0, 0, 0);
        [textView setContentInset:offset];
    }

2.判断是否是网页的正则

NSString *reg = @"((http[s]{0,1}|ftp)://[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)|(www.[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)";
    NSPredicate *urlPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", reg];
    return [urlPredicate evaluateWithObject:url];

3.判断字符串是不是NULL

+(BOOL)IsNotEmpty:(id)string
{
    BOOL result = NO;
    if (![string isKindOfClass:[NSNull class]])
    {
        if (string != nil)
        {
            result = YES;
        }
    }
    return result;
}

4.时间戳转时间

+(NSString *)timeConversionWithTimeStamp:(NSString *)timeStamp
{
    NSString *latestMessageTime = @"";
    double timeInterval = [timeStamp doubleValue];
    if(timeInterval > 140000000000) {
        timeInterval = timeInterval / 1000;
    }
    NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"YYYY-MM-dd hh:mm:ss"];
    latestMessageTime = [formatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:timeInterval]];
 }

5.截图

/**
 *  截图
 *
 *  @param view 需要截取的视图
 *
 *  @return 截图 图片
 */
+ (UIImage *)screenShotFormView:(UIView *)view
{
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, YES, [UIScreen mainScreen].scale);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;
}

6.图片设置圆角

/**
 *  图片设置圆角
 *
 *  @param cornerRadius 圆角值
 *  @param image     图片
 *
 *  @return 圆角图片
 */
- (UIImage *)imageWithCornerRadius:(CGFloat)cornerRadius image:(UIImage *)image
{
    CGRect frame = CGRectMake(0, 0, image.size.width, image.size.height);
    UIGraphicsBeginImageContextWithOptions(image.size, NO, 1.0);
    [[UIBezierPath bezierPathWithRoundedRect:frame
                                cornerRadius:cornerRadius] addClip];
    // 画图
    [image drawInRect:frame];
    // 获取新的图片
    UIImage *im = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return im;
}

你可能感兴趣的:(iOS常用小方法)