一般字符串
1.1判断字符串是否包含另一字符串
//判断字符是否包含某字符串;
NSString*string =@"hello,shenzhen,martin";
//字条串是否包含有某字符串
if([string rangeOfString:@"martin"].location ==NSNotFound)
{
NSLog(@"string 不存在 martin");
}else{
NSLog(@"string 包含 martin");
}
//字条串开始包含有某字符串
if([string hasPrefix:@"hello"])
{
NSLog(@"string 包含 hello");
}else{
NSLog(@"string 不存在 hello");
}
//字符串末尾有某字符串;
if([string hasSuffix:@"martin"]) {
NSLog(@"string 包含 martin");
}else{
NSLog(@"string 不存在 martin");
}
在iOS8以后,还可以用下面的方法来判断是否包含某字符串:
//在iOS8中你可以这样判断
NSString*str =@"hello world";
if([str containsString:@"world"]) {
NSLog(@"str 包含 world");
}else{
NSLog(@"str 不存在 world");
}
1.2字符串大小写转换(可包含其他字符)
NSString*test =@"test";
NSString*testUp = [test uppercaseString];//大写
NSString*testUpFirst = [test capitalizedString];//开头大写,其余小写
NSString*TEACHER =@"TEACHER";
NSString*TEACHERLower = [TEACHER lowercaseString];//小写
NSString*TEACHERUpFirst = [TEACHE RcapitalizedString];//开头大写,其余小写
1.3分割字符串,并将其存放在数组内
NSString*string =@"sdfsfsfsAdfsdf";
NSArray *array = [string componentsSeparatedByString:@"A"]; //从字符A中分隔成2个元素的数组
NSLog(@"array:%@",array); //结果是adfsfsfs和dfsdf
1.4替换字符串部分字符
NSString * theString = @"闽D56565";
//从第三位往后,两个字符串
NSRange range = {3,2};
NSString * resultString = [theString stringByReplacingCharactersInRange:range withString:@"**"];
NSLog(@"result:%@",resultString);
//结果为:result:闽D5**65
带属性字符串-富文本
为字符串指定位置设置特定的文字颜色、大小、字体等
1.1为特定范围内的字符串更改属性
//设置属性字符串,如``¥324.34/单价``,更改324.34和单价的颜色
-(NSMutableAttributedString *)BSGSetAttributedString:(NSString *)priceStr{
NSMutableAttributedString * priceMAStr = [[NSMutableAttributedString alloc]initWithString:priceStr];
NSRange rangeMoney,rangeWord;
NSRange range;
//range的范围为:第一位到`/`符号之前的那个单位;range以第一个字符前面为0,其范围标志插在两字符之间
range = [priceStr rangeOfString:@"/"];
if (range.location != NSNotFound) {
//index从1开始计算
NSInteger startIndex = 1;
//`/`前面的编号
NSInteger midIndex = range.location;
NSInteger len1 = midIndex - startIndex;
//价格
rangeMoney = NSMakeRange(1, len1);
NSInteger lenAll = priceStr.length;
NSInteger len2 = lenAll - (midIndex +1);
//状态
rangeWord = NSMakeRange(midIndex + 1, len2);
//设置颜色
[priceMAStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:rangeMoney];
[priceMAStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:rangeWord];
//设置字体大小
[priceMAStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:17] range:rangeMoney];
[priceMAStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:17] range:rangeWord];
}
//priceLabel.attributedText = priceMAStr;
return priceMAStr;
}
1.2展示网页格式的富文本
NSURL * url = [NSURL URLWithString:@"http://api.luncaiquan.com/Notice/show
app=1&os=2&auth=5dca82255bdb003e5f90612936d45f0f&id=44"];
NSURLRequest * request = [NSURLRequest requestWithURL:url];
NSError * error = nil;
NSData * data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];
NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
NSString * content = dic[@"result"][@"content"];
//str是要显示的字符串
NSMutableAttributedString * attrString = [[NSMutableAttributedString alloc] initWithData:[content
dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute:
NSHTMLTextDocumentType} documentAttributes:nil error:nil];
[attrString addAttributes:@{NSBaselineOffsetAttributeName: @(5),//设置基线偏移值,取值为 NSNumber (float),正值上偏,负值下偏,可使UILabel文本垂直居中
NSFontAttributeName:[UIFont systemFontOfSize:14]} range:NSMakeRange(0,
attrString.length)];
UITextView * textView = [[UITextView alloc]init];
textView.frame = self.view.bounds;
[self.view addSubview:textView];
textView.attributedText = attrString;
textView.editable = NO;
- 网页格式如<p>...</p>
常用的属性
NSFontAttributeName 字体
NSParagraphStyleAttributeName 段落格式
NSForegroundColorAttributeName 字体颜色
NSBackgroundColorAttributeName 背景颜色
NSStrikethroughStyleAttributeName 删除线格式
NSUnderlineStyleAttributeName 下划线格式
NSStrokeColorAttributeName 删除线颜色
NSStrokeWidthAttributeName 删除线宽度
NSShadowAttributeName 阴影
相关资料
iOS开发:字符串设置指定内容的文字颜色、文字大小、文字字体类型
网友评论