A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© vitanie 中级黑马   /  2015-3-2 18:57  /  1150 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

初学iOS没多久,之前做一个项目总结的代码片段
自己工作学习中总结的常用控件方法,方便写代码。
2.UIButton
<span style="font-size:18px;">#import "UIButton+More.h"

@implementation UIButton (More)

//UIButton的title居左对齐
//btn.contentHorizontalAlignment = 1;
//使文字距离做边框保持10个像素的距离。
//btn.contentEdgeInsets = UIEdgeInsetsMake(0,10, 0, 0);

/**
设置文字左对齐并留出指定缩进
*/
- (void)setTextLeftEdgeInsets:(float)insets
{
    self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    self.contentEdgeInsets = UIEdgeInsetsMake(0, insets, 0, 0);
}

/**
* 1. 无图无字
*/
+ (UIButton *)ButtonWithFrame:(CGRect)rect target:(id)target action:(SEL)action backColor:(UIColor *)color
{
    UIButton *button = [[UIButton alloc] initWithFrame:rect];
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    [button setBackgroundColor:color];
   
    return button;
}

/**
* 2. 背景图
*/
+ (UIButton *)ButtonWithFrame:(CGRect)rect target:(id)target action:(SEL)action backImage:(UIImage *)image
{
    UIButton *button = [[UIButton alloc] initWithFrame:rect];
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    [button setBackgroundImage:image forState:UIControlStateNormal];
   
    [button setBackgroundColor:[UIColor clearColor]];
    button.clipsToBounds = YES;
    return button;
}

/**
* 3. 文字
*/
+ (UIButton *)ButtonWithFrame:(CGRect)rect target:(id)target action:(SEL)action title:(NSString *)title titleColor:(UIColor *)titleColor font:(UIFont *)font backColor:(UIColor *)color
{
    UIButton *button = [[UIButton alloc] initWithFrame:rect];
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:titleColor forState:UIControlStateNormal];
    [button.titleLabel setFont:font];
    [button setBackgroundColor:color];
   
    button.clipsToBounds = YES;
    return button;
}

/**
* 4. 背景图 + 文字
*/
+ (UIButton *)ButtonWithFrame:(CGRect)rect target:(id)target action:(SEL)action backImage:(UIImage *)image title:(NSString *)title titleColor:(UIColor *)titleColor font:(UIFont *)font
{
    UIButton *button = [[UIButton alloc] initWithFrame:rect];
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    [button setBackgroundImage:image forState:UIControlStateNormal];
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:titleColor forState:UIControlStateNormal];
    [button.titleLabel setFont:font];
   
    [button setBackgroundColor:[UIColor clearColor]];
    button.clipsToBounds = YES;
    return button;
}



@end
</span>

3.UIImage
<span style="font-size:18px;">#import "UIImage+More.h"

@implementation UIImage (More)

// 来自相机或相册的相片大小处理
// 1. 等比绽放图片     照相出来时 1936.000000  2592.000000
+ (UIImage *)getScaleImage:(UIImage *)image toSize:(CGSize)size
{
    //压缩图片
    int iWidth  = image.size.width;
    int iHeight = image.size.height;
    if (iWidth > size.width)   // 自定义大小,想要弄多大,就弄多大
    {
        iWidth  = size.width;
        iHeight = image.size.height*iWidth/image.size.width;
        if (iHeight > size.height)
        {
            iHeight = size.height;
            iWidth  = image.size.width*iHeight/image.size.height;
        }
    }
   
    //主要在这里
    UIGraphicsBeginImageContext(CGSizeMake(iWidth, iHeight)); // 创建一个context
   
    // 绘制改变大小的图片
    [image drawInRect:CGRectMake(0, 0, iWidth, iHeight)];
   
    // 从当前context中创建一个改变大小后的图片
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
   
    // 使当前的context出堆栈
    UIGraphicsEndImageContext();
   
    // 返回新的改变大小后的图片
    return scaledImage;
}

// 2. 截取图中某部分小图
+ (UIImage *)getSubImageFromImage:(UIImage *)oldImage withRect:(CGRect)rect
{
    CGImageRef imageRef = oldImage.CGImage;
   
    CGImageRef subImageRef = CGImageCreateWithImageInRect(imageRef, rect);
   
    CGSize size = CGSizeMake(rect.size.width, rect.size.height);
   
    // 创建一个新图像上下文
    UIGraphicsBeginImageContext(size);
   
    CGContextRef context = UIGraphicsGetCurrentContext();
   
    CGContextDrawImage(context, rect, subImageRef);
   
    UIImage* smallImage = [UIImage imageWithCGImage:subImageRef];
   
    UIGraphicsEndImageContext();
   
    return smallImage;
}

