NS_ASSUME_NONNULL_BEGIN
@interface PROpenVipHeadTagView : UIView
@end
@interface PRGradientLabel : UILabel
/// 設(shè)置漸變文字顏色
/// @param colors漸變顏色數(shù)組(例如:@[[UIColor redColor], [UIColor blueColor]])
- (void)setGradientColors:(NSArray *)colors;
@end
NS_ASSUME_NONNULL_END
//漸變邊框
@interface PROpenVipHeadTagView()
@property (nonatomic, strong) UILabel *preLab;
@property (nonatomic, strong) PRGradientLabel *gradientLabel;
@end
@implementation PROpenVipHeadTagView
- (instancetype)initWithFrame:(CGRect)frame {
? ? if (self = [super initWithFrame:frame]) {
? ? ? ? [self initUI];
? ? }
? ? return self;
}
- (void)initUI {
? ? __weak typeof(self) wSelf = self;
? ? dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.05 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
? ? ? ? [wSelf showEffectCss];
? ? ? ? [wSelf setupGradientBorder];
? ? ? ? [wSelf setCornerRadius:self.height/2];
? ? ? ? [wSelf showText];
? ? });
}
- (void)showEffectCss {
? ? if (@available(iOS 24.0,*)) {
? ? ? ? UIGlassEffect *eView = [UIGlassEffect effectWithStyle:UIGlassEffectStyleClear];
? ? ? ? UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:eView];
? ? ? ? [self addSubview:effectView];
? ? ? ? effectView.backgroundColor = [[UIColor colrWithHexStr:@"#111217"] colorWithAlphaComponent:0.5];
? ? ? ? [effectView setCornerRadius:self.height/2];
? ? ? ? [effectView mas_makeConstraints:^(MASConstraintMaker *make) {
? ? ? ? ? ? make.edges.equalTo(self);
? ? ? ? }];
? ? } else {
? ? ? ? UIBlurEffect *eView = [UIBlurEffect effectWithStyle:UIBlurEffectStyleRegular];
? ? ? ? UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:eView];
? ? ? ? [self addSubview:effectView];
? ? ? ? effectView.backgroundColor = [[UIColor colrWithHexStr:@"#111217"] colorWithAlphaComponent:0.5];
? ? ? ? [effectView setCornerRadius:self.height/2];
? ? ? ? [effectView mas_makeConstraints:^(MASConstraintMaker *make) {
? ? ? ? ? ? make.edges.equalTo(self);
? ? ? ? }];
? ? }
}
- (void)showText {
? ? // 1. 創(chuàng)建漸變 label(子類方式)
? ? self.gradientLabel = [[PRGradientLabel alloc] initWithFrame:CGRectMake(0, 0, self.width * 2, self.height * 2)];
? ? self.gradientLabel.text = NSLocalizedString(@"Premium",nil);
? ? self.gradientLabel.textColor = [UIColor clearColor];
? ? self.gradientLabel.font = [UIFont boldSystemFontOfSize:16 * kSizeWidth * 2];
? ? // 設(shè)置漸變(從紅色到藍(lán)色,水平方向)
? ? [self addSubview:self.gradientLabel];
? ? [self.gradientLabel mas_makeConstraints:^(MASConstraintMaker *make) {
? ? ? ? make.center.equalTo(self);
? ? }];
? ? CGAffineTransform scaleTransform = CGAffineTransformMakeScale(0.5, 0.5);
? ? __weak typeof(self) wSelf = self;
? ? dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.05 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
? ? ? ? [wSelf.gradientLabel setGradientColors:@[[UIColor colrWithHexStr:@"#AABDDB"], [UIColor colrWithHexStr:@"#E5DFF9"],[UIColor colrWithHexStr:@"#8DB6FF"],[UIColor colrWithHexStr:@"#8DB6FF"],[UIColor colrWithHexStr:@"#8DB6FF"]]];
? ? ? ? wSelf.gradientLabel.transform = scaleTransform;
? ? });
}
- (void)setupGradientBorder {
? ? // 1. 配置漸變層
? ? CAGradientLayer *gradientLayer = [CAGradientLayer layer];
? ? gradientLayer.frame = self.bounds;
? ? // 漸變顏色(示例:從紅色到藍(lán)色)
? ? gradientLayer.colors = @[
? ? ? ? (id)[UIColor colrWithHexStr:@"#AABDDB"].CGColor,
? ? ? ? (id)[UIColor colrWithHexStr:@"#D2CFE3"].CGColor,
? ? ? ? (id)[UIColor colrWithHexStr:@"#7188B4"].CGColor,
? ? ? ? (id)[UIColor colrWithHexStr:@"#7188B4"].CGColor,
? ? ? ? (id)[UIColor colrWithHexStr:@"#7188B4"].CGColor
? ? ];
? ? // 漸變方向(從左到右,也可改為從上到下等)
? ? gradientLayer.startPoint = CGPointMake(0, 0.5);
? ? gradientLayer.endPoint = CGPointMake(1, 0.5);
? ? // 2. 配置描邊路徑(CAShapeLayer)
? ? CAShapeLayer *borderLayer = [CAShapeLayer layer];
? ? borderLayer.frame = self.bounds;
? ? // 描邊路徑為視圖的邊框(帶圓角,可自定義)
? ? CGFloat cornerRadius = self.height/2; // 圓角半徑
? ? CGFloat borderWidth = 3 * kSizeWidth;? // 描邊寬度
? ? borderLayer.path = [UIBezierPath bezierPathWithRoundedRect:borderLayer.bounds
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? cornerRadius:cornerRadius].CGPath;
? ? // 僅顯示描邊,不填充
? ? borderLayer.fillColor = [UIColor clearColor].CGColor;
? ? borderLayer.strokeColor = [UIColor blackColor].CGColor; // 顏色不影響,蒙版只關(guān)心路徑
? ? borderLayer.lineWidth = borderWidth;
? ? // 3. 關(guān)鍵:用描邊路徑作為漸變層的蒙版
? ? gradientLayer.mask = borderLayer;
? ? // 4. 添加到視圖圖層
? ? [self.layer addSublayer:gradientLayer];
? ? // 5. 可選:如果視圖大小會(huì)變,需要更新圖層
? ? self.layer.masksToBounds = YES; // 避免描邊超出視圖范圍
}
// 視圖大小改變時(shí),更新漸變層和路徑
- (void)layoutSubviews {
? ? [super layoutSubviews];
? ? // 找到漸變層并更新frame
? ? for (CALayer *layer in self.layer.sublayers) {
? ? ? ? if ([layer isKindOfClass:[CAGradientLayer class]]) {
? ? ? ? ? ? layer.frame = self.bounds;
? ? ? ? ? ? // 更新蒙版路徑(描邊路徑)
? ? ? ? ? ? CAShapeLayer *borderLayer = (CAShapeLayer *)layer.mask;
? ? ? ? ? ? if (borderLayer) {
? ? ? ? ? ? ? ? borderLayer.frame = self.bounds;
? ? ? ? ? ? ? ? borderLayer.path = [UIBezierPath bezierPathWithRoundedRect:borderLayer.bounds
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? cornerRadius:self.height/2].CGPath;
? ? ? ? ? ? }
? ? ? ? ? ? break;
? ? ? ? }
? ? }
}
@end
//漸變文字
@interface PRGradientLabel()
@property (nonatomic, strong) NSArray<UIColor *> * colors;
@end
@implementation PRGradientLabel
- (void)setGradientColors:(NSArray<UIColor *> *)colors {
? ? self.colors = colors;
? ? // 1. 轉(zhuǎn)換顏色為 CGColor
? ? NSMutableArray *cgColors = [NSMutableArray array];
? ? for (UIColor *color in self.colors) {
? ? ? ? [cgColors addObject:(__bridge id)color.CGColor];
? ? }
? ? // 2. 創(chuàng)建漸變層
? ? CAGradientLayer *gradientLayer = [CAGradientLayer layer];
? ? gradientLayer.colors = cgColors;
? ? gradientLayer.startPoint = CGPointMake(0, 0.5);
? ? gradientLayer.endPoint = CGPointMake(1, 0.5);
? ? gradientLayer.frame = self.bounds; // 漸變層大小與 label 一致
? ? // 3. 創(chuàng)建文字蒙版(關(guān)鍵:用 label 的文字作為蒙版裁剪漸變層)
? ? self.backgroundColor = [UIColor clearColor]; // 確保背景透明
//? ? gradientLayer.mask = self.layer; // 用 label 的圖層(文字)作為蒙版
? ? // 4. 將漸變層添加到 label 底層
? ? [self.layer addSublayer:gradientLayer];
? ? // 創(chuàng)建文字屬性
? ? NSDictionary *attributes = @{
? ? ? ? NSFontAttributeName: self.font, // 字體
? ? ? ? NSForegroundColorAttributeName: [UIColor colrWithHexStr:@"#000000"]// 文字顏色
? ? };
? ? // 創(chuàng)建文字遮罩
? ? CATextLayer *textMask = [[CATextLayer alloc] init];
? ? textMask.frame = self.bounds;
? ? textMask.string = [[NSAttributedString alloc] initWithString:self.text attributes:attributes];
? ? textMask.alignmentMode = kCAAlignmentCenter;
? ? // 將文字遮罩設(shè)置為漸變圖層的遮罩
? ? gradientLayer.mask = textMask;
}
- (void)drawRect:(CGRect)rect {
}
@end