IOS开发基础知识--碎片46
1:带中文的URL处理
// http://static.tripbe.com/videofiles/视频/我的自拍视频.mp4
NSString *path = (__bridge_transfer NSString *)CFURLCreateStringByReplacingPercentEscapesUsingEncoding(NULL,(__bridge CFStringRef)model.mp4_url, CFSTR(""),CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding));
2:取WebView高度
- (void)webViewDidFinishLoad:(UIWebView *)webView {
CGFloat height = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"] floatValue];
CGRect frame = webView.frame;
webView.frame = CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, height);
}
另外一种方式利用KVO实例:
-(void)viewDidLoad{
// KVO,作为一个观察者,只要属性"contentSize"发生变化,回调方法里面就会通知
[_webView.scrollView addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionNew context:NULL];
}
// 回调方法
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if(object == _webView.scrollView && [keyPath isEqualToString:@"contentSize"])
{
// 得到最大的Y坐标
CGSize size = _webView.scrollView.contentSize;
if (size.height > 568.0) {
// 遮挡广告
_hideBottomImage = [[UIImageView alloc] initWithFrame:CGRectMake(, size.height-, ScreenWidth, )];
_hideBottomImage.image = [UIImage imageNamed:@"banner"];
[_webView.scrollView addSubview:_hideBottomImage];
}
}
else
{
// 调用父类的方法
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
- (void)dealloc{//---->在ARC环境下也能调用dealloc方法,只是不需要写[super dealloc]
// 移除KVO,否则会引起资源泄露
[_webView.scrollView removeObserver:self forKeyPath:@"contentSize"];
}
3:UIView的部分圆角问题
UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(, , , )];
view2.backgroundColor = [UIColor redColor];
[self.view addSubview:view2];
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:view2.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(, )];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = view2.bounds;
maskLayer.path = maskPath.CGPath;
view2.layer.mask = maskLayer;
//其中,byRoundingCorners:UIRectCornerBottomLeft |UIRectCornerBottomRight
//指定了需要成为圆角的角。该参数是UIRectCorner类型的,可选的值有:
* UIRectCornerTopLeft
* UIRectCornerTopRight
* UIRectCornerBottomLeft
* UIRectCornerBottomRight
* UIRectCornerAllCorners
4:强制App直接退出
- (void)exitApplication {
AppDelegate *app = [UIApplication sharedApplication].delegate;
UIWindow *window = app.window;
[UIView animateWithDuration:1.0f animations:^{
window.alpha = ;
} completion:^(BOOL finished) {
exit();
}];
}
5:修改占位符颜色和大小
textField.placeholder = @"请输入用户名";
[textField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];
[textField setValue:[UIFont boldSystemFontOfSize:] forKeyPath:@"_placeholderLabel.font"];
6:取消系统的返回手势
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
7:改WebView字体/颜色
UIWebView设置字体大小,颜色,字体: UIWebView无法通过自身的属性设置字体的一些属性,只能通过html代码进行设置 在webView加载完毕后:
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSString *str = @"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '60%'";
[webView stringByEvaluatingJavaScriptFromString:str];
}
或者加入以下代码
NSString *jsString = [[NSString alloc] initWithFormat:@"document.body.style.fontSize=%f;document.body.style.color=%@",fontSize,fontColor];
[webView stringByEvaluatingJavaScriptFromString:jsString];
8:WebView图片自适应屏幕
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSString *js = @"function imgAutoFit() { \
var imgs = document.getElementsByTagName('img'); \
for (var i = 0; i < imgs.length; ++i) {\
var img = imgs[i]; \
img.style.maxWidth = %f; \
} \
}";
js = [NSString stringWithFormat:js, [UIScreen mainScreen].bounds.size.width - ];
[webView stringByEvaluatingJavaScriptFromString:js];
[webView stringByEvaluatingJavaScriptFromString:@"imgAutoFit()"];
}
9:BOOL / bool / Boolean / NSCFBoolean的区别

10:nil / Nil / NULL / NSNull区别

