The scarecrow's blog

No heart, No lung!

设置Xcode7|iOS9 与服务器交互支支持HTTP

在iOS9中,苹果将原http协议改成了https协议,使用TLS1.2 SSL加密请求数据

解决方法

在info.plist中添加

1
2
3
4
5
6
7
8
9
10
11
12
<plist>

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

</plist> 

NSAttributedString 文本属性详解

NSAttributedString包含的所有Key

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
NSFontAttributeName;                  //字体,value是UIFont对象
NSParagraphStyleAttributeName;        //绘图的风格(居中,换行模式,间距等诸多风格),value是NSParagraphStyle对象
NSForegroundColorAttributeName;       // 文字颜色,value是UIFont对象
NSBackgroundColorAttributeName;       // 背景色,value是UIFont
NSLigatureAttributeName;              //  字符连体,value是NSNumber
NSKernAttributeName;                  // 字符间隔
NSStrikethroughStyleAttributeName;    //删除线,value是NSNumber
NSUnderlineStyleAttributeName;        //下划线,value是NSNumber
NSStrokeColorAttributeName;           //描绘边颜色,value是UIColor
NSStrokeWidthAttributeName;           //描边宽度,value是NSNumber
NSShadowAttributeName;                //阴影,value是NSShadow对象
NSTextEffectAttributeName;            //文字效果,value是NSString
NSAttachmentAttributeName;            //附属,value是NSTextAttachment 对象
NSLinkAttributeName;                  //链接,value是NSURL or NSString
NSBaselineOffsetAttributeName;        //基础偏移量,value是NSNumber对象
NSUnderlineColorAttributeName;        //下划线颜色,value是UIColor对象
NSStrikethroughColorAttributeName;    //删除线颜色,value是UIColor
NSObliquenessAttributeName;           //字体倾斜
NSExpansionAttributeName;             //字体扁平化
NSVerticalGlyphFormAttributeName;     //垂直或者水平,value是 NSNumber,0表示水平,1垂直

第一步创建UILabel及NSMutableAttributedString的实例对象,之后的效果改变都是作用在它们上面:

1
2
3
4
5
6
7
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width - 40, 50)];
label.textAlignment = NSTextAlignmentCenter;
label.center = self.view.center;
[self.view addSubview:label];

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"caiiiac.github.io"];

字体 颜色 背景色

  • NSFontAttributeName
  • NSForegroundColorAttributeName
  • NSBackgroundColorAttributeName

效果

字体 颜色 背景色

代码

1
2
3
4
5
6
NSDictionary * attris = @{NSForegroundColorAttributeName:[UIColor whiteColor],
              NSBackgroundColorAttributeName:[UIColor grayColor],
              NSFontAttributeName:[UIFont boldSystemFontOfSize:30]};

[attributedString setAttributes:attris range:NSMakeRange(0,attributedString.length)];
label.attributedText = attributedString;

下划线

  • NSUnderlineStyleAttributeName
  • NSUnderlineColorAttributeName

效果

下划线

代码

1
2
3
4
5
6
7
NSDictionary * attris = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:30],
              NSForegroundColorAttributeName:[UIColor orangeColor],                          
              NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle),                             
              NSUnderlineColorAttributeName:[UIColor blueColor],};

[attributedString setAttributes:attris range:NSMakeRange(0,attributedString.length)];
label.attributedText = attributedString;

描边

  • NSStrokeColorAttributeName
  • NSStrokeWidthAttributeName

效果

描边

代码

1
2
3
4
5
6
7
NSDictionary * attris = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:30],
              NSForegroundColorAttributeName:[UIColor whiteColor],
              NSStrokeColorAttributeName:[UIColor blueColor],
              NSStrokeWidthAttributeName:@(2)};

[attributedString setAttributes:attris range:NSMakeRange(0,attributedString.length)];
label.attributedText = attributedString;

阴影

  • NSShadowAttributeName

效果

