iOS--代码片段

来源:互联网 发布:写jquery必须会js么 编辑:程序博客网 时间:2024/06/05 15:02

1. 获取字符串的宽度

CGSize size = [@str sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]}];

2. 根据键盘高度改变输入框的位置

监听键盘frame改变(UIKeyboardWillChangeFrameNotification)

[[NSNotificationCenter defaultCenter] addObserver:self                                             selector:@selector(keyboardWillChangeFrameNotification:)                                                 name:UIKeyboardWillChangeFrameNotification                                               object:nil];

实现监听函数

-(void)keyboardFrameDidChange:(NSNotification*)notice  {      NSDictionary * userInfo = notice.userInfo;      NSValue * endFrameValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];      CGRect endFrame = endFrameValue.CGRectValue;      NSLog(@"%@",NSStringFromCGRect(endFrame)); }

3. 设置URL Schema启动自身App

在info.plist中进行设置
新增URL types,新增URL Schema,新增URL identifier,效果如图:
这里写图片描述

4. 16进制转换成10进制

把颜色值a3b8e5转换成对应RGB的十进制

int red = strtoul([[@“a3b8e5” substringWithRange:NSMakeRange(0, 2)] UTF8String],0,16);int green = strtoul([[@“a3b8e5” substringWithRange:NSMakeRange(2, 2)] UTF8String],0,16);int blue = strtoul([[@“a3b8e5” substringWithRange:NSMakeRange(4, 2)] UTF8String],0,16);

5. iOS9使用http 请求

一共两种方法
方法一:全局使用http 请求
修改info.plist文件

<key>NSAppTransportSecurity</key>    <dict>        <key>NSAllowsArbitraryLoads</key>        <true/>    </dict>

方法二:局部使用http 请求,过滤自己认为需要的http 请求
修改info.plist文件,以允许qq.com和sina.com.cn及其子域名http请求为例

<key>NSAppTransportSecurity</key>    <dict>        <key>NSExceptionDomains</key>        <dict>            <key>qq.com</key>            <dict>                <key>NSIncludesSubdomains</key>                <true/>            </dict>            <key>sina.com.cn</key>            <dict>                <key>NSIncludesSubdomains</key>                <true/>            </dict>        </dict>   </dict>
0 0