a、nil:一般赋值给空对象;
b、NULL:一般赋值给nil之外的其他空值。如SEL等;
举个栗子(好重啊~):
[NSApp beginSheet:sheet
modalForWindow:mainWindow
modalDelegate:nil //pointing to an object
didEndSelector:NULL //pointing to a non object/class
contextInfo:NULL]; //pointing to a non object/class
c、NSNULL:NSNull只有一个方法:+ (NSNull *) null;
[NSNull null]用来在NSArray和NSDictionary中加入非nil(表示列表结束)的空值.
NSMutableDictionary *mutableDictionary = [NSMutableDictionary dictionary];
mutableDictionary[@"someKey"] = [NSNull null]; // Sets value of NSNull singleton for `someKey`
NSLog(@"Keys: %@", [mutableDictionary allKeys]); // @[@"someKey"]
d、当向nil发送消息时,返回NO,不会有异常,程序将继续执行下去;
而向NSNull的对象发送消息时会收到异常。
11:子类中实现 -isEqual: 和 hash
@interface Person
@property NSString *name;
@property NSDate *birthday; - (BOOL)isEqualToPerson:(Person *)person;
@end @implementation Person - (BOOL)isEqualToPerson:(Person *)person {
if (!person) {
return NO;
} BOOL haveEqualNames = (!self.name && !person.name) || [self.name isEqualToString:person.name];
BOOL haveEqualBirthdays = (!self.birthday && !person.birthday) || [self.birthday isEqualToDate:person.birthday]; return haveEqualNames && haveEqualBirthdays;
} #pragma mark - NSObject - (BOOL)isEqual:(id)object {
if (self == object) {
return YES;
} if (![object isKindOfClass:[Person class]]) {
return NO;
} return [self isEqualToPerson:(Person *)object];
} - (NSUInteger)hash {
return [self.name hash] ^ [self.birthday hash];
}
IOS开发基础知识--碎片46的更多相关文章
- IOS开发基础知识碎片-导航
1:IOS开发基础知识--碎片1 a:NSString与NSInteger的互换 b:Objective-c中集合里面不能存放基础类型,比如int string float等,只能把它们转化成对象才可 ...
- IOS开发基础知识--碎片33
1:AFNetworking状态栏网络请求效果 直接在AppDelegate里面didFinishLaunchingWithOptions进行设置 [[AFNetworkActivityIndicat ...
- IOS开发基础知识--碎片42
1:报thread 1:exc_bad_access(code=1,address=0x70********) 闪退 这种错误通常是内存管理的问题,一般是访问了已经释放的对象导致的,可以开启僵尸对象( ...
- IOS开发基础知识--碎片50
1:Masonry 2个或2个以上的控件等间隔排序 /** * 多个控件固定间隔的等间隔排列,变化的是控件的长度或者宽度值 * * @param axisType 轴线方向 * @param fi ...
- IOS开发基础知识--碎片3
十二:判断设备 //设备名称 return [UIDevice currentDevice].name; //设备型号,只可得到是何设备,无法得到是第几代设备 return [UIDevice cur ...
- IOS开发基础知识--碎片11
1:AFNetwork判断网络状态 #import “AFNetworkActivityIndicatorManager.h" - (BOOL)application:(UIApplicat ...
- IOS开发基础知识--碎片14
1:ZIP文件压缩跟解压,使用ZipArchive 创建/添加一个zip包 ZipArchive* zipFile = [[ZipArchive alloc] init]; //次数得zipfilen ...
- IOS开发基础知识--碎片16
1:Objective-C语法之动态类型(isKindOfClass, isMemberOfClass,id) 对象在运行时获取其类型的能力称为内省.内省可以有多种方法实现. 判断对象类型 -(BOO ...
- IOS开发基础知识--碎片19
1:键盘事件顺序 UIKeyboardWillShowNotification // 键盘显示之前 UIKeyboardDidShowNotification // 键盘显示完成后 UIKeyboar ...
随机推荐
- 深入理解DOM节点类型第三篇——注释节点和文档类型节点
× 目录 [1]注释节点 [2]文档类型 前面的话 把注释节点和文档类型节点放在一起是因为IE8-浏览器的一个bug.IE8-浏览器将标签名为"!"的元素视作注释节点,所以文档声明 ...
- 【原创】开源Math.NET基础数学类库使用(05)C#解析Delimited Formats数据格式
本博客所有文章分类的总目录:[总目录]本博客博文总目录-实时更新 开源Math.NET基础数学类库使用总目录:[目录]开源Math.NET基础数学类库使用总目录 前言 ...
- 应用程序框架实战三十:表现层及ASP.NET MVC介绍(一)
本文将介绍表现层及ASP.NET MVC的一些要点,特别是ASP.NET MVC的一些抽象和封装技巧,如果你对MVC还不了解,可以参考<ASP.NET MVC4 高级编程>,作者Jon G ...
- Windws Server 2008 R2 WEB环境配置之MYSQL 5.6.22安装配置
版本选择 因为MySql的版本越来越多,而作为中小网站者可能没有足够的经济去购买商业版本,所以一般选择免费版,而且功能也是足够使用的. 有钱任性就下载企业版,哈哈. 目前使用最多的版本是mysql i ...
- EXP/IMP迁移案例,IMP遭遇导入表的表空间归属问题
生产环境: 源数据库:Windows Server + Oracle 11.2.0.1 目标数据库:SunOS + Oracle 11.2.0.3 1.确认迁移需求:源数据库cssf 用户所有表和数据 ...
- [linux]如何为Virtualbox虚拟硬盘扩容(转载)
前言 这个教程介绍如何为Virtualbox虚拟硬盘扩容,虚拟硬盘分为动态分配大小和固定虚拟硬盘,扩容的方法不一样: 如何为动态分配的Virtualbox虚拟硬盘扩容 如何为固定大小的Virtualb ...
- 浅谈Hibernate入门
前言 最近打算做一个自己的个人网站,经过仔细思考,打算使用hibernate作为开发的ORM框架,因此各种找资料,由于本人是刚刚接触这技术的,所以就找了比较基础的知识来分享下 基本概述 Hiberna ...
- 从零开始学 Java - 我放弃了 .NET ?
这不是一篇引起战争的文章 毫无疑问,我之前是一名在微软温暖怀抱下干了近三年的 .NET 开发者,为什么要牛(sha)X一样去搞 Java 呢?因为我喜欢 iOS 阿!哈哈,开个玩笑.其实,开始学 Ja ...
- C++控制台贪吃蛇代码
游戏截图: 以下是3个代码文件: Snake_Class.h文件: #ifndef SNAKE #define SNAKE #include<windows.h> #include< ...
- 基础笔记(三):网络协议之Tcp、Http
目录 一.网络协议 二.TCP(Transmission Control Protocol,传输控制协议) TCP头格式 TCP协议中的三次握手和四次挥手 TCP报文抓取工具 三.HTTP(Hyper ...