NSString和NSMutableString的常用方法 :
//NSString常用方法
NSString *string = @"wangJiao";
//首字母大寫
NSLog(@"string: %@",[string capitalizedString]);
//全部小寫
NSLog(@"string: %@",[string lowercaseString]);
//全部大寫
NSLog(@"string: %@",[string uppercaseString]);
NSString *baseStr = @"Someday U will know this";
//判斷是否包含前后綴
BOOL isHasS = [baseStr hasPrefix:@"Someday"];
BOOL isHasT = [baseStr hasSuffix:@"this"];
//分割字符串
NSArray *array = [baseStr componentsSeparatedByString:@"a"];
NSString *string1 = [array objectAtIndex:0];
NSString *string2 = [array objectAtIndex:1];
NSLog(@"string1:%@ string2:%@",string1,string2);
//去除字符串首尾的空格和換行符
NSString *text = [baseStr stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSLog(@"text:%@",text);
//NSString替換字符串
NSString *replaceString = [baseStr stringByReplacingOccurrencesOfString:@"U" withString:@"I"];
NSLog(@"replaceString: %@",replaceString);
//NSMutableString常用方法
NSMutableString *baseMulStr = [[NSMutableString alloc] initWithString:baseStr];
//NSMutableString替換字符串
//按位置替換
[baseMulStr replaceCharactersInRange:NSMakeRange(0, 1) withString:@"a"];
//按字符替換
[baseMulStr stringByReplacingOccurrencesOfString:@"i" withString:@"u"];
//拼接字符串
[baseMulStr appendString:@"thing"];
NSLog(@"string:%@",baseMulStr);
//插入字符串
[baseMulStr insertString:@"perhaps " atIndex:7];
NSLog(@"string: %@",baseMulStr);
//刪除字符串
[baseMulStr deleteCharactersInRange:NSMakeRange(8, 1)];
NSLog(@"String1: %@",baseMulStr);
//判斷是否包含某個字符
BOOL IsContain = [str containsString:@"+"];
//注意判斷是否包含這個方法只支持iOS8以上的系統(tǒng), 要適配iOS7可以換下面這種方式實現(xiàn)
NSRange range0 = [str rangeOfString:@"+"];
if (range0.location == NSNotFound) 不包含, 否則包含"+"
//使用范圍比較大的話, 可以擴展成一個方法
+ (BOOL) checkStrContainsString:(NSString *)rangeStr with:(NSString *)str
{
NSRange range = [str rangeOfString:rangeStr];
if (range.location == NSNotFound) {
return NO;
}
return YES ;
}
**富文本簡單說明 : **
- 和String 分為 NSString與NSMutableString一樣 , AttributeString也分為NSAttributedString,和NSMutableAttributedString,但是通常我都用NSMutableAttributedString ,因為一般我使用 AttributeString ,或多或少都想要改變它的某一屬性,而不是一個固定的屬性創(chuàng)建完字符串之后,屬性不再變化.
- NSMutableAttributedString 繼承自 NSAttributedString ,它更能夠滿足我的需求, 以下主要介紹NSMutableAttributedString ,有涉及NSAttributedString 的地方會特別標出, 沒標出的就默認是NSMutableAttributedString 的方法和屬性 .
1.NSMutableAttributedString 分段設置文本屬性:
NSString * text = @"今天天氣好晴朗,嘿!處處百花香,嘿嘿嘿!!!明天星期二,后天就是星期三,再有兩天就又放假咯!吼吼吼~~~";
NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:text];
NSDictionary *attributeDict = @{NSFontAttributeName: [UIFont systemFontOfSize:18.0],
NSForegroundColorAttributeName: [UIColor redColor]};
NSDictionary *attributeDict1 = @{NSFontAttributeName: [UIFont systemFontOfSize:23.0],
NSForegroundColorAttributeName: [UIColor colorWithRed:0.130 green:0.854 blue:0.345 alpha:1.000]};
[attrStr setAttributes:attributeDict1 range:NSMakeRange(0, 7)];
[attrStr setAttributes:attributeDict range:NSMakeRange(7, attrStr.length -7)];
2.在需要的范圍內加上屬性, 注意看"嘿嘿嘿"三個字變大了:
[attrStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:23.0] range:NSMakeRange(16, 3)];
3.用屬性字典的方式實現(xiàn) 在需要的范圍內加上屬性, 注意看"!!"顏色變了:
[attrStr addAttributes:attributeDict1 range:NSMakeRange(20, 2)];
4.刪除具體范圍內的屬性, 注意天星期字體變了:
[attrStr removeAttribute:NSFontAttributeName range:NSMakeRange(23, 3)];
5.替換字符串, 注意"天天"被替換掉了:
[attrStr replaceCharactersInRange:NSMakeRange(1, 2) withString:@""];
6.插入帶屬性的字符串 :
NSMutableString *mutStr = attrStr.mutableString;
NSMutableAttributedString *tempStr1 = [[NSMutableAttributedString alloc] initWithString:@"插入的文字" attributes:attributeDict1];
NSMutableAttributedString *tempStr2 = [[NSMutableAttributedString alloc] initWithString:@"插入的文字"];
[attrStr insertAttributedString:tempStr1 atIndex:7];
7.插入不帶屬性的字符串:
[attrStr insertAttributedString:tempStr2 atIndex:7];
8.拼接和刪除字符串, 效果不貼了:
[attrStr appendAttributedString:tempStr1];
[attrStr deleteCharactersInRange:NSMakeRange(3, 3)];
9.判斷字符串相等,取子字符串, 這兩個方法是繼承自NSAttributedString的
[attrStr isEqualToAttributedString:attrStr];
[attrStr attributedSubstringFromRange:NSMakeRange(3, 6)];
動態(tài)獲取文本size :
// NString 動態(tài)獲取文本size, 這個方法默認是一行, 所以如果文本很多的話, 寬度會無限增大
CGSize size = [text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:fnt,NSFontAttributeName, nil]];
// NString 寬度一定動態(tài)獲取文本高度
CGRect strRect = [text boundingRectWithSize:CGSizeMake(150, MAXFLOAT) options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading) attributes:@{NSFontAttributeName : fontTemp} context:nil];
// NSAttributedString 寬度一定動態(tài)獲取文本高度
CGRect strRect = [attrStr boundingRectWithSize:CGSizeMake(150, MAXFLOAT) options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading) context:nil];
Tips: 高度一定, 動態(tài)獲取寬度, 只需高度寫死, 寬度無限大或0即可 ;
目前, 在UIKit中支持富文本的常用控件有四個:
- UILabel
- UITextField
- UITextView
- UIButton
除此之外, 還有一個相比來說不那么常用的控件也支持富文本, 并且在沒有用戶交互, 只展示文本的時候我更喜歡用它, 它就是CATextLayer , 我上面所有展示效果都是用CATextLayer實現(xiàn)的, 感興趣的童鞋可以看看我寫的另一篇文章CALayer及其子類(一) .