123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467 |
- //
- // GlobalMethod+UI.m
- // 乐销
- //
- // Created by 隋林栋 on 2016/12/16.
- // Copyright © 2016年 ping. All rights reserved.
- //
- #import "GlobalMethod+UI.h"
- //数据大小
- #import <limits.h>
- @implementation GlobalMethod (UI)
- #pragma mark 计算label size
- + (CGFloat)fetchHeightFromLabel:(UILabel *)label{
- return [self fetchHeightFromLabel:label heightLimit:10000];
- }
- + (CGFloat)fetchHeightFromLabel:(UILabel *)label heightLimit:(CGFloat )height{
- if (label == nil) {
- return 0;
- }
- NSAttributedString * attributeString = [[NSAttributedString alloc]initWithString:!isStr(label.text)? @"A":label.text attributes:@{NSFontAttributeName: label.font}];
- CGRect rect =[attributeString boundingRectWithSize:CGSizeMake(label.width, height) options:NSStringDrawingUsesLineFragmentOrigin context:nil];
- CGFloat num_height_return = rect.size.height+1;
- //限制行数
- if (label.numberOfLines != 0) {
- attributeString = [[NSAttributedString alloc]initWithString:@"A" attributes:@{NSFontAttributeName: label.font}];
- rect =[attributeString boundingRectWithSize:CGSizeMake(label.width, height) options:NSStringDrawingUsesLineFragmentOrigin context:nil];
- num_height_return = num_height_return >label.numberOfLines*rect.size.height?label.numberOfLines*rect.size.height:num_height_return;
- }
- return ceil(num_height_return) ;
- }
- + (CGFloat)fetchWidthFromLabel:(UILabel *)label{
- NSString * strContent = label.text == nil ? @"":label.text;
-
- UIFont * font = label.font;
- NSAttributedString * attributeString = [[NSAttributedString alloc]initWithString:strContent attributes:@{NSFontAttributeName: font}];
- CGRect rect =[attributeString boundingRectWithSize:CGSizeMake(1000, 1000) options:NSStringDrawingUsesLineFragmentOrigin context:nil];
- // NSLog(@"ceil Before: %lf",rect.size.width);
- return ceil(rect.size.width);
- }
- + (CGFloat)fetchWidthFromButton:(UIButton *)btn{
- UILabel * label = [UILabel new];
- label.font = btn.titleLabel.font;
- label.text = btn.titleLabel.text;
- return [self fetchWidthFromLabel:label];
- }
- //设置label
- + (void)setLabel:(UILabel *)label
- widthLimit:(CGFloat )widthLimit
- numLines:(NSInteger)numLines
- fontNum:(CGFloat)fontNum
- textColor:(UIColor *)textColor
- aligent:(NSTextAlignment )aligent
- text:(NSString *)text
- bgColor:(UIColor *)color{
- label.numberOfLines = numLines;
- label.font = [UIFont systemFontOfSize:fontNum];
- label.textColor = textColor == nil? COLOR_LABEL : textColor;
- label.textAlignment = aligent;
- label.backgroundColor = color == nil?[UIColor clearColor]:color;
- label.text = UnPackStr(text);
- CGFloat widthMAX= [self fetchWidthFromLabel:label];
- // NSLog(@"widthMAX : %lf",widthMAX);
- if (widthLimit != 0 ) {
- if (widthMAX < widthLimit) {
- label.width = widthMAX;
- }else {
- label.width = widthLimit;
- }
- }else {
- label.width = widthMAX;
- }
- label.height = [self fetchHeightFromLabel:label];
- }
- + (void)setLabel:(UILabel *)label
- widthLimit:(CGFloat )widthLimit
- numLines:(NSInteger)numLines
- fontNum:(CGFloat)fontNum
- textColor:(UIColor *)textColor
- text:(NSString *)text{
- [self setLabel:label widthLimit:widthLimit numLines:numLines fontNum:fontNum textColor:textColor aligent:label.textAlignment text:text bgColor:[UIColor clearColor]];
- }
- + (void)resetLabel:(UILabel *)label
- attributeString:(NSAttributedString *)text
- widthLimit:(CGFloat )widthLimit{
- label.attributedText = text;
- CGRect rect =[text boundingRectWithSize:CGSizeMake(widthLimit, 1000) options:NSStringDrawingUsesLineFragmentOrigin context:nil];
- label.width = MIN(CGRectGetWidth(rect), widthLimit?:CGFLOAT_MAX);
- //限制行数
- label.height = [self fetchHeightFromLabel:label heightLimit:CGFLOAT_MAX];
- // label.height = rect.size.height+1;
- }
- + (CGFloat)fetchHeightFromFont:(NSInteger)fontNum{
- NSAttributedString *attributeString = [[NSAttributedString alloc]initWithString:@"A" attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:fontNum]}];
- CGRect rect =[attributeString boundingRectWithSize:CGSizeMake(INTMAX_MAX, INTMAX_MAX) options:NSStringDrawingUsesLineFragmentOrigin context:nil];
- return rect.size.height;
- }
- //设置圆角
- + (void)setRoundView:(UIView *)iv color:(UIColor *)color
- {
- [self setRoundView:iv color:color numRound:4 width:4];
- }
- //设置圆角
- + (void)setRoundView:(UIView *)iv color:(UIColor *)color numRound:(CGFloat)numRound width:(CGFloat)width
- {
- iv.layer.cornerRadius = numRound;//圆角设置
- iv.layer.masksToBounds = YES;
- [iv.layer setBorderWidth:width];
- iv.layer.borderColor = color.CGColor;
- }
- //设置textfield左间距
- + (void)setTextFileLeftPadding:(UITextField *)ut leftPadding:(float)leftPadding{
- CGRect frame = ut.frame;
- frame.size.width = leftPadding;
- UIView *leftV = [[UIView alloc] initWithFrame:frame];
- ut.leftViewMode = UITextFieldViewModeAlways;
- ut.leftView = leftV;
- }
- //设置日期格式
- + (NSString *)exchangeDate:(NSDate *)date formatter:(NSString *)formate{
- if (date == nil || formate == nil) {
- return @"";
- }
-
- NSMutableDictionary *threadDictionary = [[NSThread currentThread] threadDictionary];
- NSDateFormatter *dateFormatter = threadDictionary[@"myDateFormatter"];
- if(!dateFormatter){
- @synchronized(self){
- if(!dateFormatter){
- dateFormatter = [[NSDateFormatter alloc] init];
- [dateFormatter setAMSymbol:@"上午"];
- [dateFormatter setPMSymbol:@"下午"];
- threadDictionary[@"myDateFormatter"] = dateFormatter;
- }
- }
- }
- // 必填字段
- // static NSDateFormatter *dateFormatter = nil;
- // if (dateFormatter == nil) {
- // dateFormatter = [[NSDateFormatter alloc] init];
- // [dateFormatter setAMSymbol:@"上午"];
- // [dateFormatter setPMSymbol:@"下午"];
- // }
- [dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];//解决8小时时间差问题
- //设定时间格式,这里可以设置成自己需要的格式
- [dateFormatter setDateFormat:formate];
- //用[NSDate date]可以获取系统当前时间
- NSString *currentDateStr = [dateFormatter stringFromDate:date];
- return currentDateStr;
- }
- //设置日期格式
- + (NSDate *)exchangeStringToDate:(NSString *)string formatter:(NSString *)formate{
- if (!isStr(string) || !isStr(formate)) {
- return nil;
- }
- NSMutableDictionary *threadDictionary = [[NSThread currentThread] threadDictionary];
- NSDateFormatter *dateFormatter = threadDictionary[@"myStrToDateformatter"];
- if(!dateFormatter){
- @synchronized(self){
- if(!dateFormatter){
- dateFormatter = [[NSDateFormatter alloc] init];
- threadDictionary[@"myStrToDateformatter"] = dateFormatter;
- }
- }
- }
- // // 必填字段
- // static NSDateFormatter *dateFormatter = nil;
- // if (dateFormatter == nil) {
- // dateFormatter = [[NSDateFormatter alloc] init];
- // }
- //设定时间格式,这里可以设置成自己需要的格式
- [dateFormatter setDateFormat:formate];
- //用[NSDate date]可以获取系统当前时间
- return [dateFormatter dateFromString:string];
- }
- //转换日期格式
- + (NSString *)exchangeString:(NSString *)string fromFormatter:(NSString *)formateFrom toFormatter:(NSString *)formateTo{
- if (!isStr(string)) {
- return @"";
- }
- NSDate * date= [self exchangeStringToDate:string formatter:formateFrom];
- if (date) {
- return [self exchangeDate:date formatter:formateTo];
- }
- return @"";
- }
- + (NSDate *)exchangeString:(NSString *)str formatter:(NSString *)formate{
- if (str == nil || formate == nil) {
- return [NSDate date];
- }
- NSMutableDictionary *threadDictionary = [[NSThread currentThread] threadDictionary];
- NSDateFormatter *dateFormatter = threadDictionary[@"myStrDateFormatter"];
- if(!dateFormatter){
- @synchronized(self){
- if(!dateFormatter){
- dateFormatter = [[NSDateFormatter alloc] init];
- threadDictionary[@"myStrDateFormatter"] = dateFormatter;
- }
- }
- }
- // 必填字段
- // static NSDateFormatter *dateFormatter = nil;
- //
- // if (dateFormatter == nil) {
- // dateFormatter = [[NSDateFormatter alloc] init];
- // }
-
- //设定时间格式,这里可以设置成自己需要的格式
- [dateFormatter setDateFormat:formate];
- //用[NSDate date]可以获取系统当前时间
- NSDate *currentDate = [dateFormatter dateFromString:str];
- return currentDate;
- }
- + (NSString *)exchangeDateStringResponse:(NSString *)str formatter:(NSString *)formate{
- if (!isStr(str)) {
- return @"";
- }
- NSRange range = [str rangeOfString:@"0001"];
- if (range.location != NSNotFound) {
- return @"";
- }
- range = [str rangeOfString:@"."];
- if (range.length > 0) {
- str = [str substringToIndex:range.location];
- }
- // NSArray * aryDate = [str componentsValidSeparatedByString:@"T"];
- // NSDate * dateYear = [self exchangeString:aryDate.firstObject formatter:@"yyyy-MM-dd"];
- // NSDate * dateHour = [self exchangeString:aryDate.lastObject formatter:@"HH-mm-ss"];
- // NSString * strDate = [NSString stringWithFormat:@"%@ %@",[self exchangeDate:dateYear formatter:@"yyyy-MM-dd"],[self exchangeDate:dateHour formatter:@"HH-mm-ss"]];
- NSDate * dateRight = [self exchangeString:str formatter:@"yyyy-MM-dd HH-mm-ss"];
- NSString * strReturn = [self exchangeDate:dateRight formatter:formate];
-
- return isStr(strReturn)?strReturn:str;
- }
- //获取当前时间戳
- + (NSString *)fetchTimeStamp{
- return [NSString stringWithFormat:@"%.f",[[NSDate date] timeIntervalSince1970]];
- }
- //获取时间
- + (NSString *)fetchTimeStampWithStr:(NSString *)str formatter:(NSString *)formatter{
- NSDate *date1 = [NSDate dateWithTimeIntervalSince1970:[str doubleValue]/1000+3600*8];
- return [self exchangeDate:date1 formatter:formatter];
- }
- #pragma mark 比较两个时间戳相差天数,takeCarTime:传的时间
- + (NSString *)getHour:(NSString *)takeCarTime systemTime:(NSString *)systemTime
- {
- NSDate *DRstartDate = [NSDate dateWithTimeIntervalSince1970:[takeCarTime doubleValue]/1000+3600*8];
- NSDate *DRendDate = [NSDate dateWithTimeIntervalSince1970:[systemTime doubleValue]+3600*8];
- NSCalendar *gregorian = [[ NSCalendar alloc ] initWithCalendarIdentifier : NSCalendarIdentifierGregorian];
- NSUInteger unitFlags = NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
- NSDateComponents *components = [gregorian components:unitFlags fromDate:DRendDate toDate:DRstartDate options:0];
-
- // 获得某个时间的年月日时分秒
- // NSDateComponents *createDateCmps = [calendar components:unit fromDate:DRstartDate];
- // NSDateComponents *nowCmps = [calendar components:unit fromDate:DRendDate];
- NSLog(@"剩余%ld天,%ld小时%ld分", components.day ,components.hour, components.minute);
- NSLog(@"相差%ld小时",components.hour);
- if (components.day>1||(components.day==1&&components.hour!=0)) {
- return [NSString stringWithFormat:@"%ld天",(long)components.day];
- }else{
- if (components.hour>1) {
- return [NSString stringWithFormat:@"%ld小时",(long)components.hour];
- }else{
- if (components.minute>1) {
- return [NSString stringWithFormat:@"%ld分钟",(long)components.minute];
- }else{
- return @"1分钟";
- }
- }
- }
- }
- // exhcnage status bar
- + (void)exchangeStatusBar:(UIStatusBarStyle) statusBarStyle{
- [GlobalData sharedInstance].statusBarStyle = statusBarStyle;
- UIViewController * lastVC = GB_Nav.lastVC;
- [lastVC setNeedsStatusBarAppearanceUpdate];
- }
- + (void)exchangeStatusBarHidden:(BOOL)hidden{
- [GlobalData sharedInstance].statusHidden = hidden;
- UIViewController * lastVC = GB_Nav.lastVC;
- [lastVC setNeedsStatusBarAppearanceUpdate];
- }
- //红色的数字
- + (void)exchangeLabel:(UILabel *)label positiveCount:(int)count {
- label.hidden = count <=0;
- if (!label ) return;
- count = count>99?99:count;
- NSString * strNum = count < 10? @"AAA": @"AAAA";
- [GlobalMethod setLabel:label widthLimit:0 numLines:0 fontNum:12 textColor:[UIColor whiteColor] text:strNum];
- label.backgroundColor = [UIColor redColor];
- [GlobalMethod setRoundView:label color:[UIColor clearColor] numRound:label.height / 2.0- 2 width:0];
- label.text = [NSString stringWithFormat:@"+%d",count];
- label.textAlignment = NSTextAlignmentCenter;
-
- }
- + (void)exchangeLabel:(UILabel *)label count:(int)count {
- label.hidden = count <=0;
- if (!label ) return;
- count = count>99?99:count;
- NSString * strNum = count < 10? @"AA": @"AAA";
- [GlobalMethod setLabel:label widthLimit:0 numLines:0 fontNum:12 textColor:[UIColor whiteColor] text:strNum];
- label.backgroundColor = [UIColor redColor];
- [GlobalMethod setRoundView:label color:[UIColor clearColor] numRound:label.height / 2.0 width:0];
- label.text = [NSString stringWithFormat:@"%d",count];
- label.textAlignment = NSTextAlignmentCenter;
- }
- //设置行间距
- +(void )setAttributeLabel:(UILabel *)label content:(NSString *)content width:(CGFloat)width
- {
- [self setAttributeLabel:label content:content width:width lineSpace:7];
- }
- +(void)setAttributeLabel:(UILabel *)label content:(NSString *)content width:(CGFloat)width lineSpace:(CGFloat)line{
- label.width = width;
- NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc] initWithString:content];
- NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
- [paragraphStyle setLineSpacing:line];
- [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [content length])];
- [label setAttributedText:attributedString];
- [label sizeToFit];
- }
- //获取审批cell
- + (CGFloat)resetCellView:(UIView *)view withArray:(NSArray *)array{
- return [self resetView:view withArray:array];
- }
- //sectionHeaderView
- +(UIView *)resetTitle:(NSString *)title{
- if (!isStr(title)) {
- return nil;
- }
- UIView *contentView = [[UIView alloc] init];
- contentView.backgroundColor = COLOR_ADDRESS;
- contentView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 30);
- UILabel *label = [UILabel new];
- [GlobalMethod setLabel:label widthLimit:0 numLines:1 fontNum:F(14) textColor:COLOR_999 text:@""];
- label.backgroundColor = [UIColor clearColor];
- [contentView addSubview:label];
- [label fitTitle:UnPackStr(title) variable:SCREEN_WIDTH-20];
- label.leftCenterY = XY(10,contentView.height/2.0);
- return contentView;
- }
- //复制内容到剪切板
- + (void)copyToPlte:(NSString *)str{
- if (!isStr(str)) {
- return;
- }
- UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
- pasteboard.string = str;
- }
- //显示编辑提示
- + (void)showEditAlert:(void (^)(void))block view:(UIView *)view{
- ModelBtn * modelDismiss = [ModelBtn modelWithTitle:@"取消" imageName:nil highImageName:nil tag:TAG_LINE color:[UIColor redColor]];
- modelDismiss.blockClick = block;
- ModelBtn * modelConfirm = [ModelBtn modelWithTitle:@"确认" imageName:nil highImageName:nil tag:TAG_LINE color:COLOR_BLUE];
- modelConfirm.blockClick = ^(void){
- [GB_Nav popViewControllerAnimated:true];
- };
- // [BaseAlertView initWithTitle:@"确认取消编辑?" content:@"返回将清除编辑的内容" aryBtnModels:@[modelDismiss,modelConfirm] viewShow:view];
- }
- //显示编辑提示dismissBlock与ConfirmBlock
- + (void)showEditAlertDismiss:(void (^)(void))dismissblock confirm:(void (^)(void))confirmblock view:(UIView *)view{
- ModelBtn * modelDismiss = [ModelBtn modelWithTitle:@"取消" imageName:nil highImageName:nil tag:TAG_LINE color:[UIColor redColor]];
- modelDismiss.blockClick = dismissblock;
- ModelBtn * modelConfirm = [ModelBtn modelWithTitle:@"确认" imageName:nil highImageName:nil tag:TAG_LINE color:COLOR_TEXT];
- modelConfirm.blockClick = confirmblock;
- [BaseAlertView initWithTitle:@"确认取消编辑?" content:@"返回将清除编辑的内容" aryBtnModels:@[modelDismiss,modelConfirm] viewShow:view];
- }
- //收键盘
- + (void)endEditing{
- UIWindow * window = [UIApplication sharedApplication].keyWindow;
- [window endEditing:true];
- }
- +(NSMutableAttributedString *)stringToAttributeString:(NSString *)text
- {
-
- //先把普通的字符串text转化生成Attributed类型的字符串
-
- NSMutableAttributedString * attStr = [[NSMutableAttributedString alloc]initWithString:text];
-
- NSString * zhengze = @"\\[\\([a-zA-Z0-9\u4e00-\u9fa5]+\\)\\]"; //正则表达式 ,例如 [(呵呵)] = 😑
-
- NSError * error;
-
- NSRegularExpression * re = [NSRegularExpression regularExpressionWithPattern:zhengze options:NSRegularExpressionCaseInsensitive error:&error];
-
- if (!re)
-
- {
-
- NSLog(@"%@😓",[error localizedDescription]);//打印错误😓
-
- }
-
- NSArray * arr = [re matchesInString:text options:0 range:NSMakeRange(0, text.length)];//遍历字符串,获得所有的匹配字符串
-
- NSBundle *bundle = [NSBundle mainBundle];
-
- NSString * path = [bundle pathForResource:@"emj" ofType:@"plist"]; //plist文件,制作一个 数组,包含文字,表情图片名称
-
- NSArray * face = [[NSArray alloc]initWithContentsOfFile:path];//获取 所有的数组
-
- //如果有多个表情图,必须从后往前替换,因为替换后Range就不准确了
-
- for (int j =(int) arr.count - 1; j >= 0; j--) {
-
- //NSTextCheckingResult里面包含range
-
- NSTextCheckingResult * result = arr[j];
-
- for (int i = 0; i < face.count; i++) {
-
- if ([[text substringWithRange:result.range] isEqualToString:face[i][@"key"]])//从数组中的字典中取元素
-
- {
-
- NSString * imageName = [NSString stringWithString:face[i][@"picture"]];
-
- NSTextAttachment * textAttachment = [[NSTextAttachment alloc]init];//添加附件,图片
-
- //textAttachment.bounds=CGRectMake(0, 0, 20, 20);//调节表情大小
-
- textAttachment.image = [UIImage imageNamed:imageName];
-
- NSAttributedString * imageStr = [NSAttributedString attributedStringWithAttachment:textAttachment];
-
- [attStr replaceCharactersInRange:result.range withAttributedString:imageStr];//替换未图片附件
-
- break;
-
- }
-
- }
-
- }
-
- return attStr;
-
- }
- + (BOOL)isLocationServiceOpen {
- if ([ CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied) {
- return NO;
- } else
- return YES;
- }
- @end
|