目录
- NSString
- UILabel
- UIView
- UIViewController
- NSObject
- UIImage
NSString
+ (CGFloat)calculateTextHeight:(NSString *)text width:(CGFloat)width font:(NSInteger)font {
CGSize size = CGSizeMake(width, MAXFLOAT);
NSDictionary * fontDic = @{NSFontAttributeName:[UIFont systemFontOfSize:font]};
CGRect rect = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:fontDic context:nil];
return rect.size.height;
}
+ (CGFloat)calculateTextWidth:(NSString *)text height:(CGFloat)height font:(NSInteger)font {
CGSize size = CGSizeMake(MAXFLOAT, height);
NSDictionary * fontDic = @{NSFontAttributeName:[UIFont systemFontOfSize:font]};
CGRect rect = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:fontDic context:nil];
return rect.size.width;
}
+ (NSArray *)getSeparatedLines:(NSString *)text width:(CGFloat)width font:(UIFont *)font {
CTFontRef myFont = CTFontCreateWithName((__bridge CFStringRef)([font fontName]), [font pointSize], NULL);
NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:text];
[attStr addAttribute:(NSString *)kCTFontAttributeName value:(__bridge id)myFont range:NSMakeRange(0, attStr.length)];
CTFramesetterRef frameSetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)attStr);
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRect(path, NULL, CGRectMake( 0, 0, width, MAXFLOAT));
CTFrameRef frame = CTFramesetterCreateFrame(frameSetter, CFRangeMake(0, 0), path, NULL);
NSArray *lines = (__bridge NSArray *)CTFrameGetLines(frame);
NSMutableArray *linesArray = [[NSMutableArray alloc]init];
for (id line in lines) {
CTLineRef lineRef = (__bridge CTLineRef )line;
CFRange lineRange = CTLineGetStringRange(lineRef);
NSRange range = NSMakeRange(lineRange.location, lineRange.length);
NSString *lineString = [text substringWithRange:range];
[linesArray addObject:lineString];
}
return (NSArray *)linesArray;
}
+ (NSString *)deletePunctuation:(NSString *)text {
// 正则表达式
NSString *regex = @"[a-zA-Z0-9\u4e00-\u9fa5]+";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
NSString * string = @"";
for (NSInteger i = 0; i < text.length; i ++) {
NSString * str = [text substringWithRange:NSMakeRange(i, 1)];
if ([pred evaluateWithObject:str]) string = [NSString stringWithFormat:@"%@%@", string, str];
}
return string;
}
+ (NSString *)transform:(NSString *)chinese {
NSMutableString * pinyin = [chinese mutableCopy];
CFStringTransform((__bridge CFMutableStringRef)pinyin, NULL, kCFStringTransformMandarinLatin, NO);
CFStringTransform((__bridge CFMutableStringRef)pinyin, NULL, kCFStringTransformStripCombiningMarks, NO); // 不要音标
return [pinyin uppercaseString];
}
UILabel
+ (void)increaseLineSpace:(UILabel *)label and:(CGFloat)space {
NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc] initWithString:label.text];
NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineSpacing:space];
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [label.text length])];
[label setAttributedText:attributedString];
[label sizeToFit];
}
// 指定string
// arr示例:@[@[@"string", @"color", @"font"], @[], @[]]
+ (void)customColorAndFontOfLabel_String:(UILabel *)label andString:(NSArray *)arr {
NSMutableAttributedString *noteStr = [[NSMutableAttributedString alloc] initWithString:label.text];
[arr enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
// 设置区域
NSRange range = NSMakeRange([[noteStr string] rangeOfString:arr[idx][0]].location, [arr[idx][0] length]);
// 颜色
[noteStr addAttribute:NSForegroundColorAttributeName value:arr[idx][1] range:range];
// 字体大小
[noteStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:[arr[idx][2] floatValue]] range:range];
[label setAttributedText:noteStr];
}];
}
// 指定范围
// arr示例:@[@[@"range", @"color", @"font"], @[], @[]]
+ (void)customColorAndFontOfLabel_Range:(UILabel *)label andString:(NSArray *)arr {
NSString * string = label.text;
for (NSMutableArray * array in arr) {
NSString * resultStr = [string substringWithRange:[array[0] rangeValue]];
[array replaceObjectAtIndex:0 withObject:resultStr];
}
[self customColorAndFontOfLabel_String:label andString:arr];
}
UIView
- (UIViewController *)superVC {
for (UIView * next = [self superview]; next; next = next.superview) {
UIResponder* nextResponder = [next nextResponder];
if ([nextResponder isKindOfClass:[UIViewController class]]) {
return (UIViewController *)nextResponder;
}
}
return nil;
}
UIViewController
+ (UIViewController *)getCurrentVC
{
UIViewController * result = nil;
UIWindow * window = [[UIApplication sharedApplication] keyWindow];
if (window.windowLevel != UIWindowLevelNormal)
{
NSArray * windows = [[UIApplication sharedApplication] windows];
for(UIWindow * tmpWin in windows)
{
if (tmpWin.windowLevel == UIWindowLevelNormal)
{
window = tmpWin;
break;
}
}
}
UIView * frontView = [[window subviews] objectAtIndex:0];
id nextResponder = [frontView nextResponder];
if ([nextResponder isKindOfClass:[UIViewController class]])
result = nextResponder;
else
result = window.rootViewController;
return result;
}
NSObject
+ (NSString *)platform {
struct utsname systemInfo;
uname(&systemInfo);
NSString *platform = [NSString stringWithCString:systemInfo.machine encoding:NSASCIIStringEncoding];
if ([platform isEqualToString:@"iPhone1,1"]) return @"iPhone 2G";
if ([platform isEqualToString:@"iPhone1,2"]) return @"iPhone 3G";
if ([platform isEqualToString:@"iPhone2,1"]) return @"iPhone 3GS";
if ([platform isEqualToString:@"iPhone3,1"]) return @"iPhone 4";
if ([platform isEqualToString:@"iPhone3,2"]) return @"iPhone 4";
if ([platform isEqualToString:@"iPhone3,3"]) return @"iPhone 4";
if ([platform isEqualToString:@"iPhone4,1"]) return @"iPhone 4S";
if ([platform isEqualToString:@"iPhone5,1"]) return @"iPhone 5";
if ([platform isEqualToString:@"iPhone5,2"]) return @"iPhone 5";
if ([platform isEqualToString:@"iPhone5,3"]) return @"iPhone 5c";
if ([platform isEqualToString:@"iPhone5,4"]) return @"iPhone 5c";
if ([platform isEqualToString:@"iPhone6,1"]) return @"iPhone 5s";
if ([platform isEqualToString:@"iPhone6,2"]) return @"iPhone 5s";
if ([platform isEqualToString:@"iPhone7,1"]) return @"iPhone 6 Plus";
if ([platform isEqualToString:@"iPhone7,2"]) return @"iPhone 6";
if ([platform isEqualToString:@"iPhone8,1"]) return @"iPhone 6s";
if ([platform isEqualToString:@"iPhone8,2"]) return @"iPhone 6s Plus";
if ([platform isEqualToString:@"iPhone8,4"]) return @"iPhone SE";
if ([platform isEqualToString:@"iPhone9,1"]) return @"iPhone 7";
if ([platform isEqualToString:@"iPhone9,2"]) return @"iPhone 7 Plus";
if ([platform isEqualToString:@"iPhone10,1"]) return @"iPhone 8";
if ([platform isEqualToString:@"iPhone10,2"]) return @"iPhone 8 Plus";
if ([platform isEqualToString:@"iPod1,1"]) return @"iPod Touch 1G";
if ([platform isEqualToString:@"iPod2,1"]) return @"iPod Touch 2G";
if ([platform isEqualToString:@"iPod3,1"]) return @"iPod Touch 3G";
if ([platform isEqualToString:@"iPod4,1"]) return @"iPod Touch 4G";
if ([platform isEqualToString:@"iPod5,1"]) return @"iPod Touch 5G";
if ([platform isEqualToString:@"iPod7,1"]) return @"iPod Touch 6G";
if ([platform isEqualToString:@"iPad1,1"]) return @"iPad 1G";
if ([platform isEqualToString:@"iPad2,1"]) return @"iPad 2";
if ([platform isEqualToString:@"iPad2,2"]) return @"iPad 2";
if ([platform isEqualToString:@"iPad2,3"]) return @"iPad 2";
if ([platform isEqualToString:@"iPad2,4"]) return @"iPad 2";
if ([platform isEqualToString:@"iPad2,5"]) return @"iPad Mini 1G";
if ([platform isEqualToString:@"iPad2,6"]) return @"iPad Mini 1G";
if ([platform isEqualToString:@"iPad2,7"]) return @"iPad Mini 1G";
if ([platform isEqualToString:@"iPad3,1"]) return @"iPad 3";
if ([platform isEqualToString:@"iPad3,2"]) return @"iPad 3";
if ([platform isEqualToString:@"iPad3,3"]) return @"iPad 3";
if ([platform isEqualToString:@"iPad3,4"]) return @"iPad 4";
if ([platform isEqualToString:@"iPad3,5"]) return @"iPad 4";
if ([platform isEqualToString:@"iPad3,6"]) return @"iPad 4";
if ([platform isEqualToString:@"iPad4,1"]) return @"iPad Air";
if ([platform isEqualToString:@"iPad4,2"]) return @"iPad Air";
if ([platform isEqualToString:@"iPad4,3"]) return @"iPad Air";
if ([platform isEqualToString:@"iPad4,4"]) return @"iPad Mini 2G";
if ([platform isEqualToString:@"iPad4,5"]) return @"iPad Mini 2G";
if ([platform isEqualToString:@"iPad4,6"]) return @"iPad Mini 2G";
if ([platform isEqualToString:@"iPad4,7"]) return @"iPad Mini 3G";
if ([platform isEqualToString:@"iPad4,8"]) return @"iPad Mini 3G";
if ([platform isEqualToString:@"iPad4,9"]) return @"iPad Mini 3G";
if ([platform isEqualToString:@"iPad5,1"]) return @"iPad Mini 4G";
if ([platform isEqualToString:@"iPad5,2"]) return @"iPad Mini 4G";
if ([platform isEqualToString:@"iPad5,3"]) return @"iPad Air 2";
if ([platform isEqualToString:@"iPad5,4"]) return @"iPad Air 2";
if ([platform isEqualToString:@"iPad6,3"]) return @"iPad Pro (9.7 inch)";
if ([platform isEqualToString:@"iPad6,4"]) return @"iPad Pro (9.7 inch)";
if ([platform isEqualToString:@"iPad6,7"]) return @"iPad Pro (12.9 inch)";
if ([platform isEqualToString:@"iPad6,8"]) return @"iPad Pro (12.9 inch)";
if ([platform isEqualToString:@"i386"]) return @"iPhone Simulator";
if ([platform isEqualToString:@"x86_64"]) return @"iPhone Simulator";
return platform;
}
UIImage
typedef enum : NSUInteger {
ImageToBase64CompressionTypeNormal = 0, // 默认不压缩
ImageToBase64CompressionType100K, // 压缩到100k内
} ImageToBase64CompressionType;
/**
图片转base64
@param image 原图
@param haveHead base64是否带头部信息,如(data:image/jpg;base64,)
@param type 压缩类型(仅在jpeg时有效)
@return 压缩后的base64字符串
*/
+ (NSString *)imageToBase64:(UIImage *)image
HaveHead:(BOOL)haveHead
Compression:(ImageToBase64CompressionType)type {
NSData *imageData = nil;
NSString *mimeType = nil;
if ([self imageHasAlpha: image]) {
imageData = UIImagePNGRepresentation(image);
mimeType = @"image/png";
}
else {
switch (type) {
case ImageToBase64CompressionTypeNormal:
imageData = UIImageJPEGRepresentation(image, 1.0);
break;
case ImageToBase64CompressionType100K:
imageData = [self imageData:image];
default:
break;
}
mimeType = @"image/jpeg";
}
if (haveHead) {
NSString * base64 = [NSString stringWithFormat:@"data:%@;base64,%@", mimeType, [imageData base64EncodedStringWithOptions:0]];
return base64;
}
NSString * base64 = [imageData base64EncodedStringWithOptions:0]; // 不包含图片头,如(data:image/jpg;base64,)
base64 = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL,(CFStringRef)base64,NULL,(CFStringRef)@"!*'();:@&=+$,/?%#[]",kCFStringEncodingUTF8 ));
return base64;
}
// 压缩到100k内
+(NSData *)imageData:(UIImage *)myimage {
NSData *data=UIImageJPEGRepresentation(myimage, 1.0);
if (data.length>100*1024) {
if (data.length>1024*1024) {
//1M以及以上
data=UIImageJPEGRepresentation(myimage, 0.1);
}else if (data.length>512*1024) {
//0.5M-1M
data=UIImageJPEGRepresentation(myimage, 0.5);
}else if (data.length>200*1024) {
//0.25M-0.5M
data=UIImageJPEGRepresentation(myimage, 0.9);
}
}
return data;
}
// 根据alpha判断图片格式
+ (BOOL) imageHasAlpha: (UIImage *) image {
CGImageAlphaInfo alpha = CGImageGetAlphaInfo(image.CGImage);
return (alpha == kCGImageAlphaFirst ||
alpha == kCGImageAlphaLast ||
alpha == kCGImageAlphaPremultipliedFirst ||
alpha == kCGImageAlphaPremultipliedLast);
}