阴影

代码

1
2
3
4
5
6
7
8
9
NSShadow * shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor blueColor];
shadow.shadowBlurRadius = 4.0;
shadow.shadowOffset = CGSizeMake(2.0, 2.0);
NSDictionary * attris = @{NSFontAttributeName:[UIFont systemFontOfSize:30],
              NSShadowAttributeName:shadow};

[attributedString setAttributes:attris range:NSMakeRange(0,attributedString.length)];
label.attributedText = attributedString;

字符间隔

  • NSKernAttributeName

默认间隔

默认间隔

间隔为5

间隔为5

代码

1
2
3
4
5
NSDictionary * attris = @{NSKernAttributeName:@(5),
              NSFontAttributeName:[UIFont systemFontOfSize:30]};

[attributedString setAttributes:attris range:NSMakeRange(0,attributedString.length)];
label.attributedText = attributedString;

字体倾斜

  • NSObliquenessAttributeName

效果

字体倾斜

代码

1
2
3
4
5
NSDictionary * attris = @{NSObliquenessAttributeName:@(0.8),
              NSFontAttributeName:[UIFont systemFontOfSize:30]};

[attributedString setAttributes:attris range:NSMakeRange(0,attributedString.length)];
label.attributedText = attributedString;

字体扁平化

  • NSExpansionAttributeName

效果

字体扁平化

代码

1
2
3
4
5
NSDictionary * attris = @{NSExpansionAttributeName:@(1.0),
              NSFontAttributeName:[UIFont systemFontOfSize:30]};

[attributedString setAttributes:attris range:NSMakeRange(0,attributedString.length)];
label.attributedText = attributedString;

添加图片

  • NSAttachmentAttributeName

效果

添加图片

代码

1
2
3
4
5
6
7
8
NSTextAttachment * attach = [[NSTextAttachment alloc] init];
attach.image = [UIImage imageNamed:@"收藏后"];
attach.bounds = CGRectMake(2, -4, 20, 20);
NSAttributedString * imageStr = [NSAttributedString attributedStringWithAttachment:attach];

[attributedString appendAttributedString:imageStr];

label.attributedText = attributedString;

绘图风格

  • NSMutableParagraphStyle

效果

绘图风格

代码 自定义TextView类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@implementation TextView

- (void)drawRect:(CGRect)rect {
// Drawing code

    NSMutableAttributedString * attributeStr = [[NSMutableAttributedString alloc] initWithString:@"绘图风格(居中,换行模式,间距等诸多风格),value是NSParagraphStyle对象"];
    NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.alignment = NSTextAlignmentRight;
    paragraphStyle.headIndent = 4.0;
    paragraphStyle.lineBreakMode = NSLineBreakByCharWrapping;
    paragraphStyle.lineSpacing = 2.0;
    NSDictionary * attributes = @{NSParagraphStyleAttributeName:paragraphStyle};
    [attributeStr setAttributes:attributes range:NSMakeRange(0, attributeStr.length)];
    [attributeStr drawInRect:self.bounds];

}

@end

ViewController中添加代码

1
2
3
4
TextView *textView = [[TextView alloc] initWithFrame:CGRectMake(0, 0, 100, 80)];
textView.backgroundColor = [UIColor whiteColor];
textView.center = CGPointMake(self.view.center.x, self.view.center.y + 30);
[self.view addSubview:textView];

Demo地址:点此查看

NSCalendar & NSDate​Components

  • NSDate只是一个绝对的时间
  • NSCalendar对世界上现存的常用的历法进行了封装,既提供了不同历法的时间信息,又支持日历的计算
  • NSDateComponents是一个容器,详细包含了年月日时分等信息.将时间表示成适合阅读和使用的方式
  • NSDateComponents可以快速而简单地获取某个时间点对应的年, 月,日,时,分,秒,周等信息.例如:三个月,2年,7天,15分钟,60秒等等
  • NSDateComponents的返回值day, week, weekday, month, year 都是从1开始

