一般字符串
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字符串大小寫轉(zhuǎn)換(可包含其他字符)
NSString*test =@"test";
NSString*testUp = [test uppercaseString];//大寫
NSString*testUpFirst = [test capitalizedString];//開頭大寫,其余小寫
NSString*TEACHER =@"TEACHER";
NSString*TEACHERLower = [TEACHER lowercaseString];//小寫
NSString*TEACHERUpFirst = [TEACHE RcapitalizedString];//開頭大寫,其余小寫
1.3分割字符串,并將其存放在數(shù)組內(nèi)
NSString*string =@"sdfsfsfsAdfsdf";
NSArray *array = [string componentsSeparatedByString:@"A"]; //從字符A中分隔成2個(gè)元素的數(shù)組
NSLog(@"array:%@",array); //結(jié)果是adfsfsfs和dfsdf
1.4替換字符串部分字符
NSString * theString = @"閩D56565";
//從第三位往后,兩個(gè)字符串
NSRange range = {3,2};
NSString * resultString = [theString stringByReplacingCharactersInRange:range withString:@"**"];
NSLog(@"result:%@",resultString);
//結(jié)果為:result:閩D5**65
帶屬性字符串-富文本
為字符串指定位置設(shè)置特定的文字顏色、大小、字體等
1.1為特定范圍內(nèi)的字符串更改屬性
//設(shè)置屬性字符串,如``¥324.34/單價(jià)``,更改324.34和單價(jià)的顏色
-(NSMutableAttributedString *)BSGSetAttributedString:(NSString *)priceStr{
NSMutableAttributedString * priceMAStr = [[NSMutableAttributedString alloc]initWithString:priceStr];
NSRange rangeMoney,rangeWord;
NSRange range;
//range的范圍為:第一位到`/`符號(hào)之前的那個(gè)單位;range以第一個(gè)字符前面為0,其范圍標(biāo)志插在兩字符之間
range = [priceStr rangeOfString:@"/"];
if (range.location != NSNotFound) {
//index從1開始計(jì)算
NSInteger startIndex = 1;
//`/`前面的編號(hào)
NSInteger midIndex = range.location;
NSInteger len1 = midIndex - startIndex;
//價(jià)格
rangeMoney = NSMakeRange(1, len1);
NSInteger lenAll = priceStr.length;
NSInteger len2 = lenAll - (midIndex +1);
//狀態(tài)
rangeWord = NSMakeRange(midIndex + 1, len2);
//設(shè)置顏色
[priceMAStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:rangeMoney];
[priceMAStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:rangeWord];
//設(shè)置字體大小
[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展示網(wǎng)頁(yè)格式的富文本
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),//設(shè)置基線偏移值,取值為 NSNumber (float),正值上偏,負(fù)值下偏,可使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;
- 網(wǎng)頁(yè)格式如<p>...</p>
常用的屬性
NSFontAttributeName 字體
NSParagraphStyleAttributeName 段落格式
NSForegroundColorAttributeName 字體顏色
NSBackgroundColorAttributeName 背景顏色
NSStrikethroughStyleAttributeName 刪除線格式
NSUnderlineStyleAttributeName 下劃線格式
NSStrokeColorAttributeName 刪除線顏色
NSStrokeWidthAttributeName 刪除線寬度
NSShadowAttributeName 陰影
相關(guān)資料
iOS開發(fā):字符串設(shè)置指定內(nèi)容的文字顏色、文字大小、文字字體類型