iOS常用代码

//键盘透明
    [UITextField alloc].keyboardAppearance = UIKeyboardAppearanceAlert; 

//状态栏的网络活动风火轮是否旋转,默认为NO
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; 

//开启右滑返回
    self.navigationController.interactivePopGestureRecognizer.delegate = nil; 

//修改PlaceHolder的默认颜色
    [[UITextField alloc] setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];

//  导航栏返回按钮颜色设置 及 文字变成<
-(void)UIBarButtonBackItemSet{
    [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60)
                                                         forBarMetrics:UIBarMetricsDefault];
    
    self.navigationController.navigationBar.barStyle = UIStatusBarStyleDefault;
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
}

//TODO:,中英文混合时,英文会自动换行,为了禁止此行为,由于下面的设置无效,暂时处理方法:
//self.textContainer.lineBreakMode=NSLineBreakByCharWrapping;//无效果
    NSMutableParagraphStyle *pa=[[NSMutableParagraphStyle alloc] init];
    pa.lineBreakMode=NSLineBreakByCharWrapping;
    self.attributedText=[[NSAttributedString alloc] initWithString:@" " attributes:@{NSParagraphStyleAttributeName:pa}];
    self.text=nil; 

//修改navgation的标题颜色大小
    [self.navigationController.navigationBar setTitleTextAttributes:
                                              @{NSFontAttributeName:[UIFont systemFontOfSize:17.0f],
                                     NSForegroundColorAttributeName:RainColor(51, 51, 51)}];//title字体颜色和大小
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:NO]; 

//修改nav带状态栏的背景图 
    NSString *title_bgName = @"recharge_nav.png";
    UIImage *title_bg = [UIImage imageNamed:title_bgName];  //获取图片
    CGSize titleSize = CGSizeMake(self.navigationController.navigationBar.bounds.size.width, self.navigationController.navigationBar.bounds.size.height + 20);  //获取Navigation Bar的位置和大小
    title_bg = [self scaleToSize:title_bg size:titleSize];//设置图片的大小与Navigation Bar相同
    [self.navigationController.navigationBar setBackgroundImage:title_bg forBarMetrics:UIBarMetricsDefault];
    //消除细线
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
//调整图片大小
- (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size{
    UIGraphicsBeginImageContext(size);
    [img drawInRect:CGRectMake(0, 0, size.width, size.height)];
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return scaledImage;
} 
 //MD5加密
@interface NSString (MD5)

- (NSString *)stringToMD5:(NSString *)str;

@end 

#import "NSString+MD5.h"
#import 
/*
记得要导入这个框架和它的头文件
*/
@implementation NSString (MD5)

- (NSString *)stringToMD5:(NSString *)str
{

//1.首先将字符串转换成UTF-8编码, 因为MD5加密是基于C语言的,所以要先把字符串转化成C语言的字符串
    const char *fooData = [str UTF8String];

//2.然后创建一个字符串数组,接收MD5的值
    unsigned char result[CC_MD5_DIGEST_LENGTH];

//3.计算MD5的值, 这是官方封装好的加密方法:把我们输入的字符串转换成16进制的32位数,然后存储到result中
    CC_MD5(fooData, (CC_LONG)strlen(fooData), result);
    /**
    第一个参数:要加密的字符串
    第二个参数: 获取要加密字符串的长度
    第三个参数: 接收结果的数组
    */

//4.创建一个字符串保存加密结果
    NSMutableString *saveResult = [NSMutableString string];

//5.从result 数组中获取加密结果并放到 saveResult中
    for (int i = 0; i < CC_MD5_DIGEST_LENGTH; i++) {
        [saveResult appendFormat:@"%02x", result];
    }
    /*
       x表示十六进制,%02X  意思是不足两位将用0补齐,如果多余两位则不影响
        NSLog("%02X", 0x888);  //888
        NSLog("%02X", 0x4); //04
     */
    return saveResult;
}  

你可能感兴趣的:(iOS常用代码)