xcode13适配

This commit is contained in:
kai60
2021-10-30 15:23:22 +08:00
parent cb537f1949
commit 6029a5eb67
17 changed files with 480 additions and 39 deletions
@@ -0,0 +1,81 @@
//
// UIImage+WaterMark.h
// PictureWatermark
//
// Created by AD-iOS on 15/8/3.
// Copyright (c) 2015年 Adinnet. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface UIImage (WaterMark)
/**
* 给图片加水印图片
*
* @param image 水印图片
* @param imgRect 水印图片所在位置,大小
* @param alpha 水印图片的透明度,0~1之间,透明度太大会完全遮盖被加水印图片的那一部分
*
* @return 加完水印的图片
*/
- (UIImage*)imageWaterMarkWithImage:(UIImage *)image imageRect:(CGRect)imgRect alpha:(CGFloat)alpha;
/**
* 同上
*
* @param image 同上
* @param imgPoint 水印图片(00)所在位置
* @param alpha 同上
*
* @return 同上
*/
- (UIImage*)imageWaterMarkWithImage:(UIImage*)image imagePoint:(CGPoint)imgPoint alpha:(CGFloat)alpha;
/**
* 给图片加文字水印
*
* @param str 水印文字
* @param strRect 文字所在的位置大小
* @param attri 文字的相关属性,自行设置
*
* @return 加完水印文字的图片
*/
- (UIImage*)imageWaterMarkWithString:(NSString*)str rect:(CGRect)strRect attribute:(NSDictionary *)attri;
/**
* 同上
*
* @param str 同上
* @param strPoint 文字(00)点所在位置
* @param attri 同上
*
* @return 同上
*/
- (UIImage*)imageWaterMarkWithString:(NSString*)str point:(CGPoint)strPoint attribute:(NSDictionary*)attri;
/**
* 返回加水印文字和图片的图片
*
* @param str 水印文字
* @param strPoint 文字(00)点所在位置
* @param attri 文字属性
* @param image 水印图片
* @param imgPoint 图片(00)点所在位置
* @param alpha 透明度
*
* @return 加完水印的图片
*/
- (UIImage*)imageWaterMarkWithString:(NSString*)str point:(CGPoint)strPoint attribute:(NSDictionary*)attri image:(UIImage*)image imagePoint:(CGPoint)imgPoint alpha:(CGFloat)alpha;
/**
* 同上
*
* @param str 同上
* @param strRect 文字的位置大小
* @param attri 同上
* @param image 同上
* @param imgRect 图片的位置大小
* @param alpha 透明度
*
* @return 同上
*/
- (UIImage*)imageWaterMarkWithString:(NSString*)str rect:(CGRect)strRect attribute:(NSDictionary *)attri image:(UIImage *)image imageRect:(CGRect)imgRect alpha:(CGFloat)alpha;
@end
@@ -0,0 +1,70 @@
//
// UIImage+WaterMark.m
// PictureWatermark
//
// Created by AD-iOS on 15/8/3.
// Copyright (c) 2015年 Adinnet. All rights reserved.
//
#import "UIImage+WaterMark.h"
@implementation UIImage (WaterMark)
- (UIImage*)imageWaterMarkWithImage:(UIImage *)image imageRect:(CGRect)imgRect alpha:(CGFloat)alpha
{
return [self imageWaterMarkWithString:nil rect:CGRectZero attribute:nil image:image imageRect:imgRect alpha:alpha];
}
- (UIImage*)imageWaterMarkWithImage:(UIImage*)image imagePoint:(CGPoint)imgPoint alpha:(CGFloat)alpha
{
return [self imageWaterMarkWithString:nil point:CGPointZero attribute:nil image:image imagePoint:imgPoint alpha:alpha];
}
- (UIImage*)imageWaterMarkWithString:(NSString*)str rect:(CGRect)strRect attribute:(NSDictionary *)attri
{
return [self imageWaterMarkWithString:str rect:strRect attribute:attri image:nil imageRect:CGRectZero alpha:0];
}
- (UIImage*)imageWaterMarkWithString:(NSString*)str point:(CGPoint)strPoint attribute:(NSDictionary*)attri
{
return [self imageWaterMarkWithString:str point:strPoint attribute:attri image:nil imagePoint:CGPointZero alpha:0];
}
- (UIImage*)imageWaterMarkWithString:(NSString*)str point:(CGPoint)strPoint attribute:(NSDictionary*)attri image:(UIImage*)image imagePoint:(CGPoint)imgPoint alpha:(CGFloat)alpha
{
UIGraphicsBeginImageContext(self.size);
[self drawAtPoint:CGPointMake(0, 0) blendMode:kCGBlendModeNormal alpha:1.0];
if (image) {
[image drawAtPoint:imgPoint blendMode:kCGBlendModeNormal alpha:alpha];
}
if (str) {
[str drawAtPoint:strPoint withAttributes:attri];
}
UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return resultImage;
}
- (UIImage*)imageWaterMarkWithString:(NSString*)str rect:(CGRect)strRect attribute:(NSDictionary *)attri image:(UIImage *)image imageRect:(CGRect)imgRect alpha:(CGFloat)alpha
{
UIGraphicsBeginImageContext(self.size);
[self drawInRect:CGRectMake(0, 0, self.size.width, self.size.height) blendMode:kCGBlendModeNormal alpha:1.0];
if (image) {
[image drawInRect:imgRect blendMode:kCGBlendModeNormal alpha:alpha];
}
if ([str isKindOfClass:[NSAttributedString class]])
{
[(NSAttributedString*)str drawInRect:strRect];
}
else if (str) {
[str drawInRect:strRect withAttributes:attri];
}
UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return resultImage;
}
@end
@@ -0,0 +1,44 @@
//
// UIViewController+Navgation.h
// E-Mobile7
//
// Created by ldg on 2018/4/13.
// Copyright © 2018年 Weaver. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface UIViewController (Navgation)
/**
* 设置导航栏背景颜色
*/
- (void)setNavbgColorStr:(NSString *)colorStr;
/**
* 设置导航栏背景图片
*/
- (void)setNavbgImage:(UIImage *)image;
//获取
-(UIImage*)getNaveImage;
- (void)setNavbgColor:(UIColor *)color;
/**
* 设置导航栏文字颜色
*/
- (void)setNavTitleColor:(UIColor *)color;
//字体和颜色
- (void)setNavTitleTextAttributes:(NSDictionary*)attribute;
//字体
- (void)setNavTitleFont:(UIFont *)font;
- (UINavigationBarAppearance *)getNavgationAppearanceWithNavgationController:(UINavigationBar *)bar API_AVAILABLE(ios(13.0));
@end
@@ -0,0 +1,202 @@
//
// UIViewController+Navgation.m
// E-Mobile7
//
// Created by ldg on 2018/4/13.
// Copyright © 2018年 Weaver. All rights reserved.
//
#import "UIViewController+Navgation.h"
@implementation UIViewController (Navgation)
+ (UIColor *)colorWithHexString:(NSString *)stringToConvert
{
if ([stringToConvert hasPrefix:@"#"])
{
stringToConvert = [stringToConvert substringFromIndex:1];
}
float alpha=1.0;
if (stringToConvert.length>6&&![stringToConvert hasPrefix:@"0x"])//含有透明度
{
NSString*alphaString=[stringToConvert substringToIndex:2];
NSScanner *alphaScanner = [NSScanner scannerWithString:alphaString];
unsigned hex;
if (![alphaScanner scanHexInt:&hex])
{
alpha=1.0;
}
else
{
const char *hexChar = [alphaString cStringUsingEncoding:NSUTF8StringEncoding];
int hexNumber;
sscanf(hexChar, "%x", &hexNumber);
alpha=hexNumber/255.0;
}
}
NSScanner *scanner = [NSScanner scannerWithString:stringToConvert];
unsigned hexNum;
if (![scanner scanHexInt:&hexNum])
{
return [UIColor clearColor];
}
return [UIColor \
colorWithRed:((float)((hexNum & 0xFF0000) >> 16))/255.0 \
green:((float)((hexNum & 0xFF00) >> 8))/255.0 \
blue:((float)(hexNum & 0xFF))/255.0 alpha:alpha];
}
+ (UIImage *)createImageWithColor:(UIColor *)color
{
CGRect rect=CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return theImage;
}
- (void)setNavbgColorStr:(NSString *)colorStr
{
UIImage *image = [self.class createImageWithColor:[self.class colorWithHexString:colorStr]];
[self setNavbgImage:image];
}
- (void)setNavbgColor:(UIColor *)color
{
UIImage *image = [self.class createImageWithColor:color];
[self setNavbgImage:image];
}
- (void)setNavbgImage:(UIImage *)image
{
UINavigationController *nav = nil;
if ([self isKindOfClass:UINavigationController.class]) {
nav = (UINavigationController *)self;
}else{
nav = self.navigationController;
}
if (image && nav) {
if (@available(iOS 15.0, *)) {
UINavigationBarAppearance *apperar = [self getNavgationAppearanceWithNavgationController:nav.navigationBar];
apperar.backgroundImage = image;
nav.navigationBar.standardAppearance = apperar;
nav.navigationBar.scrollEdgeAppearance = apperar;
}else{
[nav.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
}
}
}
-(UIImage*)getNaveImage
{
UINavigationController *nav = nil;
if ([self isKindOfClass:UINavigationController.class]) {
nav = (UINavigationController *)self;
}else{
nav = self.navigationController;
}
if (nav) {
if (@available(iOS 15.0, *)) {
UINavigationBarAppearance *apperar = [self getNavgationAppearanceWithNavgationController:nav.navigationBar];
return apperar.backgroundImage;
}else{
return [nav.navigationBar
backgroundImageForBarMetrics:UIBarMetricsDefault];
}
}
return nil;
}
- (UINavigationBarAppearance *)getNavgationAppearanceWithNavgationController:(UINavigationBar *)bar
API_AVAILABLE(ios(13.0)){
if (@available(iOS 15.0, *)) {
if (bar.standardAppearance) {
return bar.standardAppearance;
}
if (bar.scrollEdgeAppearance) {
return bar.scrollEdgeAppearance;
}
return [[UINavigationBarAppearance alloc] init];
}
return nil;
}
- (void)setNavTitleColor:(UIColor *)color
{
UINavigationController *nav = nil;
if ([self isKindOfClass:UINavigationController.class]) {
nav = (UINavigationController *)self;
}else{
nav = self.navigationController;
}
if (color && nav) {
if (@available(iOS 15.0, *)) {
UINavigationBarAppearance *apperar = [self getNavgationAppearanceWithNavgationController:nav.navigationBar];
apperar.titleTextAttributes = @{NSForegroundColorAttributeName:color};
nav.navigationBar.standardAppearance = apperar;
nav.navigationBar.scrollEdgeAppearance = apperar;
}else{
nav.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:color};
}
}
}
- (void)setNavTitleFont:(UIFont *)font
{
UINavigationController *nav = nil;
if ([self isKindOfClass:UINavigationController.class]) {
nav = (UINavigationController *)self;
}else{
nav = self.navigationController;
}
if (font && nav) {
if (@available(iOS 15.0, *)) {
UINavigationBarAppearance *apperar = [self getNavgationAppearanceWithNavgationController:nav.navigationBar];
apperar.titleTextAttributes = @{NSFontAttributeName:font};
nav.navigationBar.standardAppearance = apperar;
nav.navigationBar.scrollEdgeAppearance = apperar;
}else{
nav.navigationBar.titleTextAttributes = @{NSFontAttributeName:font};
}
}
}
- (void)setNavTitleTextAttributes:(NSDictionary*)attribute
{
UINavigationController *nav = nil;
if ([self isKindOfClass:UINavigationController.class]) {
nav = (UINavigationController *)self;
}else{
nav = self.navigationController;
}
if (attribute && nav) {
if (@available(iOS 15.0, *)) {
UINavigationBarAppearance *apperar = [self getNavgationAppearanceWithNavgationController:nav.navigationBar];
apperar.titleTextAttributes = attribute;
nav.navigationBar.standardAppearance = apperar;
nav.navigationBar.scrollEdgeAppearance = apperar;
}else{
nav.navigationBar.titleTextAttributes = attribute;
}
}
}
@end