当前时间的 年 月 日 时 分

1
2
3
4
NSDate * date = [NSDate date];//当前时间
NSCalendar * calendar = [NSCalendar currentCalendar];//当前用户的calendar
NSDateComponents * components = [calendar components:NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute fromDate:date];
NSLog(@"%ld月%ld日%ld时%ld分" ,(long)components.month,(long)components.day,(long)components.hour,(long)components.minute);

今天是今年的第几周

1
2
3
4
NSCalendar * calendar = [NSCalendar currentCalendar];
NSDate * currentDate = [NSDate date];
NSInteger week = [calendar ordinalityOfUnit:NSCalendarUnitWeekday inUnit:NSCalendarUnitYear forDate:currentDate];
NSLog(@"今天是今年的第%ld周",week);

指定年 月 日 时 分 秒 得到 NSDate

1
2
3
4
5
6
7
8
9
10
11
12
13
14
NSDateComponents * components = [[NSDateComponents alloc] init];
components.year = 2015;
components.month = 8;
components.day = 7;
components.hour = 11;
components.minute = 11;
components.second = 11;
NSCalendar * calendar = [NSCalendar currentCalendar];
NSDate * date = [calendar dateFromComponents:components];

NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyy年MM月dd日hh时mm分ss秒";
NSString * time = [formatter stringFromDate:date];
NSLog(@"%@",time);

7天12小时之后

1
2
3
4
5
6
7
8
9
10
11
12
NSDateComponents * components = [[NSDateComponents alloc] init];

components.day = 7;
components.hour = 12;
NSCalendar * calendar = [NSCalendar currentCalendar];
NSDate * currentDate = [NSDate date];
NSDate * nextData = [calendar dateByAddingComponents:components toDate:currentDate options:NSCalendarMatchStrictly];
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];

formatter.dateFormat = @"yyyy年MM月dd日hh时mm分ss秒";
NSString * after = [formatter stringFromDate:nextData];
NSLog(@"7天12小时之后的时间:%@",after);

这个月有几天

1
2
3
NSCalendar * calendar = [NSCalendar currentCalendar];
NSDate * currentDate = [NSDate date];
NSRange range = [calendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:currentDate];

常用宏汇总

NSString

以@方式生成的字符串,会作为字符串常量,在程序过程中,会一直存在,占用着内存。

1
2
3
#define STR(str) [NSString stringWithCString:(str) encoding:NSUTF8StringEncoding]

#define STR(str)[[NSString alloc] initWithUTF8String:str];

IOS7判断

1
#define IOS7 [[[UIDevice currentDevice]systemVersion] floatValue]>=7.0

屏幕宽高

1
2
#define kScreenWidth [[UIScreen mainScreen] bounds].size.width
#define kScreenHeight [[UIScreen mainScreen] bounds].size.height

颜色

1
2
3
4
5
6
7
8
//RGB
#define RGBA(R, G, B, A) [UIColor colorWithRed:R/255.0f green:G/255.0f blue:B/255.0f alpha:A]

//(16进制->10进制)  
#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]  

//透明色
#define CLEARCOLOR [UIColor clearColor] 

图片

1
2
3
4
5
//读取本地图片 
#define LOADIMAGE(file,type) [UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:file ofType:type]]  

//定义UIImage对象 
#define IMAGE(A) [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:A ofType:nil]]  

weakSelf

1
#define WS(weakSelf)  __weak __typeof(&*self)weakSelf = self;

重写NSLog,Debug模式下打印日志和当前行数

1
2
3
4
5
#if DEBUG  
#define NSLog(FORMAT, ...) fprintf(stderr,"\nfunction:%s line:%d content:%s\n", __FUNCTION__, __LINE__, [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);  
#else  
#define NSLog(FORMAT, ...) nil  
#endif 

判断是真机还是模拟器

