#import <Foundation/Foundation.h>

@interface NSString (Extension)

- (CGFloat)heightWithLimitWidth:(CGFloat)width
fontSize:(CGFloat)fontSize
otherAttributes:(NSDictionary *)otherAttributes; - (CGFloat)widthWithLimitHeight:(CGFloat)height
fontSize:(CGFloat)fontSize
otherAttributes:(NSDictionary *)otherAttributes; - (CGSize)sizeWithLimitSize:(CGSize)size
fontSize:(CGFloat)fontSize
otherAttributes:(NSDictionary *)otherAttributes; - (CGRect)rectWithSize:(CGSize)size
fontSize:(CGFloat)fontSize
otherAttributes:(NSDictionary *)otherAttributes; //属性字符串 指定子串变色
- (NSMutableAttributedString *)attributedStringWithcolor:(UIColor *)color subString:(NSString *)subString; // 属性字符串 指定子串变色 多处
- (NSMutableAttributedString *)attributedStringWithColorArray:(NSArray *)colorArray subStringArray:(NSArray *)subStringArray; //属性字符串 指定子串改变显示效果
- (NSMutableAttributedString *)attributedStringWithAttributeds:(NSDictionary *)attributeds subString:(NSString *)subString;
/**
转变指定字符串 @param regArray 正则表达式数组
@param attributeds 指定属性
@return 返回的可变字符串
*/
- (NSMutableAttributedString *)changeStringWithReg:(NSArray *)regArray attributeds:(NSDictionary *)attributeds; // 将url中的参数转为字典
- (NSMutableDictionary *)getURLParameters; /**
插入图片 @param images 图片数组
@param font 字体
@param span 间距
@return 结果
*/
-(NSMutableAttributedString *)attributedStringWithImages:(NSArray<UIImage *> *)images font:(UIFont *)font imageSpan:(CGFloat)span; @end

  

#import "NSString+Extension.h"

@implementation NSString (Extension)

- (CGFloat)heightWithLimitWidth:(CGFloat)width
fontSize:(CGFloat)fontSize
otherAttributes:(NSDictionary *)otherAttributes { return [self rectWithSize:CGSizeMake(width, CGFLOAT_MAX)
fontSize:fontSize
otherAttributes:otherAttributes].size.height;
} - (CGFloat)widthWithLimitHeight:(CGFloat)height
fontSize:(CGFloat)fontSize
otherAttributes:(NSDictionary *)otherAttributes { return [self rectWithSize:CGSizeMake(CGFLOAT_MAX, height)
fontSize:fontSize
otherAttributes:otherAttributes].size.width;
} - (CGSize)sizeWithLimitSize:(CGSize)size
fontSize:(CGFloat)fontSize
otherAttributes:(NSDictionary *)otherAttributes { return [self rectWithSize:size
fontSize:fontSize
otherAttributes:otherAttributes].size;
} - (CGRect)rectWithSize:(CGSize)size
fontSize:(CGFloat)fontSize
otherAttributes:(NSDictionary *)otherAttributes { NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithDictionary:@{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]}];
if (otherAttributes) {
for (NSString *aKey in otherAttributes.allKeys) {
id aValue = [otherAttributes objectForKey:aKey];
[dictionary setObject:aValue forKey:aKey];
}
} return [self boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:dictionary context:nil];
} - (NSMutableAttributedString *)attributedStringWithcolor:(UIColor *)color subString:(NSString *)subString
{
NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc] initWithString:self];
if (![subString isKindOfClass:[NSString class]] || !subString.length) {
return attributedString;
} NSRange range = [self rangeOfString:subString];
if (range.location != NSNotFound) {
[attributedString addAttribute:NSForegroundColorAttributeName value:color range:NSMakeRange(range.location, range.length)];
}
return attributedString;
} - (NSMutableAttributedString *)attributedStringWithColorArray:(NSArray *)colorArray subStringArray:(NSArray *)subStringArray
{
NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc] initWithString:self];
NSInteger count = subStringArray.count;
if (count<2) {
return [self attributedStringWithcolor:colorArray[0] subString:subStringArray[0]];
} for (NSInteger i=0; i<count; i++) {
NSString *subString= subStringArray[i];
if (![subString isKindOfClass:[NSString class]] || !subString.length) {
return attributedString;
} NSRange range = [self rangeOfString:subString];
if (range.location != NSNotFound) {
[attributedString addAttribute:NSForegroundColorAttributeName value:colorArray[i] range:NSMakeRange(range.location, range.length)];
}
} return attributedString;
} - (NSMutableAttributedString *)attributedStringWithAttributeds:(NSDictionary *)attributeds subString:(NSString *)subString{ NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc] initWithString:self];
if (![subString isKindOfClass:[NSString class]] || !subString.length) {
return attributedString;
} NSRange range = [self rangeOfString:subString];
if (range.location != NSNotFound) { // [attributedString addAttribute:NSForegroundColorAttributeName value:color range:NSMakeRange(range.location, range.length)];
[attributedString addAttributes:attributeds range:NSMakeRange(range.location, range.length)];
}
return attributedString;
} /**
转变指定字符串 @param regArray 正则表达式数组
@param attributeds 指定属性
@return 返回的可变字符串
*/
- (NSMutableAttributedString *)changeStringWithReg:(NSArray *)regArray attributeds:(NSDictionary *)attributeds{ NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:self];
for (int i=0; i<regArray.count; i++) { NSString *re = [regArray ty_ObjectWithIndex:i];
NSRegularExpression *regExp = [[NSRegularExpression alloc]initWithPattern:re
options:NSRegularExpressionDotMatchesLineSeparators
error:nil]; NSArray* match = [regExp matchesInString:self options:NSMatchingReportCompletion range:NSMakeRange(0, [self length])]; if (match.count != 0)
{
for (NSTextCheckingResult *matc in match)
{
NSRange range = [matc range]; [attr addAttributes:attributeds range:range];
}
} } return attr;
}
/**
插入图片 @param images 图片数组
@param font 字体
@param span 间距
@return 结果
*/
-(NSMutableAttributedString *)attributedStringWithImages:(NSArray<UIImage *> *)images font:(UIFont *)font imageSpan:(CGFloat)span{ NSMutableAttributedString *textAttrStr = [[NSMutableAttributedString alloc] init]; for (UIImage *img in images) { NSTextAttachment *attach = [[NSTextAttachment alloc] init];
attach.image = img;
CGFloat imgH = font.pointSize;
CGFloat imgW = (img.size.width / img.size.height) * imgH;
attach.bounds = CGRectMake(0, -2 , imgW, imgH); NSAttributedString *imgStr = [NSAttributedString attributedStringWithAttachment:attach];
[textAttrStr appendAttributedString:imgStr];
//标签后添加空格
[textAttrStr appendAttributedString:[[NSAttributedString alloc] initWithString:@" "]];
} [textAttrStr appendAttributedString:[[NSAttributedString alloc]initWithString:self]];
//设置间距
if (span != 0) {
[textAttrStr addAttribute:NSKernAttributeName value:@(span)
range:NSMakeRange(0, images.count * 2/*由于图片也会占用一个单位长度,所以带上空格数量,需要 *2 */)];
} return textAttrStr;
} - (NSMutableDictionary *)getURLParameters { // 查找参数
NSRange range = [self rangeOfString:@"?"];
if (range.location == NSNotFound) {
return nil;
} NSMutableDictionary *params = [NSMutableDictionary dictionary]; // 截取参数
NSString *parametersString = [self substringFromIndex:range.location + 1]; // 判断参数是单个参数还是多个参数
if ([parametersString containsString:@"&"]) { // 多个参数,分割参数
NSArray *urlComponents = [parametersString componentsSeparatedByString:@"&"]; for (NSString *keyValuePair in urlComponents) {
// 生成Key/Value
NSArray *pairComponents = [keyValuePair componentsSeparatedByString:@"="];
NSString *key = [pairComponents.firstObject stringByRemovingPercentEncoding];
NSString *value = [pairComponents.lastObject stringByRemovingPercentEncoding]; // Key不能为nil
if (key == nil || value == nil) {
continue;
} id existValue = [params valueForKey:key]; if (existValue != nil) { // 已存在的值,生成数组
if ([existValue isKindOfClass:[NSArray class]]) {
// 已存在的值生成数组
NSMutableArray *items = [NSMutableArray arrayWithArray:existValue];
[items addObject:value]; [params setValue:items forKey:key];
} else { // 非数组
[params setValue:@[existValue, value] forKey:key];
} } else { // 设置值
[params setValue:value forKey:key];
}
}
} else {
// 单个参数 // 生成Key/Value
NSArray *pairComponents = [parametersString componentsSeparatedByString:@"="]; // 只有一个参数,没有值
if (pairComponents.count == 1) {
return nil;
} // 分隔值
NSString *key = [pairComponents.firstObject stringByRemovingPercentEncoding];
NSString *value = [pairComponents.lastObject stringByRemovingPercentEncoding]; // Key不能为nil
if (key == nil || value == nil) {
return nil;
} // 设置值
[params setValue:value forKey:key];
} return params;
} @end

  

最新文章

  1. Ext之ExtGrid增删改查询回顾总结
  2. NFA转DFA - json数字识别
  3. 解决VS下“LC.exe已退出,代码为-1”问题
  4. RequireJS实例分析【转】
  5. php iquery
  6. Spring +SpringMVC 实现文件上传功能。。。
  7. jQuery的如何捕捉回车键,改变事件标签
  8. Android开发之MediaPlayer和SurfaceView组成视频播放器
  9. svg转换工具
  10. DotNET知识点总结三(笔记整合)
  11. 恢复Linux下被误删除的文件(笔记)
  12. Apache Commons Pool 故事一则
  13. jquery阻止默认滑动
  14. [Unity Shader]光照模型对物体的假设
  15. Java 基础知识总结 2
  16. 手机app微信支付后台处理流程
  17. [20180822]session_cached_cursors与子游标堆0.txt
  18. AI R-CNN目标检测算法
  19. Flask 框架
  20. 排序(Sort)-----冒泡排序

热门文章

  1. 使用RecyclerView
  2. JS性能分析(测试代码运行时间)
  3. Python2和Python3除法
  4. AttributeError: &#39;tuple&#39; object has no attribute &#39;extend&#39;
  5. VC++检测硬件设备状态
  6. Google Chrome浏览器调试
  7. js【table】合并行,合并列
  8. JAVA基础——设计模式之装饰者模式
  9. Python机器学习——Agglomerative层次聚类
  10. [Luogu] P1131 [ZJOI2007]时态同步