目标:
将对象的一些信息提取保存起来,然后转化成JSON,再上传到服务器,或其他保存本地的用途。
从服务器接收或从本地读取,然后通过JSON的信息生成指定的对象。
方案代码:
//
// KeepLabelInfoModel.h
// TestViewXIB
//
// Created by yococo on 15/9/29.
// Copyright (c) 2015年 yococo. All rights reserved.
//
#import
#import
@interface KeepLabelInfoModel : NSObject
/***************************/
//记录当前所保存的数据信息
@property(nonatomic,strong,readonly) NSMutableArray* infoArray;
/***************************/
//记录指定对象,内部自行提取需要保存的数据信息
- (void) addObject:(UILabel*)anObject;
/***************************/
//将指定的数据信息数组转换成JSON 字符串格式
@property(nonatomic,readonly) NSString* JSONString;
//将指定的数据信息数组转换成JSON 数据格式
@property(nonatomic,readonly) NSData* JSONData;
/***************************/
//内部处理间的错误情况
@property(nonatomic,strong) NSError* error;
/***************************/
//通过获取指定数据信息生成指定对象
- (void) modelObjectsWithJSONString:(NSString*)jsonString;
- (void) modelObjectsWithJSONData:(NSData*)jsonData;
//记录所生成的对象
@property(nonatomic,strong,readonly) NSArray* modelObjects;
@end
//
// infoLabelInfoModel.m
// TestViewXIB
//
// Created by yococo on 15/9/29.
// Copyright (c) 2015年 yococo. All rights reserved.
//
#import "KeepLabelInfoModel.h"
@implementation KeepLabelInfoModel
- (void) dealloc
{
_infoArray = nil;
_error = nil;
_modelObjects = nil;
}
- (void) addObject:(UILabel*)anObject
{
if (_infoArray == nil) {
_infoArray = [NSMutableArray array];
}
if (anObject == nil || [anObject isKindOfClass:[UILabel class]] == NO) {
return;
}
NSMutableDictionary* tempDictionary = [NSMutableDictionary dictionary];
[tempDictionary setObject:anObject.text forKey:@"text"];
[tempDictionary setObject:NSStringFromCGRect(anObject.frame) forKey:@"frame"];
[tempDictionary setObject:NSStringFromCGAffineTransform(anObject.transform) forKey:@"transform"];
[tempDictionary setObject:[self getHSBAStringByColor:anObject.textColor] forKey:@"textColor"];
[_infoArray addObject:tempDictionary];
}
- (NSString*) JSONString
{
if (_infoArray == nil) {
return nil;
}
NSError* tempError = nil;
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:_infoArray options:NSJSONWritingPrettyPrinted error:&tempError];
if (tempError == nil) {
NSString* jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
return jsonString;
}
self.error = tempError;
return nil;
}
- (NSData*) JSONData
{
if (_infoArray == nil) {
return nil;
}
NSError* tempError = nil;
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:_infoArray options:NSJSONWritingPrettyPrinted error:&tempError];
if (tempError == nil) {
return jsonData;
}
self.error = tempError;
return nil;
}
- (void) modelObjectsWithJSONData:(NSData *)jsonData
{
if (jsonData == nil) {
return;
}
NSError* tempError = nil;
NSArray* tempArr = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&tempError];
if (tempError) {
self.error = tempError;
return;
}
if (tempArr == nil || [tempArr isKindOfClass:[NSArray class]] == NO) {
return;
}
NSMutableArray* tempArray = [NSMutableArray array];
for (NSDictionary* tempDictionary in tempArr) {
if ([tempDictionary isKindOfClass:[NSDictionary class]]) {
UILabel* tempLabel = [[UILabel alloc] init];
NSString* tempText = tempDictionary[@"text"];
if (tempText && [tempText isKindOfClass:[NSString class]]) {
tempLabel.text = tempText;
}
NSString* tempFrame = tempDictionary[@"frame"];
if (tempFrame && [tempFrame isKindOfClass:[NSString class]]) {
tempLabel.frame = CGRectFromString(tempFrame);
}
NSString* tempTransform = tempDictionary[@"transform"];
if (tempTransform && [tempTransform isKindOfClass:[NSString class]]) {
tempLabel.transform = CGAffineTransformFromString(tempTransform);
}
NSString* tempTextColor = tempDictionary[@"textColor"];
if (tempTextColor && [tempTextColor isKindOfClass:[NSString class]]) {
tempLabel.textColor = [self colorWithHSBAString:tempTextColor];
}
[tempArray addObject:tempLabel];
}
}
_modelObjects = tempArray;
}
- (void) modelObjectsWithJSONString:(NSString*)jsonString
{
if (jsonString == nil) {
return;
}
NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
[self modelObjectsWithJSONData:jsonData];
}
- (NSString *)getHSBAStringByColor:(UIColor *)originColor {
NSDictionary *hsbaDict = [self getHSBAValueByColor:originColor];
return [NSString stringWithFormat:@"(%0.2f, %0.2f, %0.2f, %0.2f)",
[hsbaDict[@"H"] floatValue],[hsbaDict[@"S"] floatValue],[hsbaDict[@"B"] floatValue],[hsbaDict[@"A"] floatValue]];
}
- (NSDictionary *)getHSBAValueByColor:(UIColor *)originColor
{
CGFloat h=0,s=0,b=0,a=0;
if ([originColor respondsToSelector:@selector(getHue:saturation:brightness:alpha:)]) {
[originColor getHue:&h saturation:&s brightness:&b alpha:&a];
}
return @{@"H":@(h),@"S":@(s),@"B":@(b),@"A":@(a)};
}
- (UIColor *) colorWithHSBAString:(NSString*)aColorString
{
if (aColorString == nil) {
return nil;
}
NSString* tempString = [aColorString stringByReplacingOccurrencesOfString:@"(" withString:@""];
tempString = [aColorString stringByReplacingOccurrencesOfString:@")" withString:@""];
NSArray* tempArray = [tempString componentsSeparatedByString:@","];
if (tempArray && tempArray.count == 4) {
return [UIColor colorWithHue:[tempArray[0] floatValue] saturation:[tempArray[1] floatValue] brightness:[tempArray[2] floatValue] alpha:[tempArray[3] floatValue]];
}
return nil;
}
@end
通过 Button 以及 Storyboard 上的Label进行测试
1.获取 Label 的信息
KeepLabelInfoModel* tempModel = [[KeepLabelInfoModel alloc] init];
for (UILabel* subview in self.view.subviews) {
if ([subview isKindOfClass:[UILabel class]]) {
[tempModel addObject:subview];
}
}
NSLog(@"%@",tempModel.JSONString);
for (UILabel* subview in self.view.subviews) {
if ([subview isKindOfClass:[UILabel class]]) {
[subview removeFromSuperview];
}
}
NSString* tempString = @"["
"{"
"\"textColor\" : \"(0.00, 0.00, 0.67, 1.00)\","
"\"frame\" : \"{{104, 380}, {134, 35}}\","
"\"text\" : \"123\","
"\"transform\" : \"[1, 0, 0, 1, 0, 0]\""
"},"
"{"
"\"textColor\" : \"(0.00, 0.96, 1.00, 1.00)\","
"\"frame\" : \"{{273, 276}, {29, 21}}\","
"\"text\" : \"234\","
"\"transform\" : \"[1, 0, 0, 1, 0, 0]\""
"},"
"{"
"\"textColor\" : \"(0.00, 0.00, 1.00, 1.00)\","
"\"frame\" : \"{{150, 498}, {29, 21}}\","
"\"text\" : \"567\","
"\"transform\" : \"[1, 0, 0, 1, 0, 0]\""
"},"
"{"
"\"textColor\" : \"(0.00, 0.00, 0.00, 1.00)\","
"\"frame\" : \"{{217, 405}, {29, 21}}\","
"\"text\" : \"346\","
"\"transform\" : \"[1, 0, 0, 1, 0, 0]\""
"},"
"{"
"\"textColor\" : \"(0.18, 0.74, 1.00, 1.00)\","
"\"frame\" : \"{{273, 498}, {29, 21}}\","
"\"text\" : \"498\","
"\"transform\" : \"[1, 0, 0, 1, 0, 0]\""
"},"
"]";
KeepLabelInfoModel* tempModel = [[KeepLabelInfoModel alloc] init];
[tempModel modelObjectsWithJSONString:tempString];
for (UIView* subview in tempModel.modelObjects) {
if ([subview isKindOfClass:[UIView class]]) {
[self.view addSubview:subview];
}
}