您的位置:首页 > 其它

小知识整理

2016-01-19 12:58 447 查看
工作中遇到的一些小知识,不定时更新,以免忘记,方便以后要用到时查找

1.获取webView中的scrollView

UIScrollView *tempView = (UIScrollView *)[_WebView.subviewsobjectAtIndex:0];


2.iOS9访问http协议的URL需要修改

在Info.plist 配置中添加NSAppTransportSecurity—>NSAllowsArbitraryLoads—>YES

在 plist 文件里显示如下:



3.设置导航条、状态栏属性(全局)

// 设置状态栏字体颜色
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
// 设置导航栏的主题背景颜色
[[UINavigationBar appearance] setBarTintColor:[UIColor greenColor]];
// 设置导航栏item颜色
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
// 设置导航栏标题颜色及字体
[[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor], NSFontAttributeName:
[UIFont boldSystemFontOfSize:18]}];


4.Label文字自适应

NSString *text = @"your text";
CGSize textSize = [text boundingRectWithSize:CGSizeMake(self.view.frame.size.width - 20, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]} context:nil].size;
UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(10,100, textSize.width, textSize.height)];
textLabel.numberOfLines = 0;
CGSize size = [label.text sizeWithAttributes:@{NSFontAttributeName:label.font}];


5.页面跳转时隐藏显示底部tabbar

self.hidesBottomBarWhenPushed = YES;   // 设置push下一页面tabbar隐藏
[self.navigationController pushViewController:viewController animated:YES];
self.hidesBottomBarWhenPushed = NO;    // 设置pop回这一页面tabbar不隐藏


6.判断是否是邮箱号和手机号

+ (BOOL)validateEmail:(NSString *)email
{
NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailPred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
return [emailPred evaluateWithObject:email];
}

+ (BOOL)validateTelephone:(NSString *)phone
{
NSString *phoneRegex = @"^((13[0-9])|(147)|(15[^4,\\D])|(18[0-9]))\\d{8}$";
NSPredicate *phonePred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", phoneRegex];
return [phonePred evaluateWithObject:phone];
}


7.设置Label文本前后不同属性(字体大小、颜色等等)富文本

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20,100,300,100)];
label.textColor = [UIColor blackColor];
label.font = [UIFont systemFontOfSize:17];
NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc] initWithString:@"字符串字体颜色和大小等等"];
// 修改指定范围字体单个属性
[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range: NSMakeRange(0, 3)];
// 修改指定范围字体多个属性
[attributedString addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14],NSForegroundColorAttributeName:[UIColor redColor]} range:NSMakeRange(3, 5)];
// 字体后添加自定义图片
NSTextAttachment *attch = [[NSTextAttachment alloc] init];
attch.image = [UIImage imageNamed:@"imageName"];
attch.bounds = CGRectMake(0, 0, 20, 20);
NSAttributedString *str = [NSAttributedString attributedStringWithAttachment:attch];
[attributedString appendAttributedString:str];
label.attributedText = attributedString;
[self.view addSubview:label];
//    NSFontAttributeName
//    字体
//    NSParagraphStyleAttributeName
//    段落格式
//    NSForegroundColorAttributeName
//    字体颜色
//    NSBackgroundColorAttributeName
//    背景颜色
//    NSStrikethroughStyleAttributeName
//    删除线格式
//    NSUnderlineStyleAttributeName
//    下划线格式
//    NSStrokeColorAttributeName
//    删除线颜色
//    NSStrokeWidthAttributeName
//    删除线宽度
//    NSShadowAttributeName
//    阴影


8.URL地址需要编码,当出现非ASCCI码,需要对其他字符集做编码处

NSString * url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];


9.nib和xib的区别

nib文件是xib文件编译后的二进制文件


10.HTML/CSS/JavaScript网页三剑客

HTML:显示,类似iOS中的UI部分
CSS(层叠样式表):定义样式,类似iOS中的属性值color,font
JavaScript:脚本语言,定义交互,类似iOS中的action-target事件响应


11.生成指定范围的随机整数、随机颜色

/*
@brief 生成指定范围的随机整数
@param min 下限(闭区间)
@param max 上限(闭区间)
@return 指定范围的随机数
*/
+ (int)getRandomIntBetweenMin:(int)min andMax:(int)max
{
return arc4random() % (max - min + 1) + min;
}
/*
@brief 生成随机颜色
@return UIColor对象的指针
*/
+ (UIColor *)getRandomColor
{
double red = [self getRandomIntBetweenMin:0andMax:255] /255.0;
<
ae2f
span class="hljs-keyword">double green = [self getRandomIntBetweenMin:0andMax:255] /255.0;
double blue = [self getRandomIntBetweenMin:0andMax:255] /255.0;
return [UIColor colorWithRed:red green:green blue:blue alpha:1];
}


12.改变图片尺寸

/*
@brief 改变图片尺寸
@param image 想要改变的图片
@param size 想要的图片尺寸
@return 改变尺寸后的图片
*/
+ (UIImage *)imageWithImage:(UIImage *)image convertToSize:(CGSize)size
{
UIGraphicsBeginImageContext(size);
[image drawInRect:CGRectMake(0, 0, size.width, size.height)];
UIImage *destImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return destImage;
}


13.在ARC代码中混编非ARC代码

ios中如果arc和非arc文件混编,可以在build parses中展开Compile Sources,找到你你想要混编的文件名,如果是arc文件则双击添加-fobjc-arc,非arc文件则双击添加-fno-objc-arc

将整个项目改成非arc机制的方法:在build Settings中:把Objective-C Automatic Reference Counting设为NO
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: