cdts/xdts-ios 3/TreeHole/Code/Utility/MTUIViewCreate/PYThemeButton.m

258 lines
9.8 KiB
Mathematica
Raw Normal View History

2023-07-27 09:20:00 +08:00
//
// PYThemeButton.m
// Food
//
// Created by ko1o on 2019/7/16.
// Copyright © 2019 ko1o. All rights reserved.
//
#import "PYThemeButton.h"
#import "UITextField+MTLimit.h"
#import "TZImagePickerController+MTImagePicker.h"
#import "UIImage+TintColor.h"
@interface PYThemeButton()
@property (nonatomic, assign) BOOL isForNav;
@end
@implementation PYThemeButton
+ (instancetype)buttonWithTitle:(NSString *)title action:(void(^)(UIButton *))action
{
return [self buttonWithTitle:title width:SCREEN_WIDTH - FIX_SIZE(15) * 2 action:action];
}
+ (instancetype)middleButtonWithTitle:(NSString *)title action:(void(^)(UIButton *))action
{
return [self buttonWithTitle:title width:FIX_SIZE(124) action:action];
}
+ (instancetype)smallButtonWithTitle:(NSString *)title action:(void (^)(UIButton * _Nonnull))action {
return [self buttonWithTitle:title width:FIX_SIZE(61) action:action];
}
+ (instancetype)buttonWithTitle:(NSString *)title width:(CGFloat)width action:(void(^)(UIButton *))action
{
BOOL isSmall = width <= FIX_SIZE(61);
PYThemeButton *button = [PYThemeButton buttonWithType:UIButtonTypeCustom];
[button setTitle:title forState:UIControlStateNormal];
[button setTitleColor:COLOR_WITH_RGB(0x040000) forState:UIControlStateNormal];
button.width = width;
button.centerX = SCREEN_WIDTH * 0.5;
button.height = isSmall ? FIX_SIZE(31) : FIX_SIZE(52);
button.layer.cornerRadius = isSmall ? FIX_SIZE(12) : FIX_SIZE(20);
button.clipsToBounds = YES;
button.enabled = YES;
[button addTarget:button action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
[button setTintAdjustmentMode:UIViewTintAdjustmentModeNormal];
button.titleLabel.font = MT_FONT_MEDIUM_SIZE(isSmall ? 13 : 15);
[button setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 10)];
button.action = action;
button.backgroundColor = THEME_COLOR;
return button;
}
+ (instancetype)nextButtonWithAction:(void(^)(UIButton *button))action {
PYThemeButton *button = [PYThemeButton buttonWithType:UIButtonTypeCustom];
[button setImage:[ImageNamed(@"TH_next_icon") mt_imageWithTintColor:COLOR_WITH_RGB(0x040000)] forState:UIControlStateNormal];
button.width = FIX_SIZE(124);
button.height = FIX_SIZE(52);
button.layer.cornerRadius = FIX_SIZE(20);
button.clipsToBounds = YES;
button.enabled = YES;
[button addTarget:button action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
[button setImageEdgeInsets:UIEdgeInsetsMake(FIX_SIZE(42), FIX_SIZE(6), FIX_SIZE(42), FIX_SIZE(6))];
button.action = action;
button.backgroundColor = THEME_COLOR;
button.centerX = SCREEN_WIDTH * 0.5;
return button;
}
+ (instancetype)normalButtonWithTitle:(NSString *)title action:(void(^)(UIButton *))action
{
PYThemeButton *button = [PYThemeButton buttonWithType:UIButtonTypeCustom];
[button setTitle:title forState:UIControlStateNormal];
[button setTitleColor:UIColor.blackColor forState:UIControlStateNormal];
button.width = SCREEN_WIDTH - FIX_SIZE(32.5) * 2;
button.x = FIX_SIZE(32.5);
button.height = FIX_SIZE(40);
button.layer.cornerRadius = button.height * 0.5;
button.clipsToBounds = YES;
button.enabled = YES;
button.layer.borderWidth = 1.0;
button.layer.borderColor = UIColor.blackColor.CGColor;
[button addTarget:button action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
button.titleLabel.font = MT_FONT_MEDIUM_SIZE(18.0);
[button setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 10)];
button.action = action;
button.backgroundColor = UIColor.whiteColor;
return button;
}
+ (instancetype)navItemWithImageName:(NSString *)imageName action:(void(^)(UIButton *button))action {
PYThemeButton *button = [PYThemeButton buttonWithType:UIButtonTypeCustom];
[button setImage:ImageNamed(imageName) forState:UIControlStateNormal];
button.width = FIX_SIZE(43);
button.height = FIX_SIZE(36);
[button addTarget:button action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
button.layer.cornerRadius = FIX_SIZE(16);
button.backgroundColor = COLOR_WITH_RGB_A(0x222222, 0.3);
button.action = action;
return button;
}
+ (instancetype)normalNavItemWithImageName:(NSString *)imageName action:(void(^)(UIButton *button))action {
PYThemeButton *button = [PYThemeButton buttonWithType:UIButtonTypeCustom];
[button setImage:ImageNamed(imageName) forState:UIControlStateNormal];
button.width = FIX_SIZE(36);
button.height = FIX_SIZE(36);
[button addTarget:button action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
button.action = action;
return button;
}
+ (instancetype)navItemWithTitle:(NSString *)title action:(void(^)(UIButton *button))action {
PYThemeButton *button = [PYThemeButton buttonWithType:UIButtonTypeCustom];
button.titleLabel.font = MT_FONT_REGULAR_SIZE(15);
[button setTitle:title forState:UIControlStateNormal];
[button setTitleColor:THEME_COLOR forState:UIControlStateNormal];
[button setTitleColor:TITLE_COLOR forState:UIControlStateDisabled];
button.width = FIX_SIZE(43);
button.height = FIX_SIZE(36);
[button addTarget:button action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
button.action = action;
return button;
}
+ (instancetype)addImageButtonWithAction:(void(^)(UIButton *btn))didAddAction {
PYThemeButton *button = [PYThemeButton buttonWithType:UIButtonTypeCustom];
button.width = FIX_SIZE(90);
button.height = FIX_SIZE(90);
button.backgroundColor = COLOR_WITH_RGB(0x2D2F42);
button.layer.cornerRadius = FIX_SIZE(13);
// button.layer.borderWidth = 0.7;
button.clipsToBounds = YES;
// button.layer.borderColor = COLOR_WITH_RGB(0xE5E5E5).CGColor;
[button setImage:ImageNamed(@"TH_report_photo_add_icon") forState:UIControlStateNormal];
[button addTouchUpInsideWithAction:^(UIButton * btn) {
TZImagePickerController *picker = [TZImagePickerController mt_imagePickerWithMaxImagesCount:1 didFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
if (photos.count) {
((PYThemeButton *)btn).addedImage = photos.firstObject;
btn.layer.borderWidth = 0.0;
btn.imageView.contentMode = UIViewContentModeScaleAspectFill;
[btn setImage:photos.firstObject forState:UIControlStateNormal];
if (didAddAction) {
didAddAction(btn);
}
}
}];
[[PYAppService currentViewController] presentViewController:picker animated:YES completion:nil];
}];
return button;
}
- (void)setEnabled:(BOOL)enabled
{
[super setEnabled:enabled];
self.alpha = enabled ? 1.0 : 0.15;
}
- (void)action:(UIButton *)sender
{
self.userInteractionEnabled = NO;
if (self.action) {
self.action(sender);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
self.userInteractionEnabled = YES;
});
}
}
- (void)btnLayerForColor:(UIColor*)forColor toColor:(UIColor*)toColor andWithCornerRadius:(float)cornerRadius{
CAGradientLayer *gl = [CAGradientLayer layer];
gl.frame = CGRectMake(0,0,self.width,self.height);
gl.startPoint = CGPointMake(0, 0.5);
gl.endPoint = CGPointMake(1, 0.5);
gl.colors = @[(__bridge id)forColor.CGColor, (__bridge id)toColor.CGColor];
gl.locations = @[@(0), @(1.0f)];
[self.layer addSublayer:gl];
self.layer.cornerRadius = cornerRadius;
self.layer.masksToBounds = YES;
[self bringSubviewToFront:self.titleLabel];
[self bringSubviewToFront:self.imageView];
}
@end
@interface PYThemeTextField () <MTTextFieldLimitDelegate>
@end
@implementation PYThemeTextField
+ (instancetype)fieldWithPlaceholder:(NSString *)placeholder maxLength:(int)maxLength {
PYThemeTextField *field = [[PYThemeTextField alloc] init];
field.backgroundColor = COLOR_WITH_RGB(0x2D2F42);
field.height = FIX_SIZE(52);
field.width = SCREEN_WIDTH - FIX_SIZE(35) * 2;
field.centerX = SCREEN_WIDTH * 0.5;
field.layer.cornerRadius = FIX_SIZE(20);
UITextField *textField = [[UITextField alloc] init];
textField.placeholder = placeholder;
textField.font = NORMAL_FONT;
textField.textColor = UIColor.whiteColor;
textField.height = field.height;
textField.x = FIX_SIZE(17);
[textField mt_setupLimitTextFieldWithBytesLength:maxLength delegate:field];
[field addSubview:textField];
field.textField = textField;
dispatch_async(dispatch_get_main_queue(), ^{
textField.width = field.width - textField.x * 2;
});
return field;
}
+ (instancetype)smallFieldWithPlaceholder:(NSString *)placeholder maxLength:(int)maxLength {
PYThemeTextField *field = [[PYThemeTextField alloc] init];
field.backgroundColor = COLOR_WITH_RGB(0xF3F9FB);
field.height = FIX_SIZE(48);
field.width = SCREEN_WIDTH - FIX_SIZE(15) * 2;
field.centerX = SCREEN_WIDTH * 0.5;
field.layer.cornerRadius = FIX_SIZE(20);
UITextField *textField = [[UITextField alloc] init];
textField.placeholder = placeholder;
textField.font = SMALL_FONT;
textField.textColor = COLOR_WITH_RGB(0x474747);
textField.height = field.height;
textField.x = FIX_SIZE(15);
[textField mt_setupLimitTextFieldWithBytesLength:30 delegate:field];
textField.width = field.width - textField.x * 2;
[field addSubview:textField];
field.textField = textField;
field.layer.borderColor = COLOR_WITH_RGB(0xE5E5E5).CGColor;
field.layer.borderWidth = 0.5;
return field;
}
- (NSString *)text {
return self.textField.text;
}
- (void)limitTextFieldDidChange:(UITextField *)textField {
if (self.textDidChangeAction) {
self.textDidChangeAction(self);
}
}
@end