近期开发中用到的一些东西(关于自适应大小,字符串处理,心跳,本地推送,json处理等)

Label根据字符串长度自动适应宽度和高度

//这个frame是初设的,没关系,后面还会重新设置其size。
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,0,0)];
label.numberOfLines = 0;
label.backgroundColor = [UIColor clearColor];

NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:20],};

NSString *str = @"abcdefg你上课可是你的拿到了";
CGSize textSize = [str boundingRectWithSize:CGSizeMake(100, 100) options:NSStringDrawingTruncatesLastVisibleLine attributes:attributes context:nil].size;;

[label setFrame:CGRectMake(100, 100, textSize.width, textSize.height)];
label.textColor = [UIColor greenColor];
label.text = str;
[self.view addSubview:label];

/—————-从文件读取字符串:initWithContentsOfFile方法—————-/

NSString *path = @"astring.text";
NSString *astring = [[NSString alloc] initWithContentsOfFile:path];
NSLog(@"astring:%@",astring);
[astring release];


/*----------------写字符串到文件:writeToFile方法----------------*/    


NSString *astring = [[NSString alloc] initWithString:@"This is a String!"];
NSLog(@"astring:%@",astring);
NSString *path = @"astring.text";    
[astring writeToFile: path atomically: YES];
[astring release];    




/*----------------比较两个字符串----------------*/        

//用C比较:strcmp函数

char string1[] = "string!";
char string2[] = "string!";
if(strcmp(string1, string2) = = 0)
{
    NSLog(@"1");
}



//isEqualToString方法    
NSString *astring01 = @"This is a String!";
NSString *astring02 = @"This is a String!";
BOOL result = [astring01 isEqualToString:astring02];
NSLog(@"result:%d",result);




//compare方法(comparer返回的三种值)    
NSString *astring01 = @"This is a String!";
NSString *astring02 = @"This is a String!";    
BOOL result = [astring01 compare:astring02] = = NSOrderedSame;    
NSLog(@"result:%d",result);    
//NSOrderedSame判断两者内容是否相同




NSString *astring01 = @"This is a String!";
NSString *astring02 = @"this is a String!";
BOOL result = [astring01 compare:astring02] = = NSOrderedAscending;    
NSLog(@"result:%d",result);
//NSOrderedAscending判断两对象值的大小(按字母顺序进行比较,astring02大于astring01为真)



NSString *astring01 = @"this is a String!";
NSString *astring02 = @"This is a String!";
BOOL result = [astring01 compare:astring02] = = NSOrderedDescending;    
NSLog(@"result:%d",result);     
//NSOrderedDescending判断两对象值的大小(按字母顺序进行比较,astring02小于astring01为真)



//不考虑大小写比较字符串1
NSString *astring01 = @"this is a String!";
NSString *astring02 = @"This is a String!";
BOOL result = [astring01 caseInsensitiveCompare:astring02] = = NSOrderedSame;    
NSLog(@"result:%d",result);     
//NSOrderedDescending判断两对象值的大小(按字母顺序进行比较,astring02小于astring01为真)



//不考虑大小写比较字符串2
NSString *astring01 = @"this is a String!";
NSString *astring02 = @"This is a String!";
BOOL result = [astring01 compare:astring02
                        options:NSCaseInsensitiveSearch | NSNumericSearch] = = NSOrderedSame;    
NSLog(@"result:%d",result);     

//NSCaseInsensitiveSearch:不区分大小写比较 NSLiteralSearch:进行完全比较,区分大小写 NSNumericSearch:比较字符串的字符个数,而不是字符值。


/*----------------改变字符串的大小写----------------*/    

NSString *string1 = @"A String"; 
NSString *string2 = @"String"; 
NSLog(@"string1:%@",[string1 uppercaseString]);//大写
NSLog(@"string2:%@",[string2 lowercaseString]);//小写
NSLog(@"string2:%@",[string2 capitalizedString]);//首字母大小


/*----------------在串中搜索子串----------------*/        

NSString *string1 = @"This is a string";
NSString *string2 = @"string";
NSRange range = [string1 rangeOfString:string2];
int location = range.location;
int leight = range.length;
NSString *astring = [[NSString alloc] initWithString:[NSString stringWithFormat:@"Location:%i,Leight:%i",location,leight]];
NSLog(@"astring:%@",astring);
[astring release];


/*----------------抽取子串 ----------------*/        

//-substringToIndex: 从字符串的开头一直截取到指定的位置,但不包括该位置的字符
NSString *string1 = @"This is a string";
NSString *string2 = [string1 substringToIndex:3];
NSLog(@"string2:%@",string2);