1
2
3
4
5
6
7
#if TARGET_OS_IPHONE  
//iPhone Device  
#endif  

#if TARGET_IPHONE_SIMULATOR  
//iPhone Simulator  
#endif  

利用JavaScript改变webView中图片的大小

webView中图片过大

最简单的图文混排就是用webView去实现,如果已经在html源码中添加了不同设备的适配,其中的文字就会自动换行,但是图片如果大过屏幕的宽度,html的约束就会失去效果,这样webView显示出来后图片显示不全,出现了可以左右滑动的情况.解决方式如下:

这种情况一般 webView.scalesPageToFit = YES 也无效

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    //拦截网页图片  并修改图片大小
    [webView stringByEvaluatingJavaScriptFromString:
    @"var script = document.createElement('script');"
    "script.type = 'text/javascript';"
    "script.text = \"function ResizeImages() { "
    "var myimg,oldwidth,oldheight;"
    "var maxwidth=300;" //缩放系数
    "for(var i=0;i <document.images.length;i++){"
    "myimg = document.images[i];"
    "oldwidth = myimg.width;"
    "oldheight = myimg.height;"
    "if(myimg.width > maxwidth){"
    "myimg.style.width = maxwidth+'px';"
    "myimg.style.height = (oldheight * (maxwidth/oldwidth))+'px';"
    "}"
    "}"
    "}\";"
    "document.getElementsByTagName('head')[0].appendChild(script);"];

    [webView stringByEvaluatingJavaScriptFromString:@"ResizeImages();"];

}

禁用webView中点击or长按响应

在webView中长按就会弹出拷贝等功能的弹窗,如果图片或文字添加了超链接还能进行跳转.通常我们看别人的app里的图文控件是不是webView,用这种方式就可以看出来. 如果你不想让别人看出来你这是webView,添加以下代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    ...
    //禁用webView长按弹窗
    [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitUserSelect='none';"];

    [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitTouchCallout='none';"];

}
//该方法的返回值用以控制是否允许加载目标链接页面的内容,返回YES将直接加载内容,NO则反之。
- (BOOL) webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    //用户触击了一个链接
    if (navigationType == UIWebViewNavigationTypeLinkClicked) {
      return NO;
    }

    return YES;
}

UIWebViewNavigationType枚举:

  • UIWebViewNavigationTypeLinkClicked, 用户触击了一个链接。
  • UIWebViewNavigationTypeFormSubmitted, 用户提交了一个表单。
  • UIWebViewNavigationTypeBackForward, 用户触击前进或返回按钮。
  • UIWebViewNavigationTypeReload, 用户触击重新加载的按钮。
  • UIWebViewNavigationTypeFormResubmitted, 用户重复提交表单
  • UIWebViewNavigationTypeOther, 发生其它行为。

webView链接失效的时候出现 404 页面

如果不想在网页加载失败的时候出现404扫兴,就添加代码吧!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
- (BOOL) webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    static BOOL isRequestWeb = YES;

    if (isRequestWeb) {
        NSHTTPURLResponse *response = nil;

        NSData *data = [NSURLConnection sendSynchronousRequest:request   returningResponse:&response error:nil];

        if (response.statusCode == 404) {
            // code for 404
            return NO;
        } else if (response.statusCode == 403) {
            // code for 403
            return NO;
        }

        [webView loadData:data MIMEType:@"text/html" textEncodingName:nil baseURL:[request URL]];

        isRequestWeb = NO;
        return NO;
    }

    return YES;
}

App所需图片尺寸汇总

5s.6.6plus 屏幕尺寸

屏幕大小尺寸,也就是app启动图片的尺寸大小,取下图中的像素分辨率

app所有尺寸

app所有图标大小

图标大小计算方式如iPhone 29pt所示:

  • 1x: 29pt * 1
  • 2X: 29pt * 2
  • 3x: 29pt * 3

另:上架时,还需要一张1024*1024的图标

图标

Octopress写博客命令及安装步骤

