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以上的系统, 要适配iOS7可以换下面这种方式实现
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 ,或多或少都想要改变它的某一属性,而不是一个固定的属性创建完字符串之后,属性不再变化.
- 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.用属性字典的方式实现 在需要的范围内加上属性, 注意看"!!"颜色变了:
[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)];
动态获取文本size :
// NString 动态获取文本size, 这个方法默认是一行, 所以如果文本很多的话, 宽度会无限增大
CGSize size = [text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:fnt,NSFontAttributeName, nil]];
// NString 宽度一定动态获取文本高度
CGRect strRect = [text boundingRectWithSize:CGSizeMake(150, MAXFLOAT) options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading) attributes:@{NSFontAttributeName : fontTemp} context:nil];
// NSAttributedString 宽度一定动态获取文本高度
CGRect strRect = [attrStr boundingRectWithSize:CGSizeMake(150, MAXFLOAT) options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading) context:nil];
Tips: 高度一定, 动态获取宽度, 只需高度写死, 宽度无限大或0即可 ;
目前, 在UIKit中支持富文本的常用控件有四个:
- UILabel
- UITextField
- UITextView
- UIButton
除此之外, 还有一个相比来说不那么常用的控件也支持富文本, 并且在没有用户交互, 只展示文本的时候我更喜欢用它, 它就是CATextLayer , 我上面所有展示效果都是用CATextLayer实现的, 感兴趣的童鞋可以看看我写的另一篇文章CALayer及其子类(一) .