NSString *temp = @"english, french, japanese, chinese";
NSString *jap = @"japanese";
NSRange foundObj=[temp rangeOfString:jap options:NSCaseInsensitiveSearch];
if(foundObj.length>0) {
NSLog(@"Yes ! Jap found");
} else {
NSLog(@"Oops ! no jap");
}
4.從某一個字符串中的一個字符串開始截取
NSString *a =@"/pubic/evdaily.nsf/vwtrymaindocid/8E88469482579180035442A/$file/測試帳號及信息22.docx";
NSRange range = [a rangeOfString:@"$file/"];//獲取$file/的位置
NSString *b = [a substringFromIndex:range.location +
range.length];//開始截取
NSLog(@"\n b: %@",b);
1.截取字符串
NSString*string =@"sdfsfsfsAdfsdf";
string = [string substringToIndex:7];//截取掉下標(biāo)7之后的字符串
NSLog(@"截取的值為:%@",string);
[string substringFromIndex:2];//截取掉下標(biāo)2之前的字符串
NSLog(@"截取的值為:%@",string);
2.匹配字符串
NSString*string =@"sdfsfsfsAdfsdf";
NSRangerange = [stringrangeOfString:@"f"];//匹配得到的下標(biāo)
NSLog(@"rang:%@",NSStringFromRange(range));
string = [string substringWithRange:range];//截取范圍類的字符串
NSLog(@"截取的值為:%@",string);
3.分隔字符串
NSString*string =@"sdfsfsfsAdfsdf";
NSArray *array = [str componentsSeparatedByString:@"A"]; //從字符A中分隔成2個元素的數(shù)組
NSLog(@"array:%@",array); //結(jié)果是adfsfsfs和dfsdf
// 調(diào)整行間距
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:textStr];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineSpacing:6];//行間距
styfirstLineHeadIndent = 10.0f;//首行縮進(jìn)
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [textStr length])];
openMicPrivilegeTipsLabel.attributedText = attributedString;
常見的屬性及說明
NSFontAttributeName
字體
NSParagraphStyleAttributeName
段落格式
NSForegroundColorAttributeName
字體顏色
NSBackgroundColorAttributeName
背景顏色
NSStrikethroughStyleAttributeName
刪除線格式
NSUnderlineStyleAttributeName
下劃線格式
NSStrokeColorAttributeName
刪除線顏色
NSStrokeWidthAttributeName
刪除線寬度
NSShadowAttributeName
陰影
更多方法和屬性說明詳見蘋果官方說明文檔:
https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSMutableAttributedString_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40003689
- 使用實例
UILabel *testLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 100, 320, 30)];
testLabel.backgroundColor = [UIColor lightGrayColor];
testLabel.textAlignment = NSTextAlignmentCenter;
NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:@"今天天氣不錯呀"];
[AttributedStr addAttribute:NSFontAttributeName
value:[UIFont systemFontOfSize:16.0]
range:NSMakeRange(2, 2)];
[AttributedStr addAttribute:NSForegroundColorAttributeName
value:[UIColor redColor]
range:NSMakeRange(2, 2)];
testLabel.attributedText = AttributedStr;
[self.view addSubview:testLabel];
4.富文本
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"Using NSAttributed String"];
[str addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0,5)];
[str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(6,12)];
[str addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(19,6)];
[str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial-BoldItalicMT" size:30.0] range:NSMakeRange(0, 5)];
[str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Bold" size:30.0] range:NSMakeRange(6, 12)];
[str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Courier-BoldOblique" size:30.0] range:NSMakeRange(19, 6)];
attrLabel.attribu
NSString *_test = @"首行縮進(jìn)根據(jù)字體大小自動調(diào)整 間隔可自定根據(jù)需求隨意改變焙压。旬蟋。觅彰。拯腮。技掏。幔崖。滋迈。" ;
NSMutableParagraphStyle *paraStyle01 = [[NSMutableParagraphStyle alloc] init];
paraStyle01.alignment = NSTextAlignmentLeft; //對齊
paraStyle01.headIndent = 0.0f;//行首縮進(jìn)
//參數(shù):(字體大小17號字乘以2罪郊,34f即首行空出兩個字符)
CGFloat emptylen = self.contentLabel.font.pointSize * 2;
paraStyle01.firstLineHeadIndent = emptylen;//首行縮進(jìn)
paraStyle01.tailIndent = 0.0f;//行尾縮進(jìn)
paraStyle01.lineSpacing = 2.0f;//行間距
NSAttributedString *attrText = [[NSAttributedString alloc] initWithString:_test attributes:@{NSParagraphStyleAttributeName:paraStyle01}];
self.contentLabel.attributedText = attrText;Text = str;