字符串互转(数组,字典)
时间戳转为指定字符串
/**
* @method 时间戳装换为指定格式
*
* @param time 时间戳
* @param formatter 转换格式
* @result NSString 字符串
*/
+ (NSString *)timestampChange:(NSInteger)time Formatter:(NSString *)formatter
{
NSString *strTime;
NSDate *newdate=[NSDate dateWithTimeIntervalSince1970:time/1000];//服务器传回来要除1000
NSDateFormatter *dateformatter=[NSDateFormatter new];//定义时间格式
[dateformatter setDateFormat:formatter];
strTime =[dateformatter stringFromDate:newdate];//转为字符串
return strTime;
}
字符串处理
NSString *filename=@"ios.doc";
#-----获取指定字符,根据小数点获取,获取小数点前面的数字
NSRange range = [str rangeOfString:@"."];
NSString *b = [str substringWithRange:NSMakeRange(0,range.location - 1)];
#-----判断一个字符串是否是 ios 开头
bool m = [filename hasPrefix:@"ios"];
#-----判断一个字符串是否是 .doc 结尾
bool n = [filename hasSuffix:@".doc"];
#-----将字符串根据某个字符分离成数组
NSString *filename = @"a,b,bb,dd,";
NSArray *newArray3 = [filename componentsSeparatedByString:@","];
#-----将字符串数组用 逗号分离开
NSArray *dataArray = @[@"111",@"222",@"333",@"444",@"555"];
NSString *str = [dataArray componentsJoinedByString:@","];
NSLog(@"%@",str);
#-----替换所有含有字符 i 的字符(也可以删除-龙换成空字符串)
if([filename containsString:@"i"])
{
filename = [filename stringByReplacingOccurrencesOfString:@"i" withString:@"龙"];
}
#-----判断一个字符串中是否存在某个字符
if([filename containsString:@","])
{
}
if([filename rangeOfString:@"."].location == NSNotFound )
{
NSLog(@"不存在");
}
else
{
NSLog(@"存在");
}
字符串转整型
NSString *num_2=@"3443";
int num=[num_2 intValue];
NSLog(@"%d",num);
删除字符串最后一位
- (NSString *)clearLastString:(NSString *)str
{
if(str.length > 0)
{
str = [str substringWithRange:NSMakeRange(0, [str length] - 1)];
}
return str;
}
判断是否为空
- (BOOL) isNullString:(id)string
{
if(string == nil)
{
return YES;
}
else if([string isKindOfClass:[NSNull class]])
{
return YES;
}
else if([string isEqualToString:@"(null)"])
{
return YES;
}
else if([string isEqualToString:@"<null>"])
{
return YES;
}
else if([[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length] == 0)
{
return YES;
}
else
{
return NO;
}
}
UTF-8 编码
解码
url = [url stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
编码
url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
计算文字高度
/**
* @method 获取指定宽度width的字符串的高度
*
* @param text 文本
* @param Width 限制字符串显示区域的宽度
* @param font Lab的Font
* @result CGFloat 返回的高度
*/
- (CGFloat)HeightWithText:(NSString *)text constrainedToWidth:(CGFloat)width LabFont:(UIFont *)font
{
CGSize textSize=[text boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font} context:nil].size;
CGFloat height = textSize.height + 5;
return height;
}
计算文字宽度
/**
* @method 获取指定宽度width的字符串的宽度
*
* @param text 文本
* @param Width 限制字符串显示区域的宽度
* @param font Lab的Font
* @result CGFloat 返回的宽度
*/
- (CGFloat)widthWithText:(NSString *)text constrainedToHeight:(CGFloat)height LabFont:(UIFont *)font
{
CGSize textSize =[text boundingRectWithSize:CGSizeMake(MAXFLOAT,height) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font} context:nil].size;
CGFloat width = textSize.width + 5;
return width;
}
坑
注意:返回的 height
,width
要 +5 不然会显示不出来
计算富文本高度(带间距)
/**
* @method 获取指定宽度width的带间距富文本的高度
*
* @param text 富文本
* @param Width 限制字符串显示区域的宽度
* @param font 富文本的Font
* @param style 富文本的style
* @result CGFloat 返回的高度
*/
- (CGFloat)HeightWithAttributedString:(NSMutableAttributedString *)text andFont:(UIFont *)font ParagraphStyle:(NSMutableParagraphStyle *)style constrainedToWidth:(CGFloat)width
{
CGRect rect;
CGFloat height;
CGRect textRect=[text boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil];
//文本的高度减去字体高度小于等于行间距,判断为当前只有1行
if ((textRect.size.height - font.lineHeight) <= style.lineSpacing)
{
if ([self containChinese:text.string])//如果包含中文
{
rect = CGRectMake(textRect.origin.x, textRect.origin.y, textRect.size.width, textRect.size.height-style.lineSpacing);
height = rect.size.height;
}
else
{
height = textRect.size.height;
}
}
else
{
height = textRect.size.height;
}
return height;
}
调用
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:string];
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
style.lineSpacing =10;
[attributeString addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, string.length)];
[attributeString addAttribute:NSFontAttributeName value:_titleLab.font range:NSMakeRange(0, string.length)];
CGFloat height2 = [self HeightWithAttributedString:attributeString andFont:_titleLab.font ParagraphStyle:style constrainedToWidth:SCREEN_WIDTH];
[_titleLab updateConstraints:^(MASConstraintMaker *make) {
make.height.equalTo(@(height2 + 5));
}];
[_titleLab setAttributedText:attributeString];
判断字符串是否含有中文
- (BOOL)containChinese:(NSString *)str
{
for(int i=0; i< [str length];i++){
int a = [str characterAtIndex:i];
if( a > 0x4e00 && a < 0x9fff){
return YES;
}
}
return NO;
}
富文本颜色
#pragma mark ************** 富文本
- (void)ParagraphStyle:(NSString *)priceStr Count:(NSString *)count TotalPrice:(NSString *)totalPrice Lab:(UILabel *)lab
{
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = 4.0f;// 行间距
NSDictionary *ats = @{NSParagraphStyleAttributeName : paragraphStyle};
NSMutableAttributedString *priceString = [[NSMutableAttributedString alloc] initWithString:priceStr attributes:ats];
[priceString addAttribute:NSForegroundColorAttributeName
value:defaultColor
range:[priceStr rangeOfString:totalPrice]];
[priceString addAttribute:NSForegroundColorAttributeName
value:defaultColor
range:[priceStr rangeOfString:count]];
lab.attributedText = priceString;
lab.textAlignment = NSTextAlignmentRight;
}