#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

  

ios计算字符串宽高,指定字符串变色,获取URL参数集合的更多相关文章

  1. String的两个API,判断指定字符串是否包含另一字符串,在字符串中删除指定字符串。

    // 在字符串中删除指定字符串. String phoneNum="1795112345"; phoneNum = phoneNum.replace("17951&quo ...

  2. java查找字符串里与指定字符串相同的个数

    public class EmployeeDemo { //方法一: public int search(String str,String strRes) {//查找字符串里与指定字符串相同的个数 ...

  3. iOS xib View宽高不能改变

    IOS - xib(Interface Builder,view) - can't change view size(view不能改变大小问题) 今天在试着swift语言写个demo,,当中遇到了这个 ...

  4. iOS - web自适应宽高(预设置的大小)

    //web自适应宽高 -(void)webViewDidFinishLoad:(UIWebView *)webView { NSLog(@"wessd"); [ webView s ...

  5. Swift计算文本宽高

    iOS 8 开始可以配合 AutoLayout 自动估算文本的高度,但是当 Cell 比较复杂的时候,还会需要手动去计算.首先声明一个样式 var TextStyle : [String : NSOb ...

  6. Js删除字符串中的指定字符串

    案例一. 比如:原字符串 var StringFirst = "12:30:08"; 现在要删掉冒号,变成123008 就可以先split var splitFirst = Str ...

  7. js判断字符串是否以指定字符串开头或是否包含指定字符串

    1.  用js判断一个字符串是否是以某个子字符串开头如:ssss001是否以ssss开头, 可以这样做: 1 2 3 4 5 6 var fdStart = strCode.indexOf(" ...

  8. iOS计算字符串的宽度高度

    OC开发中会遇到根据字符串和字体大小来算计算出字符串所占的宽高->> 封装方法如下: #import <Foundation/Foundation.h> #import < ...

  9. 【IOS 开发】Objective-C Foundation 框架 -- 字符串 | 日期 | 对象复制 | NSArray | NSSet | NSDictionary | 谓词

    一. 字符串 API 1. NSString 用法简介 (1) NSString API 介绍 NSString 功能 : -- 创建字符串 : 使用 init 开头的实例方法, 也可以使用 Stri ...

随机推荐

  1. 最新WIN10系统32位和64位纯净版自动激活版1010074 V2015年

    系统来自:系统妈 本系统定位于个人在家庭.网吧.办公环境使用,采用久经考验的精简方法和体积压缩技术,在小巧体积中提供了几乎100%的原版Win10兼容性.经过在多个版本的更新和升级过程后,已经被证明能 ...

  2. 番茄花园Ghost Win10系统X64位10041装机版2015年4月

    转载:系统妈,系统下载地址:http://www.xitongma.com/windows10/2015-04-01/6639.html 番茄花园Ghost Win10系统X64位10041装机版20 ...

  3. C/C++ 标准输入、输出

    一.分类 1.标准输入输出 键盘输入,显示器输出.2.文件输入输出 以外存为对象,即硬盘.光盘等.3.串输入输出 对内存中指定空间进行输入输出. 二.c语言中的输入输出 #include <st ...

  4. codeforces_1066_B.Heaters

    题意:一个数组只含有0或1,1表示该元素可以覆盖其自身.左边r-1个元素和右边r-1个元素,问最少保留多少个1元素可以覆盖整个数组. 思路:一个指针指向当前未被覆盖的最左边的元素下标,每次找离它最远且 ...

  5. ANALYZE - 收集与数据库有关的统计

    SYNOPSIS ANALYZE [ VERBOSE ] [ table [ (column [, ...] ) ] ] DESCRIPTION 描述 ANALYZE 收集有关 PostgreSQL ...

  6. cookie和session的用法用途,执行流程,区别联系

    1.为什么要有cookie/session?在客户端浏览器向服务器发送请求,服务器做出响应之后,二者便会断开连接(一次会话结束).那么下次用户再来请求服务器,服务器没有任何办法去识别此用户是谁.比如w ...

  7. Java基础(十一)--Serializable和Externalizable接口实现序列化

    序列化在日常开发中经常用到,特别是涉及到网络传输的时候,例如调用第三方接口,通过一个约定好的实体进行传输,这时你必须实现序列 化,这些都是大家都了解的内容,所以文章也会讲一下序列化的高级内容. 序列化 ...

  8. 获取汉字的拼音首字母--pinyin

    var pinyin = (function (){ var Pinyin = function (ops){ this.initialize(ops); }, options = { checkPo ...

  9. 2.10.2 section元素

    section元素 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> & ...

  10. 关于idea的目录结构如何变成树状,也就是横向变纵向

    横向 竖向 方法: