357 lines
13 KiB
Objective-C
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// sysInfo.m
// myhome
//
// Created by hkh on 15/10/31.
//
//
#import "UI.h"
#import <math.h>
#import "Pub.h"
@interface UI ()
@end
@implementation UI{
}
+ (CGFloat)getScreenWidth{
CGRect screenRect = [[UIScreen mainScreen] bounds];
return screenRect.size.width;
}
+ (CGFloat)getScreenHeight{
CGRect screenRect = [[UIScreen mainScreen] bounds];
return screenRect.size.height;
}
+ (void)SetRView:(UIView *) view Top:(NSString *) t Right:(NSString *) r Bottom:(NSString *) b Left:(NSString *) l{
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat t1 = [UI String2Float:t ULen:screenRect.size.height];
CGFloat r1 = [UI String2Float:r ULen:screenRect.size.width];
CGFloat b1 = [UI String2Float:b ULen:screenRect.size.height];
CGFloat l1 = [UI String2Float:l ULen:screenRect.size.width];
CGFloat w1 = screenRect.size.width - l1 - r1;
CGFloat h1 = screenRect.size.height - t1 - b1;
CGFloat x1 = l1;
CGFloat y1 = t1;
//NSLog(@"SetRView:%f%f%f%f",x1, y1, w1, h1);
view.frame = CGRectMake(x1, y1, w1, h1);
}
+ (CGFloat)String2Float:(NSString *) str ULen:(CGFloat) l{
CGFloat v = 0;
//判断宽度是值还是百分比
if([str containsString:@"%"]){
v = [[str stringByReplacingOccurrencesOfString:@"%" withString:@""] floatValue];
v = v * l/100;
}
else if([str containsString:@"v"]){
CGRect screenRect = [[UIScreen mainScreen] bounds];
if([str containsString:@"vw"]){
v = [[str stringByReplacingOccurrencesOfString:@"vw" withString:@""] floatValue];
v = v * screenRect.size.width/100;
}
else if([str containsString:@"vh"]){
v = [[str stringByReplacingOccurrencesOfString:@"vw" withString:@""] floatValue];
v = v * screenRect.size.height/100;
}
else if([str containsString:@"vmin"]){
v = [[str stringByReplacingOccurrencesOfString:@"vw" withString:@""] floatValue];
v = v * MIN(screenRect.size.width, screenRect.size.height) /100;
}
else if([str containsString:@"vmax"]){
v = [[str stringByReplacingOccurrencesOfString:@"vw" withString:@""] floatValue];
v = v * MAX(screenRect.size.width, screenRect.size.height) /100;
}
}
else{
v = [[str stringByReplacingOccurrencesOfString:@"dp" withString:@""] floatValue];
}
return v;
}
+ (void)setFrame:(UIView *) view Width:(NSString *) w Height:(NSString *) h Left:(NSString *) x Top:(NSString *) y{
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat w1 = [UI String2Float:w ULen:screenRect.size.width];
CGFloat h1 = [UI String2Float:h ULen:screenRect.size.height];
CGFloat x1 = [UI String2Float:x ULen:screenRect.size.width];
CGFloat y1 = [UI String2Float:y ULen:screenRect.size.height];
//NSLog(@"setFrame:%f%f%f%f",x1, y1, w1, h1);
view.frame = CGRectMake(x1, y1, w1, h1);
}
+ (void)setFrameInView:(UIView *) view Width:(NSString *) w Height:(NSString *) h TranslateX:(NSString *) x TranslateY:(NSString *) y InView:(UIView *) parent Position:(UIPositionInParent)pos{
if(!parent)parent = (UIView *)[view nextResponder];
CGFloat w1 = [UI String2Float:w ULen:parent.frame.size.width];
CGFloat h1 = [UI String2Float:h ULen:parent.frame.size.height];
CGFloat x1 = [UI String2Float:x ULen:parent.frame.size.width];
CGFloat y1 = [UI String2Float:y ULen:parent.frame.size.height];
switch (pos) {
case UITopLeft:
//y1 = y1;
//x1 = x1;
break;
case UITopMid:
//y1 = y1;
x1 += (parent.frame.size.width - w1)/2;
break;
case UITopRight:
//y1 = y1;
x1 = parent.frame.size.width - w1 - x1;
break;
case UIMidLeft:
y1 += (parent.frame.size.height - h1)/2;
//x1 = x1;
break;
case UICenter:
y1 += (parent.frame.size.height - h1)/2;
x1 += (parent.frame.size.width - w1)/2;
break;
case UIMidRight:
y1 += (parent.frame.size.height - h1)/2;
x1 = parent.frame.size.width - w1 - x1;
break;
case UIBottomLeft:
y1 = parent.frame.size.height - h1 - y1;
//x1 = x1;
break;
case UIBottomMid:
y1 = parent.frame.size.height - h1 - y1;
x1 += (parent.frame.size.width - w1)/2;
break;
case UIBottomRight:
y1 = parent.frame.size.height - h1 - y1;
x1 = parent.frame.size.width - w1 - x1;
break;
default:
break;
}
//NSLog(@"setFrameInView:%f%f%f%f",x1, y1, w1, h1);
view.frame = CGRectMake(x1, y1, w1, h1);
}
+ (void)setFrameByView:(UIView *) view Width:(NSString *) w Height:(NSString *) h TranslateX:(NSString *) x TranslateY:(NSString *) y ByView:(UIView *) brother Position:(UIPositionByView)pos{
UIView * parent = (UIView *)[view nextResponder];
CGFloat w1 = [UI String2Float:w ULen:parent.frame.size.width];
CGFloat h1 = [UI String2Float:h ULen:parent.frame.size.height];
CGFloat x1 = [UI String2Float:x ULen:parent.frame.size.width];
CGFloat y1 = [UI String2Float:y ULen:parent.frame.size.height];
switch (pos) {
case TopAlignLeft:
y1 = brother.frame.origin.y - h1 - y1;
x1 += brother.frame.origin.x;
break;
case TopAlignCenter:
y1 = brother.frame.origin.y - h1 - y1;
x1 += brother.frame.origin.x + (brother.frame.size.width - w1)/2 ;
break;
case TopAlignRight:
y1 = brother.frame.origin.y - h1 - y1;
x1 += brother.frame.size.width + brother.frame.origin.x - w1;
break;
case RightAlignTop:
x1 += brother.frame.size.width + brother.frame.origin.x ;
y1 += brother.frame.origin.y;
break;
case RightAlignCenter:
x1 += brother.frame.size.width + brother.frame.origin.x ;
y1 += brother.frame.origin.y + (brother.frame.size.height - h1)/2 ;
break;
case RightAlignBottom:
x1 += brother.frame.size.width + brother.frame.origin.x ;
y1 += brother.frame.size.height + brother.frame.origin.y - h1;
break;
case BottomAlignLeft:
y1 += brother.frame.size.height + brother.frame.origin.y;
x1 += brother.frame.origin.x;
break;
case BottomAlignCenter:
y1 += brother.frame.size.height + brother.frame.origin.y;
x1 += brother.frame.origin.x + (brother.frame.size.width - w1)/2 ;
break;
case BottomAlignRight:
y1 += brother.frame.size.height + brother.frame.origin.y;
x1 += brother.frame.size.width + brother.frame.origin.x - w1;
break;
case LeftAlignTop:
x1 = brother.frame.origin.x - w1 - x1;
y1 += brother.frame.origin.y;
break;
case LeftAlignCenter:
x1 += brother.frame.origin.x - w1 - x1;
y1 += brother.frame.origin.y + (brother.frame.size.height - h1)/2 ;
break;
case LeftAlignBottom:
x1 += brother.frame.origin.x - w1 - x1;
y1 += brother.frame.size.height + brother.frame.origin.y - h1;
break;
default:
break;
}
//NSLog(@"setFrameInView:%f%f%f%f",x1, y1, w1, h1);
view.frame = CGRectMake(x1, y1, w1, h1);
}
/**不能对最上层控件使用*/
+ (void)chgMargin:(UIView *) view Css:(NSString *) css{
UIView * parent = (UIView *)[view nextResponder];
CGFloat w1 = view.frame.size.width;
CGFloat h1 = view.frame.size.height;
CGFloat x1 = view.frame.origin.x;
CGFloat y1 = view.frame.origin.y;
NSArray *array = [css componentsSeparatedByString:@" "];
if(array.count>0){//上
CGFloat minus = [UI String2Float:array[0] ULen:parent.frame.size.height];
if(minus < h1){
y1 += minus;
h1 -= minus;
}
}
if(array.count>1){//右
CGFloat minus = [UI String2Float:array[1] ULen:parent.frame.size.width];
if(minus < w1){
w1 -= minus;
//y1不变
}
}
if(array.count>2){//下
CGFloat minus = [UI String2Float:array[2] ULen:parent.frame.size.height];
if(minus < h1){
//x1不变;
h1 -= minus;
}
}
if(array.count>3){//左
CGFloat minus = [UI String2Float:array[3] ULen:parent.frame.size.width];
if(minus < w1){
x1 += minus;
w1 -= minus;
}
}
view.frame = CGRectMake(x1, y1, w1, h1);
}
+ (void)setCornerRadius:(UIView *) view{
CGFloat w1 = view.frame.size.width;
CGFloat h1 = view.frame.size.height;
//NSLog(@"setCornerRadius: %f%f", w1, h1);
[view.layer setCornerRadius:MIN(w1,h1)/2]; //设置矩形四个圆角半径
}
+ (void)setGradientBorder:(UIView *)view withColor:(NSInteger)color{
view.backgroundColor = [UI colorWithHex:color alpha:0.5] ;
CAGradientLayer *layer1 = [[CAGradientLayer alloc] init];
layer1.frame = view.bounds;
layer1.colors = @[(__bridge id)[UI colorWithHex:color].CGColor,(__bridge id)[UI colorWithHex:0xffffff].CGColor];
layer1.startPoint = CGPointMake(0.3, 0.3);
layer1.endPoint = CGPointMake(1, 1);
[layer1 setCornerRadius:MIN(view.frame.size.width,view.frame.size.height)/2];
CAShapeLayer *layer2 = [[CAShapeLayer alloc] init];
layer2.lineWidth = 0;
//maskLayer.path = [UIBezierPath bezierPathWithRect:self.openView.bounds].CGPath;
CGPoint center = CGPointMake(view.frame.size.width * 0.5, view.frame.size.height * 0.5);
CGFloat radius = view.frame.size.height * 0.44;
layer2.path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:0 endAngle:M_PI * 2 clockwise:YES].CGPath;
layer2.fillColor = [UI colorWithHex:color].CGColor;
layer2.strokeColor = nil;
[view.layer addSublayer:layer1];
[view.layer addSublayer:layer2];
}
+ (UIImage *)reSizeImage:(UIImage *)image toSize:(CGSize)reSize
{
UIGraphicsBeginImageContext(CGSizeMake(reSize.width, reSize.height));
[image drawInRect:CGRectMake(0, 0, reSize.width, reSize.height)];
UIImage *reSizeImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return reSizeImage;
}
+ (UILabel *)NewLabel:(NSString *) text Color:(UIColor *)color Size:(CGFloat)size Align:(NSTextAlignment) align{
UILabel *label = [[UILabel alloc] init];
label.font = [UIFont systemFontOfSize:size];
label.textColor = color;
label.textAlignment = align;
label.text = NSLocalizedString(text, nil);
return label;
}
+ (CGFloat)getNavHeight{
@try
{
if([@"MainTabBar" isEqualToString:NSStringFromClass([[Pub getApp].window.rootViewController class])]){
UITabBarController *tab = (UITabBarController *)[Pub getApp].window.rootViewController;
UINavigationController *nav = tab.viewControllers[tab.selectedIndex];
return nav.navigationBar.frame.size.height;
}
} @catch (NSException * e) {
}
return 44.;
}
//颜色处理
+ (UIColor *)colorWithHex:(NSInteger)hexValue
{
return [self colorWithHex:hexValue alpha:1.0];
}
+ (UIColor *)colorWithHex:(NSInteger)hexValue alpha:(CGFloat)alphaValue
{
return [UIColor colorWithRed:((float)((hexValue & 0xFF0000) >> 16)) / 255.0
green:((float)((hexValue & 0xFF00) >> 8)) / 255.0
blue:((float)(hexValue & 0xFF)) / 255.0
alpha:alphaValue];
}
+ (CGFloat)getStatuHeight{
if(@available(ios 13.0,*)){
NSSet *set = [UIApplication sharedApplication].connectedScenes;
UIWindowScene * windowScene = [set anyObject];
UIStatusBarManager *statusBarManager = windowScene.statusBarManager;
return statusBarManager.statusBarFrame.size.height;
}
else{
return [UIApplication sharedApplication].statusBarFrame.size.height;
}
}
+ (CGFloat)getStatuAndStatuHeight{
return [self getStatuHeight]+[self getNavHeight];
}
+ (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size
{
CGRect rect = CGRectMake(0, 0, size.width, size.height);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context,color.CGColor);
CGContextFillRect(context, rect);
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
@end