// 3. 固定某张图片方向 向上
+ (UIImage *)fixOrientationForImage:(UIImage *)image
{
    // No-op if the orientation is already correct
    if (image.imageOrientation == UIImageOrientationUp) return image;
   
    // We need to calculate the proper transformation to make the image upright.
    // We do it in 2 steps: Rotate if Left/Right/Down, and then flip if Mirrored.
    CGAffineTransform transform = CGAffineTransformIdentity;
   
    //    if(self.imageOrientation == UIImageOrientationDown)
    switch (image.imageOrientation) {
        case UIImageOrientationDown:
        case UIImageOrientationDownMirrored:
            transform = CGAffineTransformTranslate(transform, image.size.width, image.size.height);
            transform = CGAffineTransformRotate(transform, M_PI);
            break;
            
        case UIImageOrientationLeft:
        case UIImageOrientationLeftMirrored:
            transform = CGAffineTransformTranslate(transform, image.size.width, 0);
            transform = CGAffineTransformRotate(transform, M_PI_2);
            break;
            
        case UIImageOrientationRight:
        case UIImageOrientationRightMirrored:
            transform = CGAffineTransformTranslate(transform, 0, image.size.height);
            transform = CGAffineTransformRotate(transform, -M_PI_2);
            break;
        default:
            break;
    }
   
    switch (image.imageOrientation) {
        case UIImageOrientationUpMirrored:
        case UIImageOrientationDownMirrored:
            transform = CGAffineTransformTranslate(transform, image.size.width, 0);
            transform = CGAffineTransformScale(transform, -1, 1);
            break;
            
        case UIImageOrientationLeftMirrored:
        case UIImageOrientationRightMirrored:
            transform = CGAffineTransformTranslate(transform, image.size.height, 0);
            transform = CGAffineTransformScale(transform, -1, 1);
            break;
        default:
            break;
    }
   
    // Now we draw the underlying CGImage into a new context, applying the transform
    // calculated above.
    CGContextRef ctx = CGBitmapContextCreate(NULL, image.size.width, image.size.height,
                                             CGImageGetBitsPerComponent(image.CGImage), 0,
                                             CGImageGetColorSpace(image.CGImage),
                                             CGImageGetBitmapInfo(image.CGImage));
    CGContextConcatCTM(ctx, transform);
    switch (image.imageOrientation) {
        case UIImageOrientationLeft:
        case UIImageOrientationLeftMirrored:
        case UIImageOrientationRight:
        case UIImageOrientationRightMirrored:
            // Grr...
            CGContextDrawImage(ctx, CGRectMake(0,0,image.size.height,image.size.width), image.CGImage);
            break;
            
        default:
            CGContextDrawImage(ctx, CGRectMake(0,0,image.size.width,image.size.height), image.CGImage);
            break;
    }
   
    // And now we just create a new UIImage from the drawing context
    CGImageRef cgimg = CGBitmapContextCreateImage(ctx);
    UIImage *img = [UIImage imageWithCGImage:cgimg];
    CGContextRelease(ctx);
    CGImageRelease(cgimg);
    return img;
}

// 4. 获取自适应大小的图片
+ (UIImage *)getResizableImage:(NSString *)name
{
    UIImage *image = [UIImage imageNamed:name];
    image = [image resizableImageWithCapInsets:UIEdgeInsetsMake(5, 5, 5, 5) resizingMode:UIImageResizingModeStretch];
    return image;
}

@end
</span>

4.UIImageView
<span style="font-size:18px;">#import "UIImageView+More.h"

@implementation UIImageView (More)

/**
实例化 Frame + 图片名
*/
+ (UIImageView *)ImageViewWithFrame:(CGRect)rect image:(UIImage *)image
{
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:rect];
    imageView.image = image;
    return imageView;
}

/**
标准实例化 + 用户交互
*/
+ (UIImageView *)ImageViewWithFrame:(CGRect)rect image:(UIImage *)image userInteraction:(BOOL)is
{
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:rect];
    imageView.image = image;
    imageView.userInteractionEnabled = is;
    return imageView;
}


@end
</span>
5.UIAlertView
<span style="font-size:18px;">#import "AlertView.h"

@implementation AlertView

/**
显示简单弹窗
*/
+ (void)showAlertViewWithMessage:(NSString *)message
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"" message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [alertView show];
}

/**
指定时间后自动消失的提示窗
*/
+ (void)showMessage:(NSString*)message time:(float)time
{
    CGSize size = [self getSizeForText:message font:[UIFont systemFontOfSize:16] withConstrainedSize:CGSizeMake(280, 200)];
   
    if(size.width < 50) {
        size.width = 50;
    }
    if(size.height < 40) {
        size.height = 40;
    }
   

2 个回复

倒序浏览
OC刚开始学,有些还看不懂,UI还没开始
回复 使用道具 举报
现在还看不懂 收藏
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马