您的位置:首页 > 移动开发 > IOS开发

iOS开发经验总结

2014-08-18 17:27 495 查看
1. TapGesture使用要注意,当对某一个view使用tapGesture之后,发到这个view上的touchBegin等方法不正常,具体表现为touchBegin发出之后,接着发出touchCancel,很少的情况下在gesture失效的时候才会有touchEnd,具体过程是我在使用TTTAttributedLabel的时候把TTTAttributedLabel放在了一个view上,又为该view添加了一个tapGesture,于是TTTAttributedLabel中定义的link操作没有收到,请看相关链接https://github.com/TTTAttributedLabel/TTTAttributedLabel/issues/266,
注意里面getaaron这个人的回答

2.对于block,都应该使用copy来声明,原因是block来捕获上下文的信息。【官方文档】Objects
Use Properties to Keep Track of Blocks

3.创建容器对象,比如NSArray和NSDictionary的时候必须严格的check放入的object是否是nil。

4.TTTAttributedLabel的text应该在最后才设置,否则frame和换行等会出现问题。

5.使用MagicalRecord操作数据库的时候,会用到contextForCurrentThread方法从当前线程获取context,但是在串行队列里面并不能保证每次拿到的context都是一样的,具体情况见http://saulmora.com/coredata/magicalrecord/2013/09/15/why-contextforcurrentthread-doesn-t-work-in-magicalrecord.html

6. 应用在后台运行的代码:

- (void)applicationDidEnterBackground:(UIApplication *)application
{
__block UIBackgroundTaskIdentifier bgTask = UIBackgroundTaskInvalid;
bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
dispatch_async(dispatch_get_main_queue(), ^{
if (bgTask != UIBackgroundTaskInvalid) {
bgTask = UIBackgroundTaskInvalid;
}
});
}];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
dispatch_async(dispatch_get_main_queue(), ^{
if (bgTask != UIBackgroundTaskInvalid) {
bgTask = UIBackgroundTaskInvalid;
}
});
});
}


但其实不是应用一直在运行。

7. 计算一段NSString在视图中渲染出来的尺寸

+ (CGSize)sizeOfString:(NSString *)textString font:(UIFont *)font bound:(CGSize)bound {
NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
paragraphStyle.alignment = NSTextAlignmentLeft;
if (font == nil) {
font = [UIFont systemFontOfSize:[UIFont systemFontSize]];
}
NSDictionary * attributes = @{NSFontAttributeName : font,
NSParagraphStyleAttributeName : paragraphStyle};

NSAttributedString* attributedString = [[NSAttributedString alloc] initWithString:textString attributes:attributes];
return [TTTAttributedLabel sizeThatFitsAttributedString:attributedString
withConstraints:CGSizeMake(bound.width, 999)
limitedToNumberOfLines:0];
}


不能使用NSString的[NSString boundingRectWithSize: options: attributes: context:]方法获取到的尺寸偶尔会不准,故不推荐使用。TTTAttributedLabel在这里是使用的CoreText框架进行渲染,更准确一些。
可以参考http://stackoverflow.com/a/37773093/1115791 上我的回答,以及其他人对该问题的回答
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: