iOS String字符串

普通文本NSString

NSString *string;
[string rangeOfString:@""];  // 获取某段字符串在原串中的范围
[string substringWithRange:];  // 根据Range截取字符串
[string stringByReplacingOccurencesOfString:@".0" withString:@""];  // 将string中的.0替换为空
string.lowercaseString;  // string的小写字符串
string.pathExtension;  // 根据路径string获取文件扩展名

富媒体文本NSAttributedString

// AttributedString
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@“哈哈123”];
// 添加一个属性
[string addAttribute:(NSString*) value:(id) range:(NSRange)];
// NSFontAttributeName
// NSForegroundColorAttributeName
// NSBackgroundColorAttributeName
// 测量字体所占空间范围
CGSize size = [string boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin context:nil].size;
// 设置行高
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc]init];
style.lineSpacing = 10;  // 行高
style.headIndent = 10;  // 缩进
style.firstLineHeadIndent = 20;  // 首行缩进
[string addAttribute:NSParagraphStyleAttributeName value:style range:range];

图片附件

NSTextAttachment *attach = [[NSTextAttachment alloc] init];
attach.image =[UIImage imageNamed:@“”];
textview.font.lineHeight;  // 获取字体高度
attach.bounds = CGRectMake(0, 0, 15, 15); // set bounds;
NSAttributedString *string = [NSAttributedString attributedStringWithAttachment:attach];

遍历获取富文本中的文本

[string enumerateAttributesInRange:range options:0 usingBlock:^(NSDictionary *attrs, NSRange range, BOOL *stop){
   // 如果是图片,通过attrs[@“NSAttachment”]即可取出对应的图片attachment对象;
   // 如果不是图片,则直接通过range获取此段文字;
   NSTextAttachment *attach = attrs[@"NSAttachment"];
   if(attach){
       // [string appendString:attach.emotion.chs];
   }else{ // append original string;
       [string appendString:[str attributedSubstringFromRange:range].string];
   }
}];
// 获取子串;
[string attributedSubstringFromRange:].string;

普通text文字的大小通过font设置;
富文本attributedText文字大小通过addAttributed方法设置;

正则表达式

NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@“[a-z]” options:NSRegularExpressionCaseInsensitive error:nil];
// 开始匹配
NSArray *arr = [regex matchesInString:str options:NSMatchingReportCompletion range:NSMakeRange(0, str.length)];

常用的Pattern

QQPattern:@“[0-9]{5,12}”;
汉字Pattern:@“\\u4e00-\\u9fa5”;
#话题#Pattern:@“#[a-zA-Z0-9\\u4e00-\\u9fa5]+#”;  @“#([^\\#|.]+)#”;
@提到Pattern:@“@[a-zA-Z0-9\\u4e00-\\u9fa5\\-\\w]+ ?”;
httpPattern:@"http(s)?://([a-zA-Z|\\d]+\\.)+[a-zA-Z|\\d]+(/[a-zA-Z|\\d|\\-|\\+|_./?%&=]*)?”;

第三方库RegexKitLite
依赖于libicucore.dylib

[str emunerateStringsMatchedByRegex:regex usingBlock:^{
    NSLog(@“%@ %@”, *captureStrings, NSStringFromRange(*captureStrings));
}]; // 
[str enumerateStringsSeparatedByRegex:regex usingBlock:^{
}]; // 

计算字体区域大小

CGSize size = [string boundingRectWithSize:CGSizeMake(MXAFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: font} context:nil].size;
//
CGSize size = [str sizeWithFont:[UIFont systemFontOfSize:16.0f] constrainedToSize:CGSizeMake(300, 1000000) lineBreakMode:NSLineBreakByWordWrapping];

需要注意的是,一定要设置label使用的字体大小和上面计算方法里面设置的字体大小;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值