标签:
不是什么非常高深的知识、只是记载开发中用到过的一些小技巧。随时更新!
//定义UILable宽度最大值 CGFloat contentMaxW = 300; //获取UILabel的Size,注意传入的Font大小要与UILabel的的Font大小保持相同 CGSize contentSize = [content boundingRectWithSize:CGSizeMake(contentMaxW, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} context:nil].size; //注意要设置UILable的numberOfLines属性为0,才能显示多行文字 contentLabel.numberOfLines = 0;
NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[htmlstr dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
contentLabel.attributedText = attrStr;
AFN非常优秀的HTTP网络框架,一般在项目中我们会在其基础上封装网络层。在这一层处理传入的url和参数,例如数据加密。
//YQHttpTool.h + (void)post:(NSString *)url params:(NSDictionary *)params success:(void (^)(id json))success failure:(void (^)(NSError *error))failure; + (void)get:(NSString *)url params:(NSDictionary *)params success:(void (^)(id json))success failure:(void (^)(NSError *error))failure; //YQHttpTools.m + (void)post:(NSString *)url params:(NSDictionary *)params auth:(NSDictionary *)auth success:(void (^)(id))success failure:(void (^)(NSError *))failure { AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager]; mgr.responseSerializer = [AFHTTPResponseSerializer serializer]; [mgr POST:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) { if(success){ success(responseObject); } } failure:^(AFHTTPRequestOperation *operation, NSError *error) { if(failure){ failure(error); } }]; }
+ (void)get:(NSString *)url params:(NSDictionary *)params success:(void (^)(id))success failure:(void (^)(NSError *))failure { AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager]; mgr.responseSerializer = [AFHTTPResponseSerializer serializer]; [mgr GET:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) { if(success){ success(responseObject); } } failure:^(AFHTTPRequestOperation *operation, NSError *error) { if(failure){ failure(error); } }]; }
//appID为应用的appid,建议定义成宏 define appID @"xxxxxxxxx" - (void)checkUpdate { NSString *url = [NSString stringWithFormat:@"https://itunes.apple.com/lookup?id=%@", appID]; [YQHttpTool getWithURL:url params:nil success:^(id json) { id jsonObject = [NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingAllowFragments error:nil]; if ([jsonObject isKindOfClass:[NSDictionary class]]) { NSDictionary *dic = jsonObject; if (nil != dic) { NSArray *result_array = [dic objectForKey:@"results"]; if (nil != result_array && [result_array count] != 0) { NSDictionary *result_dic = [result_array objectAtIndex:0]; if (nil != result_dic) { NSString *version = [result_dic objectForKey:@"version"]; NSString *releaseNotes = [result_dic objectForKey:@"releaseNotes"]; NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Info" ofType:@"plist"]; NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithContentsOfFile: filePath]; NSString *oldVersion = [dict objectForKey: @"CFBundleShortVersionString"]; if ([version compare:oldVersion options:NSNumericSearch] == NSOrderedDescending) { UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"升级提示" message:releaseNotes delegate:self //委托给Self,才会执行上面的调用 cancelButtonTitle:@"以后再说" otherButtonTitles:@"马上更新",nil]; [av show]; } } } } } } failure:^(NSError *error) { }]; }
//遵循<UIAlertView>
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (buttonIndex != [alertView cancelButtonIndex])
{
NSString *str = [NSString stringWithFormat:@"itms-apps://itunes.apple.com/app/id%@?mt=8", appID];
NSURL *url = [NSURL URLWithString:str];
[[UIApplication sharedApplication] openURL:url];
}
}
标签:
原文地址:http://www.cnblogs.com/c-y-q/p/4692521.html