生活随笔
收集整理的這篇文章主要介紹了
IOS之代理文字点击变大变小
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
IOS之代理文字點(diǎn)擊變大變小
重點(diǎn)是掌握代理的使用,否則也失去次此意義
#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
#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
:@"正文字號(hào)-滑條紅"]];_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
= @[@"正文字號(hào)-小(默認(rèn))",@"正文字號(hào)-中(默認(rèn))",@"正文字號(hào)-大(默認(rèn))",@"正文字號(hào)-大+(默認(rèn))"];NSArray
*arrSel
= @[@"正文字號(hào)-小",@"正文字號(hào)-中",@"正文字號(hào)-大",@"正文字號(hào)-大+"];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
:@"正文字號(hào)-滑塊"]];[self addSubview
:slider
];self.slider
= slider
;UIImageView
*bottomLine
= [[UIImageView alloc
] initWithImage
:[UIImage imageNamed
:@"正文字號(hào)-滑條"]];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
總結(jié)
以上是生活随笔為你收集整理的IOS之代理文字点击变大变小的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。