IOS之代理文字点击变大变小

IOS之代理文字点击变大变小

请添加图片描述
重点是掌握代理的使用,否则也失去次此意义

#import "ViewController.h"
#import "AHLJFontSizePicker.h"
#define kScreenW [UIScreen mainScreen].bounds.size.width
@interface ViewController ()<AHLJFontSizePickerDelegate>
@property (weak, nonatomic) IBOutlet UILabel *lbl;
@end
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    AHLJFontSizePicker *pickView = [[AHLJFontSizePicker alloc] initWithFrame:CGRectZero];
    pickView.delegate = self;
    pickView.frame = CGRectMake(0, 150, kScreenW, 80);
    pickView.backgroundColor = UIColor.systemTealColor;
    [self.view addSubview:pickView];
    
}
- (void)fontSizePickerDidBtnClick:(AHLJFontSizePicker *)picker didSetFontSize:(FontSizeEnum)fontSize{
    switch (fontSize) {
        case FontSizeSmall:
            self.lbl.font = [UIFont systemFontOfSize:11 weight:UIFontWeightBold];
            break;
        case FontSizeMiddle:
            self.lbl.font = [UIFont systemFontOfSize:15 weight:UIFontWeightBold];
            break;
        case FontSizeBig:
            self.lbl.font = [UIFont systemFontOfSize:18 weight:UIFontWeightBold];
            break;
        case FontSizeSuperBig:
            self.lbl.font = [UIFont systemFontOfSize:22 weight:UIFontWeightBold];
            break;
        default:
            break;
    }
    
    
}
@end

//
//  AHLJFontSizePicker.h
//  图片点击文字变大或者变小
//
//  Created by lujun on 2021/10/20.
//

#import <UIKit/UIKit.h>
@class AHLJFontSizePicker;
typedef enum {
    FontSizeSmall = 0,
    FontSizeMiddle = 1,
    FontSizeBig = 2,
    FontSizeSuperBig = 3
    
}FontSizeEnum;
NS_ASSUME_NONNULL_BEGIN
@protocol AHLJFontSizePickerDelegate <NSObject>
-(void)fontSizePickerDidBtnClick:(AHLJFontSizePicker *)picker didSetFontSize:(FontSizeEnum)fontSize;
@end
@interface AHLJFontSizePicker : UIView
@property(nonatomic,weak)id<AHLJFontSizePickerDelegate> delegate;
@end
NS_ASSUME_NONNULL_END

#define kBtnCount 4
#import "AHLJFontSizePicker.h"
#define btnH1 30
#define  btnY1 5
#define btnW1 self.frame.size.width / btnCount
@interface AHLJFontSizeButton : UIButton
@end
@implementation AHLJFontSizeButton
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        self.adjustsImageWhenHighlighted  = NO;
        self.imageView.contentMode = UIViewContentModeScaleAspectFit;
    }
    
    return  self;
}
- (void)setHighlighted:(BOOL)highlighted{}
@end
@interface AHLJFontSizePicker()
@property(nonatomic,weak)AHLJFontSizeButton *selectedButton;
@property(nonatomic,weak)UIImageView *topLine;
@property(nonatomic,weak)UIImageView *bottomLine;
@property(nonatomic,weak)UIImageView *slider;
@end
@implementation AHLJFontSizePicker
- (UIImageView *)topLine{
    if(!_topLine){
        UIImageView *topLineImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"正文字号-滑条红"]];
        _topLine = topLineImg;
        [self addSubview:_topLine];
    }
    return _topLine;
}
- (void)drawRect:(CGRect)rect{
    [super drawRect:rect];
    CGFloat sW = [UIScreen mainScreen].bounds.size.width;
    CGFloat btnW = sW / kBtnCount;
    CGFloat btnY = 5;
    CGFloat btnH = 30;
    NSArray *arrNor = @[@"正文字号-小(默认)",@"正文字号-中(默认)",@"正文字号-大(默认)",@"正文字号-大+(默认)"];
    NSArray *arrSel = @[@"正文字号-小",@"正文字号-中",@"正文字号-大",@"正文字号-大+"];
    for(int i=0;i<kBtnCount;i++){
        AHLJFontSizeButton *btn = [[AHLJFontSizeButton alloc] initWithFrame:CGRectMake(i * btnW, btnY, btnW, btnH)];
        [btn setImage:[UIImage imageNamed:arrNor[i]] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:arrSel[i]] forState:UIControlStateSelected];
        btn.tag = i;
        [self addSubview:btn];
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    AHLJFontSizeButton *firstBtn = (AHLJFontSizeButton *)self.subviews.firstObject;
    AHLJFontSizeButton *lastBtn = (AHLJFontSizeButton *)self.subviews.lastObject;
    UIImageView *slider = [[UIImageView alloc] initWithImage: [UIImage imageNamed:@"正文字号-滑块"]];
    [self addSubview:slider];
    self.slider = slider;
    UIImageView *bottomLine = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"正文字号-滑条"]];
    CGFloat bottomLineX = firstBtn.center.x - slider.frame.size.width*0.5;
    CGFloat bottomLineMaxX = lastBtn.center.x + slider.frame.size.width*0.5;
    CGFloat bottomY = 35 + 15;
    bottomLine.frame = CGRectMake(bottomLineX,bottomY , bottomLineMaxX - bottomLineX, 3);
    [self addSubview:bottomLine];
    self.bottomLine = bottomLine;
    slider.center = CGPointMake( firstBtn.center.x, bottomLine.center.y);
    [self btnClick:firstBtn];
}
-(void)btnClick:(AHLJFontSizeButton *)btn{
    self.selectedButton.selected = NO;
    btn.selected = YES;
    self.selectedButton = btn;
    self.slider.center =CGPointMake(self.selectedButton.center.x, self.bottomLine.center.y);
    self.topLine.frame = CGRectMake(self.bottomLine.frame.origin.x, self.bottomLine.frame.origin.y, self.slider.center.x-self.slider.frame.size.width-7, 3);
    if([self.delegate respondsToSelector:@selector(fontSizePickerDidBtnClick:didSetFontSize:)]){
        
        [self.delegate fontSizePickerDidBtnClick:self didSetFontSize:(int)btn.tag];
    }
}
@end

https://e.coding.net/lujun1/wangyecebianlan/IOSSmallPictureAndBig.git

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值