美文网首页UIButtonUILabel笔记篇
UIView+AKit.h 扩展(渐变色)

UIView+AKit.h 扩展(渐变色)

作者: 失忆的程序员 | 来源:发表于2022-03-21 13:21 被阅读0次
参考图.png 红苹果.jpg
// 

#import "UIView+AKit.h"

@implementation UIView (AKit)

// 添加Tap手势
- (void)addTapGestureRecognizerWithTarget:(id)target action:(SEL)action
{
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:target action:action];
    self.userInteractionEnabled = YES;
    [self addGestureRecognizer:tap];
}

// 添加Pan手势
- (void)addPanGestureRecognizerWithTarget:(id)target action:(SEL)action
{
    UIPanGestureRecognizer *tap = [[UIPanGestureRecognizer alloc]initWithTarget:target action:action];
    self.userInteractionEnabled = YES;
    [self addGestureRecognizer:tap];
}

// 添加LongPress手势
- (void)addLongPressGestureRecognizerWithTarget:(id)target action:(SEL)action
{
    UILongPressGestureRecognizer *tap = [[UILongPressGestureRecognizer alloc]initWithTarget:target action:action];
    self.userInteractionEnabled = YES;
    [self addGestureRecognizer:tap];
}

/// 设置阴影
/// @param opacity 阴影透明度
/// @param shadowColor 阴影颜色
/// @param shadowOffset 阴影偏移量
/// @param shadowRadius 阴影圆角
/// @param cornerRadius view圆角
- (void)setShadowOpacity:(CGFloat)opacity ShadowColor:(CGColorRef)shadowColor ShadowOffset:(CGSize)shadowOffset ShadowRadius:(CGFloat)shadowRadius CornerRadius:(CGFloat)cornerRadius {
    self.layer.shadowColor = shadowColor;
    self.layer.shadowOffset = shadowOffset;
    self.layer.shadowOpacity = opacity;
    self.layer.shadowRadius = shadowRadius;
    self.layer.cornerRadius = cornerRadius;
}

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

/// 给View添加背景渐变色
/// @param startColor 开始颜色
/// @param endColor 结束颜色
/// @param cornerRadius 圆角
- (void)setCAGradientBackgroundColorWithStartColor:(UIColor *)startColor endColor:(UIColor *)endColor cornerRadius:(CGFloat)cornerRadius {
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.colors = @[(__bridge id)startColor.CGColor, (__bridge id)endColor.CGColor];
    gradientLayer.locations = @[@0, @1];
    gradientLayer.startPoint = CGPointMake(0, 0);
    gradientLayer.endPoint = CGPointMake(1, 1);
    gradientLayer.frame = self.bounds;
    gradientLayer.cornerRadius = cornerRadius;
    [self.layer insertSublayer:gradientLayer atIndex:0];
}


/// 给View添加 边框渐变色
/// @param lineWidth  边框宽度
/// @param startColor 开始颜色
/// @param endColor 结束颜色
/// @param conrners 圆角位置
/// @param cornerRadii 圆角大小
- (void)setCAGradientLayerCAShapeLayerWithLineWidth:(CGFloat)lineWidth startColor:(UIColor *)startColor endColor:(UIColor *)endColor RoundingCorners:(UIRectCorner)conrners CornerRadii:(CGSize)cornerRadii
{
    CAGradientLayer *gradientLayer = [[CAGradientLayer alloc] init];
    gradientLayer.frame = self.bounds;
    gradientLayer.cornerRadius = cornerRadii.height; // cornerRadii.width
    gradientLayer.colors = [NSArray arrayWithObjects:(id)[startColor CGColor], (id)[endColor CGColor], nil];
    gradientLayer.locations = @[@0, @1];
    gradientLayer.startPoint = CGPointMake(0, 0);
    gradientLayer.endPoint = CGPointMake(1, 1);
    
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.lineWidth = lineWidth;
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:conrners cornerRadii:cornerRadii];
    maskLayer.path = maskPath.CGPath;
    maskLayer.fillColor = [UIColor clearColor].CGColor;
    maskLayer.strokeColor = [UIColor blackColor].CGColor;
    gradientLayer.mask = maskLayer;
    [self.layer addSublayer:gradientLayer];
}


@end

@implementation UILabel (AKit)

/// 设置富文本文字
/// @param firstStr 第一个字符串
/// @param secondStr 第二个字符串
/// @param firstFont 第一个字体
/// @param secondFont 第二个字体
/// @param firstColor 第一个颜色
/// @param secondColor 第二个颜色
- (void)setAttributedTextWithFirstStr:(NSString *)firstStr SecondStr:(NSString *)secondStr FirstFont:(UIFont *)firstFont SecondFont:(UIFont *)secondFont FirstColor:(UIColor *)firstColor SecondColor:(UIColor *)secondColor {
    NSString *totalStr = [NSString stringWithFormat:@"%@%@",firstStr,secondStr];
    NSMutableAttributedString *mutableString = [[NSMutableAttributedString alloc]initWithString:totalStr];
    [mutableString addAttribute:NSFontAttributeName value:firstFont range:NSMakeRange(0,firstStr.length)];
    [mutableString addAttribute:NSFontAttributeName value:secondFont range:NSMakeRange(firstStr.length,secondStr.length)];
    [mutableString addAttribute:NSForegroundColorAttributeName value:firstColor range:NSMakeRange(0, firstStr.length)];
    [mutableString addAttribute:NSForegroundColorAttributeName value:secondColor range:NSMakeRange(firstStr.length,secondStr.length)];
    self.attributedText = mutableString;
}

