iOS 字串常用方法總結——不定時更新
1.字串逆序排序:
2.漢字轉拼音:NSString *strs = @"abcdefg"; NSString *str = @""; for (int i = strs.length-1; i>-1; i--) { char a = [strs characterAtIndex:i]; NSLog(@"#########%c",a); str= [str stringByAppendingString:[NSString stringWithFormat:@"%c",a]]; NSLog(@"&&&&&&&&%@",str); }
3.刪除指定字元:+ (NSString*)HanZiZhuanPinYin:(NSString*)hanzi { CFStringRef aCFString = (CFStringRef)hanzi; CFMutableStringRef string = CFStringCreateMutableCopy(NULL, 0, aCFString); CFStringTransform(string, NULL, kCFStringTransformMandarinLatin, NO); CFStringTransform(string, NULL, kCFStringTransformStripDiacritics, NO); //CFRelease(string); return (NSString*)string; }
4.擷取字串:NSArray *array = @[@"(",@")",@"-",@" "]; for (NSString *str in array) { NSRange range = [theStr rangeOfString:str]; while (range.length > 0) { [theStr deleteCharactersInRange:range]; range = [theStr rangeOfString:str]; NSLog(@"*******%d",range.length); } }
NSString *strrrr = @"1233456.png";
NSRange range = [strrrr rangeOfString:@"."];
NSString *result = [strrrr substringToIndex:range.location]
關於字串的擷取:
1、substringFromIndex:x //從索引x開始到結束,包含索引x指向的值(索引的理解有點像C語言中陣列的下標)
2、substringToIndex:x //從開始到索引x之前結束,不包含索引x指向的值
3、substringWithRange //定義一個區間,有開始位置和擷取長度
如下:
NSString *d = [a1 substringFromIndex:1];
NSString *e = [a1 substringToIndex:3];
NSLog(@"d = %@,e = %@",d,e);
結果輸出為:d = evin,e = kev
substringWithRange先定義區間,如下:
NSRange r = {2,3}; //從索引2開始,擷取3個長度
NSString *f = [c substringWithRange:r];
NSLog(@"f = %@",f);
結果輸出為:f = vin
5.coreText
tips:主要掌握range的使用,對於value只是一些固定的屬性
UITextView *tf = [[UITextView alloc]initWithFrame:CGRectMake(50, 100, 270, 30)];
tf.editable = NO; //設定不可編輯
NSString *str = @"2536456http://www.baidu.com";
NSMutableAttributedString *aStr = [[NSMutableAttributedString alloc]initWithString:str];
[aStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 2)];//設定字型顏色<pre name="code" class="html">range:NSMakeRange(0, 2) 表示從下標0開始,取兩個長度的字元
[aStr addAttribute:NSForegroundColorAttributeName value:[UIColor cyanColor] range:NSMakeRange(2, 2)]; [aStr addAttribute:NSForegroundColorAttributeName value:[UIColor purpleColor] range:NSMakeRange(4, 3)]; [aStr addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(7, 20)]; [aStr addAttribute:NSLinkAttributeName value:@"http://www.baidu.com" range:NSMakeRange(7, 20)];//設定超連結
value為你要跳轉的網址
[aStr addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:[str rangeOfString:@"http://www.baidu.com"]]; //設定下劃線 對於此屬性5.1之前label可以顯示下劃線,但是6.1就不可以了
[aStr addAttribute:NSUnderlineColorAttributeName value:[UIColor blackColor] range:NSMakeRange(7, 20)];//設定下劃線顏色tf.attributedText = aStr;[self.view addSubview:tf];
6.關於計算文字大小:
之前使用了NSString類的sizeWithFont:constrainedToSize:lineBreakMode:方法,但是該方法已經被iOS7 Deprecated了,而iOS7新出了一個boudingRectWithSize:options:attributes:context方法來代替:
- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options attributes:(NSDictionary *)attributes context:(NSStringDrawingContext *)context NS_AVAILABLE_IOS(7_0);
可以使用該方法計算文字繪製所需的空間。size引數是一個constraint ,用於在繪製文字時作為參考。但是,如果繪製完整個文字需要更大的空間,則返回的矩形大小可能比 size更大。一般,繪製時會採用constraint提供的寬度,但高度則會根據需要而定。
+ (CGSize)stringSize:(NSString *)contentString widthOfFatherView:(CGFloat)width textFont:(UIFont *)font{
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_7_0
CGSize size = [contentString sizeWithFont:font constrainedToSize:CGSizeMake(width, CGFLOAT_MAX)];
return size;
#else
NSDictionary *attributesDic = @{NSFontAttributeName:font};
CGSize size = [contentString boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributesDic context:nil].size;
return size;
#endif
}
儲存一個網址:http://www.objccn.io/issue-9-2/
暫時就先貼這麼多了,有點混亂,歡迎吐槽,有空更新。。。。。