Octopress关于写博客的命令

创建新文章

1
$ rake new_post['title']

生成的文件在~/source/_posts目录下

编辑文章

1
2
3
4
5
6
7
8
9
10
$ rake generate     //生成博客

$ git add .
$ git commit -m "comment" 
$ git push origin source

$ rake deploy       //部署


$ rake preview #本地生成浏览 localhost:4000

安装Octopress过程

安装Octopress主要需要两个东西

  • git

  • ruby

git:mac上都默认安装

ruby:mac默认安装.不过按官方要求是需要1.9.3的版本

1.安装Homebrew

1
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

2.安装Rbenv

1
2
3
brew update
brew install rbenv
brew install ruby-build

3.安装低版本ruby

1
2
3
rbenv install 1.9.3-p125
rbenv local 1.9.3-p125
rbenv rehash

使用ruby --version命令查看版本号

开始安装octopress

1
2
3
4
5
6
7
8
9
10
$ git clone git://github.com/imathis/octopress.git octopress
$ cd octopress

**安装依赖**
$ gem install bundler
$ rbenv rehash
$ bundle install

**安装octopress默认主题**
$ rake install

设置github和Octopress的关联

第一步要做的是去github创建一个username.github.io的repo 运行如下命令,按照提示完成关联

1
$ rake setup_github_pages

如果github没有添加SSH keys

首先创建SSH keys,终端敲入:ssh-keygen,根据系统提示进行设置.

生成的SSH keys保存在: ~/.ssh拷贝生成的文件内容,将其添加至github帐号管理的SSH key中,就可以克隆github上的代码库了.

多台电脑同步写作

首先把工程clone下来,切换到source分支 另外需要把master clone_deploy目录中

1
2
3
4
git clone https://github.com/caiiiac/caiiiac.github.io.git
cd caiiiac.github.io
git checkout source
git clone https://github.com/caiiiac/caiiiac.github.io.git _deploy

使用高德地图SDK过程中的各种坑

1. APP内调起高德地图导航

高德文档描写如下:

1
2
3
4
5
6
7
8
9
10
11
//配置导航参数
MANaviConfig * config = [[MANaviConfig alloc] init];
config.destination = view.annotation.coordinate;//终点坐标,Annotation的坐标
config.appScheme = [self getApplicationScheme];//返回的Scheme,需手动设置
config.appName = [self getApplicationName];//应用名称,需手动设置
config.style = MADrivingStrategyShortest;
//若未调起高德地图App,引导用户获取最新版本的
if(![MANavigation openAMapNavigation:config])
{
[MANavigation getLatestAMapApp];
}

实际情况

  • 2.5.0 SDK使用此方法调用导航都无任何反应 [MANavigation openAMapNavigation:config] 返回为成功
  • 2.6.0 SDK查无此类

解决方法

  • 更新SDK为2.6.0 pod 'AMap2DMap', '~> 2.6.0'

代码修改如下:

1
2
3
4
5
6
7
8
9
10
11
12
//配置导航参数
MANaviConfig * config = [[MANaviConfig alloc] init];
config.destination = _parkingCoordinate;         //终点坐标,Annotation的坐标    
config.appScheme = [self getApplicationScheme];  //返回的Scheme,需手动设置
config.appName = [self getApplicationName];      //应用名称,需手动设置
config.strategy = MADrivingStrategyShortest;

//若未调起高德地图App,引导用户获取最新版本的
if(![MAMapURLSearch openAMapNavigation:config])
{
[MAMapURLSearch getLatestAMapApp];
}

2. - (void)setZoomLevel:(double)newZoomLevel animated:(BOOL)animated; 失效

使用情况

  • 2.5.0 SDK使用此方法设置一次,永久有效
  • 2.6.0 SDK使用此方法设置无效,需要延时设置

解决方法,代码如下

1
2
3
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self.mapView setZoomLevel:14.f animated:YES];
});