/// label添加渐变色
/// @param startColor 开始渐变色
/// @param endColor 结束渐变色
- (void)setGradientRefWithStartColor:(UIColor *)startColor endColor:(UIColor *)endColor {
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, [UIScreen mainScreen].scale);
    CGContextRef context = UIGraphicsGetCurrentContext();
    //绘制渐变层
    CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
    CGGradientRef gradientRef = CGGradientCreateWithColors(colorSpaceRef,
                                                           (__bridge CFArrayRef)@[(id)startColor.CGColor,(id)endColor.CGColor],
                                                           NULL);
    CGPoint startPoint = CGPointZero;
    CGPoint endPoint = CGPointMake(CGRectGetMaxX(self.bounds), CGRectGetMaxY(self.bounds));
    CGContextDrawLinearGradient(context, gradientRef, startPoint, endPoint,  kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation);
    //取到渐变图片
    UIImage *gradientImage = UIGraphicsGetImageFromCurrentImageContext();
    //释放资源
    CGColorSpaceRelease(colorSpaceRef);
    CGGradientRelease(gradientRef);
    UIGraphicsEndImageContext();
    self.textColor = [UIColor colorWithPatternImage:gradientImage];
}

/// 设置阴影
/// @param view view
/// @param opacity 阴影透明度
/// @param shadowColor 阴影颜色
/// @param shadowOffset 阴影偏移量
/// @param shadowRadius 阴影圆角
/// @param cornerRadius view圆角
+ (void)setView:(UIView *)view ShadowOpacity:(CGFloat)opacity ShadowColor:(CGColorRef)shadowColor ShadowOffset:(CGSize)shadowOffset ShadowRadius:(CGFloat)shadowRadius CornerRadius:(CGFloat)cornerRadius {
    view.layer.shadowColor = shadowColor;
    view.layer.shadowOffset = shadowOffset;
    view.layer.shadowOpacity = opacity;
    view.layer.shadowRadius = shadowRadius;
    view.layer.cornerRadius = cornerRadius;
}

/// 给label添加间距
- (void)setAttributtedStringTotalStr:(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)];
    
    self.attributedText = mutableString;
    self.lineBreakMode = NSLineBreakByTruncatingTail;
}

@end

@implementation UIImageView (AKit)

/// 填充裁剪
- (void)setCommonContentMode {
    self.clipsToBounds = YES;
    self.contentMode = UIViewContentModeScaleAspectFill;
}

@end

苹果.jpg
// 

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface UIView (AKit)

// 添加Tap手势
- (void)addTapGestureRecognizerWithTarget:(id)target action:(SEL)action;

// 添加Pan手势
- (void)addPanGestureRecognizerWithTarget:(id)target action:(SEL)action;

// 添加LongPress手势
- (void)addLongPressGestureRecognizerWithTarget:(id)target action:(SEL)action;

/// 设置阴影
/// @param opacity 阴影透明度
/// @param shadowColor 阴影颜色
/// @param shadowOffset 阴影偏移量
/// @param shadowRadius 阴影圆角
/// @param cornerRadius view圆角
- (void)setShadowOpacity:(CGFloat)opacity ShadowColor:(CGColorRef)shadowColor ShadowOffset:(CGSize)shadowOffset ShadowRadius:(CGFloat)shadowRadius CornerRadius:(CGFloat)cornerRadius;

/// 利用贝塞尔曲线添加指定位置圆角
/// @param conrners 圆角位置
/// @param cornerRadii 圆角大小
- (void)setBezierPathWithRoundingCorners:(UIRectCorner)conrners CornerRadii:(CGSize)cornerRadii;

/// 给View添加背景渐变色
/// @param startColor 开始颜色
/// @param endColor 结束颜色
/// @param cornerRadius 圆角
- (void)setCAGradientBackgroundColorWithStartColor:(UIColor *)startColor endColor:(UIColor *)endColor cornerRadius:(CGFloat)cornerRadius;

/// 给View添加 边框渐变色
/// @param lineWidth  边框宽度
/// @param startColor 开始颜色
/// @param endColor 结束颜色
/// @param conrners 圆角位置
/// @param cornerRadii 圆角大小
- (void)setCAGradientLayerCAShapeLayerWithLineWidth:(CGFloat)lineWidth startColor:(UIColor *)startColor endColor:(UIColor *)endColor RoundingCorners:(UIRectCorner)conrners CornerRadii:(CGSize)cornerRadii;

@end

@interface UILabel (AKit)
/// 设置富文本文字
/// @param firstStr 第一个字符串
/// @param secondStr 第二个字符串
/// @param firstFont 第一个字体
/// @param secondFont 第二个字体
/// @param firstColor 第一个颜色
/// @param secondColor 第二个颜色
- (void)setAttributedTextWithFirstStr:(NSString *)firstStr SecondStr:(NSString *)secondStr FirstFont:(UIFont *)firstFont SecondFont:(UIFont *)secondFont FirstColor:(UIColor *)firstColor SecondColor:(UIColor *)secondColor;

/// label添加渐变色
/// @param startColor 开始渐变色
/// @param endColor 结束渐变色
- (void)setGradientRefWithStartColor:(UIColor *)startColor endColor:(UIColor *)endColor;

/// 给label添加间距
- (void)setAttributtedStringTotalStr:(NSString *)totalStr LineSpace:(CGFloat)lineSpace TextSpace:(CGFloat)textSpace NSTextAlignment:(NSTextAlignment)textAlignment;

@end

@interface UIImageView (AKit)
/// 填充裁剪
- (void)setCommonContentMode;
@end

NS_ASSUME_NONNULL_END

帅的一塌糊涂.png

相关文章

网友评论

    本文标题:UIView+AKit.h 扩展(渐变色)

    本文链接:https://www.haomeiwen.com/subject/pottjrtx.html