//-substringFromIndex: 以指定位置开始(包括指定位置的字符),并包括之后的全部字符
NSString *string1 = @"This is a string";
NSString *string2 = [string1 substringFromIndex:3];
NSLog(@"string2:%@",string2);




//-substringWithRange: //按照所给出的位置,长度,任意地从字符串中截取子串
NSString *string1 = @"This is a string";
NSString *string2 = [string1 substringWithRange:NSMakeRange(0, 4)];
NSLog(@"string2:%@",string2);


//扩展路径

NSString *Path = @"~/NSData.txt";
NSString *absolutePath = [Path stringByExpandingTildeInPath];
NSLog(@"absolutePath:%@",absolutePath);
NSLog(@"Path:%@",[absolutePath stringByAbbreviatingWithTildeInPath]);



//文件扩展名
NSString *Path = @"~/NSData.txt";
NSLog(@"Extension:%@",[Path pathExtension]);




/*******************************************************************************************
 NSMutableString
 *******************************************************************************************/    

/*---------------给字符串分配容量----------------*/
//stringWithCapacity:
NSMutableString *String;
String = [NSMutableString stringWithCapacity:40];


/*---------------在已有字符串后面添加字符----------------*/    

//appendString: and appendFormat:

NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];
//[String1 appendString:@", I will be adding some character"];
[String1 appendFormat:[NSString stringWithFormat:@", I will be adding some character"]];
NSLog(@"String1:%@",String1);
*/


/*--------在已有字符串中按照所给出范围和长度删除字符------*/    
/*
 //deleteCharactersInRange:
 NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];
 [String1 deleteCharactersInRange:NSMakeRange(0, 5)];
 NSLog(@"String1:%@",String1);



 /*--------在已有字符串后面在所指定的位置中插入给出的字符串------*/

//-insertString: atIndex:
NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];
[String1 insertString:@"Hi! " atIndex:0];
NSLog(@"String1:%@",String1);



/*--------将已有的空符串换成其它的字符串------*/

//-setString:
NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];
[String1 setString:@"Hello Word!"];
NSLog(@"String1:%@",String1);



/*--------按照所给出的范围,和字符串替换的原有的字符------*/

//-setString:
NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];
[String1 replaceCharactersInRange:NSMakeRange(0, 4) withString:@"That"];
NSLog(@"String1:%@",String1);



/*-------------判断字符串内是否还包含别的字符串(前缀,后缀)-------------*/
//01:检查字符串是否以另一个字符串开头- (BOOL) hasPrefix: (NSString *) aString;
NSString *String1 = @"NSStringInformation.txt";
[String1 hasPrefix:@"NSString"] = = 1 ?  NSLog(@"YES") : NSLog(@"NO");
[String1 hasSuffix:@".txt"] = = 1 ?  NSLog(@"YES") : NSLog(@"NO");

//02:查找字符串某处是否包含其它字符串 - (NSRange) rangeOfString: (NSString *) aString,这一点前面在串中搜索子串用到过;

pragma mark - heart beat

  • (void)wsStartHeartBeat{
    self.webSocketTimeOut = 0;
    if (self.WSHeartTimer) {
    [self.WSHeartTimer invalidate];
    self.WSHeartTimer = nil;
    }

    [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(wsHeartingBeat) userInfo:nil repeats:NO];
    self.WSHeartTimer = [NSTimer scheduledTimerWithTimeInterval:30 target:self selector:@selector(wsHeartingBeat) userInfo:nil repeats:YES];
    }

  • (void)wsHeartingBeat{
    self.webSocketTimeOut += 30;
    if (self.webSocketTimeOut >= WSHeartTimeOut) {
    [self connectWebSocket];//重连
    return;
    }
    }

pragma mark - Local notification

  • (void)pushLocalNotificationWithMessage:(NSString *)message{
    if (!message || message.length <= 0) {
    return;
    }
    // BOOL isAppActivity = [[UIApplication sharedApplication] applicationState] == UIApplicationStateActive;
    // if (isAppActivity) {
    // return;
    // }
    UILocalNotification *noti = [[UILocalNotification alloc] init];
    noti.fireDate = [NSDate date];
    noti.alertBody = message;
    [[UIApplication sharedApplication] scheduleLocalNotification:noti];
    }
    json转字典
  • (NSDictionary *)getDictionaryWithJSONString:(id)jsonStr{
    if (jsonStr == nil) {
    NSLog(@”json为空——-“);
    return nil;
    }
    NSError *err;
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:[jsonStr dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:&err];
    if(err) {
    NSLog(@”json解析失败:%@”,err);
    return nil;
    }

    return dic;
    }
    字典转json

  • (NSString*)dictionaryToJson:(NSDictionary *)dic{
    NSError *parseError = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&parseError];

    return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值