ViewTool

.adjustsFontSizeToFitWidth = true; // 字体自动变小

上面是扩展

// 

#import 

@interface ViewTool : NSObject

+(BOOL)hasSafeArea;
+(CGFloat)bottomSafeArea;

+(void)tipWithContent:(NSString *)content;
+ (void)tipWithContent:(NSString *)content afterDelay:(NSInteger)times;
//上下增加蒙版
+(void)addGradientMaskViewToView:(UIView *)targetView;

/// 返回 指定的 VC
+ (void)backToClass:(Class)aclass;
/**
 获取当前的viewController

 @return viewController
 */
+ (UIViewController *)topViewController;
/// 利用贝塞尔曲线添加指定位置圆角
/// @param view view
/// @param conrners 圆角位置
/// @param cornerRadii 圆角大小
+ (void)BezierPathWithRoundedRect:(UIView *)view RoundingCorners:(UIRectCorner)conrners CornerRadii:(CGSize)cornerRadii;

// 给label添加间距
+ (void)setAttributtedStringLabel:(UILabel *)label TotalStr:(NSString *)totalStr LineSpace:(CGFloat)lineSpace TextSpace:(CGFloat)textSpace NSTextAlignment:(NSTextAlignment)textAlignment;


@end

//

#import "ViewTool.h"
#import 

@implementation ViewTool

//是否有安全区域,是否是刘海屏幕
+(BOOL)hasSafeArea{
//    BOOL has = NO;
//    if([UIDevice currentDevice].userInterfaceIdiom != UIUserInterfaceIdiomPhone){
//        return has;
//    }
//    if(@available(iOS 11.0,*)){
//        UIWindow *mainWindow = [[[UIApplication sharedApplication] delegate] window];
//        if(mainWindow.safeAreaInsets.bottom > 0){
//            has = YES;
//        }
//    }
    return IPhoneX_All;
}

+(CGFloat)bottomSafeArea{
//    CGFloat bottom = 0;
//    if([UIDevice currentDevice].userInterfaceIdiom != UIUserInterfaceIdiomPhone){
//        return bottom;
//    }
//    if(@available(iOS 11.0,*)){
//        UIWindow *mainWindow = [[[UIApplication sharedApplication] delegate] window];
//        if(mainWindow.safeAreaInsets.bottom > 0){
//            bottom = mainWindow.safeAreaInsets.bottom;
//        }
//    }
    return SafeBottomMargin;
}

+(void)tipWithContent:(NSString *)content{
    
    dispatch_async(dispatch_get_main_queue(), ^{
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].keyWindow animated:YES];
        hud.userInteractionEnabled = NO;
        hud.mode = MBProgressHUDModeText;
        hud.label.text = content;
        hud.margin = 10.f;
        //hud.yOffset = 180;
        hud.removeFromSuperViewOnHide = YES;
        [hud hideAnimated:YES afterDelay:2];
        
    });
}

+ (void)tipWithContent:(NSString *)content afterDelay:(NSInteger)times
{    
    dispatch_async(dispatch_get_main_queue(), ^{
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].keyWindow animated:YES];
        hud.userInteractionEnabled = NO;
        hud.mode = MBProgressHUDModeText;
        hud.label.text = content;
        hud.margin = 10.f;
        //hud.yOffset = 180;
        hud.removeFromSuperViewOnHide = YES;
        [hud hideAnimated:YES afterDelay:times];
        
    });
}

+(void)addGradientMaskViewToView:(UIView *)targetView{
    UIView *maskView = [[UIView alloc] init];
    maskView.frame = targetView.bounds;
    
    // 上下蒙版
    CGFloat height = NavigationBarHeight;
    CAGradientLayer *topLayer = [[CAGradientLayer alloc] init];
    topLayer.colors = @[(id)[UIColor colorWithHexString:@"#000000" alpha:0].CGColor,
                        (id)[UIColor colorWithHexString:@"#000000" alpha:0.6].CGColor];
    topLayer.frame = CGRectMake(0, 0, targetView.bounds.size.width, height);
    topLayer.startPoint = CGPointMake(0.5, 1);
    topLayer.endPoint = CGPointMake(0.5, 0);
    [maskView.layer addSublayer:topLayer];
    
    height = SafeBottomMargin+44*RATIO;
    CAGradientLayer *bottomLayer = [[CAGradientLayer alloc] init];
    bottomLayer.colors = @[(id)[UIColor colorWithHexString:@"#000000" alpha:0].CGColor,
                           (id)[UIColor colorWithHexString:@"#000000" alpha:0.6].CGColor];
    bottomLayer.frame = CGRectMake(0, targetView.bounds.size.height - height, targetView.bounds.size.width, height);
    bottomLayer.startPoint = CGPointMake(0.5, 0);
    bottomLayer.endPoint = CGPointMake(0.5, 1);
    [maskView.layer addSublayer:bottomLayer];

    [targetView addSubview:maskView];
}

/// 返回 指定的 VC
+ (void)backToClass:(Class)aclass
{
    for (UIViewController *tempVC in [ViewTool topViewController].navigationController.viewControllers)
    {
        //if ([tempVC isKindOfClass:[你要跳转到的Controller class]])
        if ([tempVC isKindOfClass:aclass])
        {
            [[ViewTool topViewController].navigationController popToViewController:tempVC animated:YES];
        }
    }
}

/**
 获取当前的viewController
 
 @return viewController
 */
+ (UIViewController *)topViewController
{
    UIViewController *resultVC;
    resultVC = [self _topViewController:[[UIApplication sharedApplication].keyWindow rootViewController]];
    while (resultVC.presentedViewController) {
        resultVC = [self _topViewController:resultVC.presentedViewController];
    }
    return resultVC;
}

+ (UIViewController *)_topViewController:(UIViewController *)vc {
    if ([vc isKindOfClass:[UINavigationController class]]) {
        return [self _topViewController:[(UINavigationController *)vc topViewController]];
    } else if ([vc isKindOfClass:[UITabBarController class]]) {
        return [self _topViewController:[(UITabBarController *)vc selectedViewController]];
    } else {
        return vc;
    }
    return nil;
}

/// 利用贝塞尔曲线添加指定位置圆角
/// @param view view
/// @param conrners 圆角位置
/// @param cornerRadii 圆角大小
+ (void)BezierPathWithRoundedRect:(UIView *)view RoundingCorners:(UIRectCorner)conrners CornerRadii:(CGSize)cornerRadii
{
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:conrners cornerRadii:cornerRadii];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = view.bounds;
    maskLayer.path = maskPath.CGPath;
    view.layer.mask = maskLayer;
}

// 给label添加间距
+ (void)setAttributtedStringLabel:(UILabel *)label TotalStr:(NSString *)totalStr LineSpace:(CGFloat)lineSpace TextSpace:(CGFloat)textSpace NSTextAlignment:(NSTextAlignment)textAlignment {
    
   NSMutableAttributedString *mutableString = [[NSMutableAttributedString alloc]initWithString:totalStr];
//    [mutableString addAttribute:NSKernAttributeName value:@(textSpace) range:NSMakeRange(0, mutableString.length)];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = lineSpace; // 设置行间距
    paragraphStyle.alignment = textAlignment; //设置两端对齐显示
    [mutableString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, mutableString.length)];
    
    label.attributedText = mutableString;
}


@end


版权声明:
作者:ht
链接:https://www.techfm.club/p/41799.html
来源:TechFM
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
< <上一篇
下一篇>>