NSString

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];//截取掉下标7之后的字符串
NSLog(@"截取的值为:%@",string);
[string substringFromIndex:2];//截取掉下标2之前的字符串
NSLog(@"截取的值为:%@",string);

2.匹配字符串

NSString*string =@"sdfsfsfsAdfsdf";
NSRangerange = [stringrangeOfString:@"f"];//匹配得到的下标
NSLog(@"rang:%@",NSStringFromRange(range));
string = [string substringWithRange:range];//截取范围类的字符串
NSLog(@"截取的值为:%@",string);

3.分隔字符串

NSString*string =@"sdfsfsfsAdfsdf";
NSArray *array = [str componentsSeparatedByString:@"A"]; //从字符A中分隔成2个元素的数组
NSLog(@"array:%@",array); //结果是adfsfsfs和dfsdf

// 调整行间距

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:textStr];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineSpacing:6];//行间距
styfirstLineHeadIndent = 10.0f;//首行缩进
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [textStr length])];
openMicPrivilegeTipsLabel.attributedText = attributedString;
  1. 常见的属性及说明
    

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

  1. 使用实例
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  =  @"首行缩进根据字体大小自动调整 间隔可自定根据需求随意改变。。。。。。。" ;
    NSMutableParagraphStyle *paraStyle01 = [[NSMutableParagraphStyle alloc] init];
    paraStyle01.alignment = NSTextAlignmentLeft;  //对齐
    paraStyle01.headIndent = 0.0f;//行首缩进
    //参数:(字体大小17号字乘以2,34f即首行空出两个字符)
    CGFloat emptylen = self.contentLabel.font.pointSize * 2;
    paraStyle01.firstLineHeadIndent = emptylen;//首行缩进
    paraStyle01.tailIndent = 0.0f;//行尾缩进
    paraStyle01.lineSpacing = 2.0f;//行间距
    
    NSAttributedString *attrText = [[NSAttributedString alloc] initWithString:_test attributes:@{NSParagraphStyleAttributeName:paraStyle01}];
    self.contentLabel.attributedText = attrText;Text = str;

你可能感兴趣的:(NSString)