开发随记

记录工作、生活


  • Home

  • Archives

iOS 开发笔记(一)

Posted on 2017-03-25 | Edited on 2018-06-26 | In iOS |

计算字符串 size

在 iOS7 中下面计算字符串 size的方法都标注为 deprecated

1
2
3
4
5
- (CGSize)sizeWithFont:(UIFont *)font
constrainedToSize:(CGSize)size
lineBreakMode:(NSLineBreakMode)lineBreakMode

- (CGSize)sizeWithFont:(UIFont *)font;

iOS7以后提供了新的计算方法

1
2
3
4
5
6
CGRect textRect = [text boundingRectWithSize:size
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{NSFontAttributeName:FONT}
context:nil];

CGSize size = textRect.size;

参考链接

当在.h文件用readonly修饰的时候,调用属性的setter方法会报错

1
@property (readonly, nonatomic, copy) NSString* eventDomain;
1
2
// 编译器会报错
[self setEventDomain:@"someString"];

在class extension中重新声明,告诉比编译器你需要setter方法:

1
2
3
4
5
@interface YourClass ()

@property (nonatomic, copy) NSString* eventDomain;

@end

参考链接

微博中的‘@用户名’和‘#话题#’这个正则表达式怎么写

匹配 @用户名

首先分析下微博中从哪里开始到哪里结束才是一个完整的用户名,按照常规的表现形式,一般是以@开头,以:结尾,中间为用户的名称。 匹配表达式就可写为: @[^::]+ 这是简单的写法,但是有些是在微博之后再@的,还有就是连续@的情况,还有些是以逗号等结束的,因此完善一下 修改为: @[^,,::\s@]+ 但是这些匹配都是从形式上进行了一个大概的归类,但是为了更为严谨,就要彻底分析其用户名的具体格式,例如新浪微博中的用户名格式为是“4-30个字符,支持英文、数字、”“或减号” ,也就是说,支持中文、字母、数字、下划线及减号,并且是4到30个字符(这里暂且认为汉字为一个字符)那么在写匹配的表达式的时候就可以这么来写:
`@[\u4e00-\u9fa5a-zA-Z0-9
-]{4,30}`

匹配 #话题

这个相对相对就简单了很多,前后都是#,以#号开始并以#结束 匹配表达式写为: #[^#]+#

参考链接

YYKit 中匹配 @用户名 和 话题

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
+ (NSRegularExpression *)regexAt {
static NSRegularExpression *regex;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
// 微博的 At 只允许 英文数字下划线连字符,和 unicode 4E00~9FA5 范围内的中文字符,这里保持和微博一致。。
// 目前中文字符范围比这个大
regex = [NSRegularExpression regularExpressionWithPattern:@"@[-_a-zA-Z0-9\u4E00-\u9FA5]+" options:kNilOptions error:NULL];
});
return regex;
}

+ (NSRegularExpression *)regexTopic {
static NSRegularExpression *regex;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
regex = [NSRegularExpression regularExpressionWithPattern:@"#[^@#]+?#" options:kNilOptions error:NULL];
});
return regex;
}

参考链接

iOS7中 controller 的 view上移64问题

1
2
// 在 controller 中调用
self.edgesForExtendedLayout = UIRectEdgeNone;

iOS7 以上修改 navigation bar背景色

改变某一个 navigation controller(iOS7以上有效)

1
2
3
// tips : 在 viewDidLoad 方法中条用
self.navigationController.navigationBar.barTintColor = [UIColor customColor];
self.navigationController.navigationBar.translucent = NO;

更改整个 app 中的 navigation bar(iOS7以上有效)

1
2
3
4
5
6
7
// 在 appdelegate 中调用
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
[[UINavigationBar appearance] setBarTintColor:[UIColor whiteColor]];
[[UINavigationBar appearance] setTranslucent:NO];
return YES;
}

参考链接:How to change Navigation Bar color in iOS 7?

iOS获取当前版本号

1
2
3
4
//当前版本号,对应bundle,一般为数字:123
[[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString*)kCFBundleVersionKey];
//版本号,对应version,一般为字符串:1.0.2
[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];

自定义 UIBarButtonItem

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

// 导航栏左边的按钮
self.leftButton = [PPQButton ppq_buttonWithType:PPQButtonNavBarBackImageWhite];
[self.leftButton addTarget:self action:@selector(leftBtnClicked) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.leftButton];

// 导航栏 title view
self.titleView = [[UILabel alloc] init];
self.titleView.text = self.title;
self.titleView.font = [UIFont boldSystemFontOfSize:16];
[self.titleView sizeToFit];
self.navigationItem.titleView = self.titleView;

// 导航栏右侧
self.rightButton = [PPQButton ppq_buttonWithType:PPQButtonNavBarRightImageWhite];
[self.rightButton addTarget:self action:@selector(rightButtonClicked) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.rightButton];

隐藏掉 底部的黑线

1
[self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];

讲导航栏背景透明

1
[self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];

窝草,怎么导航栏没变透明??黑人问号

1
2
3

self.navigationController.navigationBar.translucent = YES;// 让导航栏具有穿透效果
[self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];// 这样导航栏就变透明了

如何判断颜色相等?

1
2
//其他类型对象也可以判断哦
[myColor isEqual:someOtherColor]

新浪微博个人实现

1
2
3
4
//手势传递问题
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return [gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]] && [otherGestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]];
}

根据url获取图片的相关信息

1
2
3
4
5
6
7
8
#import <ImageIO/ImageIO.h>

NSMutableString *imageURL = [NSMutableString stringWithFormat:@"http://www.myimageurl.com/image.png"];

CGImageSourceRef source = CGImageSourceCreateWithURL((CFURLRef)[NSURL URLWithString:imageURL], NULL);
NSDictionary* imageHeader = (__bridge NSDictionary*) CGImageSourceCopyPropertiesAtIndex(source, 0, NULL);
NSLog(@"Image header %@",imageHeader);
NSLog(@"PixelHeight %@",[imageHeader objectForKey:@"PixelHeight"]);

模板文件

Posted on 2016-07-03 | Edited on 2018-07-07 | In iOS |

这是一个模板文件

Read more »

README

Posted on 2014-06-07 | Edited on 2018-06-07 | In README |

Blog

项目介绍

{以下是码云平台说明,您可以替换为您的项目简介
码云是开源中国推出的基于 Git 的代码托管平台(同时支持 SVN)。专为开发者提供稳定、高效、安全的云端软件开发协作平台
无论是个人、团队、或是企业,都能够用码云实现代码托管、项目管理、协作开发。企业项目请看 https://gitee.com/enterprises}

软件架构

软件架构说明

安装教程

  1. xxxx
  2. xxxx
  3. xxxx

使用说明

  1. xxxx
  2. xxxx
  3. xxxx

参与贡献

  1. Fork 本项目
  2. 新建 Feat_xxx 分支
  3. 提交代码
  4. 新建 Pull Request

码云特技

  1. 使用 Readme_XXX.md 来支持不同的语言,例如 Readme_en.md, Readme_zh.md
  2. 码云官方博客 blog.gitee.com
  3. 你可以 https://gitee.com/explore 这个地址来了解码云上的优秀开源项目
  4. GVP 全称是码云最有价值开源项目,是码云综合评定出的优秀开源项目
  5. 码云官方提供的使用手册 http://git.mydoc.io/
  6. 码云封面人物是一档用来展示码云会员风采的栏目 https://gitee.com/gitee-stars/
123

Tong

23 posts
7 categories
17 tags
RSS
© 2018 Tong
Powered by Hexo v3.7.1
|
Theme — NexT.Muse v6.2.0