cdts/xdts-ios 3/TreeHole/Code/Utility/MTUIViewCreate/UIView+MTCreate.m

327 lines
12 KiB
Mathematica
Raw Normal View History

2023-07-27 09:20:00 +08:00
//
// UIView+MTCreate.m
// Meet
//
// Created by ko1o on 2018/9/13.
// Copyright © 2018 ko1o. All rights reserved.
//
#import "UIView+MTCreate.h"
#import "UIImage+Extender.h"
#import "YYText.h"
#import <objc/runtime.h>
@implementation UIButton (MTCreate)
+ (UIButton *)mt_commonButtonWithTitle:(NSString *)title target:(id)target action:(SEL)action
{
UIFont *font = MT_FONT_REGULAR_SIZE(18);
UIButton * button = [self mt_roundButtonWithFrame:CGRectMake(0, 0,FIX_SIZE(250), FIX_SIZE(40))
title:title
titleColor:[UIColor whiteColor]
backgroundColor:COLOR_WITH_RGB(0x4557DE )
adjustsImageWhenHighlighted:YES
target:target
action:action];
button.titleLabel.font = font;
return button;
}
+ (UIButton *)mt_roundButtonWithFrame:(CGRect)frame
title:(NSString *)title
titleColor:(UIColor *)titleColor
backgroundColor:(UIColor *)backgroundColor
{
return [self mt_roundButtonWithFrame:frame title:title titleColor:titleColor backgroundColor:backgroundColor adjustsImageWhenHighlighted:YES target:nil action:nil];
}
+ (UIButton *)mt_roundButtonWithFrame:(CGRect)frame
title:(NSString *)title
titleColor:(UIColor *)titleColor
backgroundColor:(UIColor *)backgroundColor
adjustsImageWhenHighlighted:(BOOL)adjustsImageWhenHighlighted
target:(id)target
action:(SEL)action
{
UIFont *font = MT_FONT_REGULAR_SIZE(18);
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTitle:title forState:UIControlStateNormal];
button.titleLabel.font = font;
button.layer.cornerRadius = frame.size.height * 0.5;
button.clipsToBounds = YES;
button.adjustsImageWhenHighlighted = adjustsImageWhenHighlighted;
CGSize size = frame.size;
if (!backgroundColor) {
backgroundColor = [UIColor whiteColor];
}
if (backgroundColor) {
CGSize newSize = CGSizeEqualToSize(size, CGSizeZero) ? CGSizeMake(10, 10): size;
UIImage *image = [UIImage imageWithColor:backgroundColor size:newSize] ;
[button setBackgroundImage:image forState:UIControlStateNormal];
if (adjustsImageWhenHighlighted) {
[button setBackgroundImage:[UIImage imageWithColor:[UIColor mt_colorWithColor:backgroundColor alphaScale:0.5] size:newSize] forState:UIControlStateHighlighted];
}
}
if (!titleColor) {
titleColor = [UIColor blackColor];
}
[button setTitleColor:titleColor forState:UIControlStateNormal];
[button setTitleColor:[UIColor mt_colorWithColor:titleColor alphaScale:0.5] forState:UIControlStateHighlighted];
button.frame = frame;
if (target) {
[button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
}
return button;
}
- (void)mt_centerVerticallyWithPadding:(float)padding
{
// reset before setting
self.imageEdgeInsets = UIEdgeInsetsZero;
self.titleEdgeInsets = UIEdgeInsetsZero;
CGSize imageSize = self.imageView.frame.size;
CGSize titleSize = self.titleLabel.frame.size;
CGFloat totalHeight = (imageSize.height + titleSize.height + padding);
self.imageEdgeInsets = UIEdgeInsetsMake(- (totalHeight - imageSize.height),
titleSize.width * 0.5,
0.0f,
- titleSize.width * 0.5);
self.titleEdgeInsets = UIEdgeInsetsMake(0.0f,
- imageSize.width,
- (totalHeight - titleSize.height),
0.0f);
}
// imagetitle
- (void)mt_imageRight
{
const CGFloat kDefaultPadding = 3.0f;
[self mt_imageRightWithPadding:kDefaultPadding];
}
- (void)mt_imageRightWithPadding:(float)padding
{
CGSize imageSize = self.imageView.frame.size;
CGSize titleSize = self.titleLabel.frame.size;
CGFloat totalWidth = (imageSize.width + titleSize.width + padding);
self.imageEdgeInsets = UIEdgeInsetsMake(0.0f,
titleSize.width,
0.0f,
- (titleSize.width + padding));
self.titleEdgeInsets = UIEdgeInsetsMake(0.0f,
- (imageSize.width + padding),
0.0,
(imageSize.width));
self.width = totalWidth;
}
- (void)mt_centerVertically
{
const CGFloat kDefaultPadding = 6.0f;
[self mt_centerVerticallyWithPadding:kDefaultPadding];
}
@end
@implementation UILabel (MTCreate)
+ (UILabel *)mt_labelWithText:(NSString *)text color:(UIColor *)color fontSize:(CGFloat )fontSize
{
UILabel * label = [[UILabel alloc]init];
label.textColor = color;
label.backgroundColor = [UIColor clearColor];
label.textAlignment = NSTextAlignmentLeft;
label.font = MT_FONT_REGULAR_NO_SCALE_SIZE(fontSize);
label.text = text;
return label;
}
+ (UILabel *)mt_titleLabelWithText:(NSString *)text {
UILabel *titleLabel = [[UILabel alloc] init];
titleLabel.text = text;
titleLabel.textColor = TITLE_COLOR;
titleLabel.font = NORMAL_MEDIUM_FONT;
[titleLabel sizeToFit];
return titleLabel;
}
- (CGSize)mt_sizeToFitMaxSize:(CGSize)maxSize
{
UIFont *font = self.font;
if (font == nil) {
return CGSizeZero;
}
if (self.numberOfLines != 0) {
maxSize.height = self.font.lineHeight * self.numberOfLines;
}
CGSize size = [self.text boundingRectWithSize:maxSize
options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading)
attributes: @{NSFontAttributeName:font}
context:nil].size;
if (self.attributedText) {
size = [self.attributedText boundingRectWithSize:maxSize options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading) context:nil].size;
}
size = CGSizeMake(size.width, ceil(size.height));
CGRect frame = self.frame;
frame.size = size;
self.frame = frame;
return size;
}
@end
@implementation UIView (MTCreateGlobal)
//YYLabel
//"
+ (YYLabel *)mt_createOperationTips:(NSString *)fullString
actionSubString:(NSString *)actionString
actionBlock:(void (^)(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) )actionBlock
{
YYLabel * yyLabel = [[YYLabel alloc]init];
NSString *string = fullString;//
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:string];
NSRange fullRange = NSMakeRange(0, string.length);
[attributedString yy_setColor:COLOR_WITH_RGB(0x777777) range:NSMakeRange(0, string.length)];
[attributedString yy_setFont:MT_FONT_REGULAR_SIZE(14) range:fullRange];
if (actionString.length > 0 && [fullString rangeOfString:actionString].length > 0) {
[attributedString yy_setTextHighlightRange:[string rangeOfString:actionString]
color:COLOR_WITH_RGB(0x0E3DF6)
backgroundColor:[UIColor clearColor]
userInfo:nil
tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
if (actionBlock) {
actionBlock(containerView,text,range,rect);
}
} longPressAction:nil];
}
yyLabel.numberOfLines = 0;
yyLabel.backgroundColor = COLOR_WITH_RGB(0xf9f9f9);
yyLabel.attributedText = attributedString;
yyLabel.textContainerInset = UIEdgeInsetsMake(FIX_SIZE(10), FIX_SIZE(10), FIX_SIZE(10), FIX_SIZE(10));
return yyLabel;
}
- (void)mt_addDashBorder:(CGFloat )lineWidth color:(UIColor *)color lineDashPattern:(NSArray *)lineDashPattern
{
CAShapeLayer *yourViewBorder = [CAShapeLayer layer];
yourViewBorder.strokeColor = color ?color.CGColor: [UIColor blackColor].CGColor;
yourViewBorder.fillColor = nil;
yourViewBorder.lineDashPattern = lineDashPattern ?: @[@4, @4];
yourViewBorder.frame = self.bounds;
yourViewBorder.lineWidth = lineWidth ;
yourViewBorder.path = [UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:self.layer.cornerRadius].CGPath;
yourViewBorder.name = @"mt_dashBorder_layer";
__block CALayer * oldBorderLayer = nil;
[self.layer.sublayers enumerateObjectsUsingBlock:^(CALayer * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if ([obj.name isEqualToString:yourViewBorder.name]) {
oldBorderLayer = obj;
}
}];
[oldBorderLayer removeFromSuperlayer];
[self.layer insertSublayer:yourViewBorder atIndex:0];
}
- (void)mt_removeDashBorder
{
__block CALayer *yourViewBorder = nil;
[[self.layer sublayers]enumerateObjectsUsingBlock:^(CALayer * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if ([obj.name isEqualToString:@"mt_dashBorder_layer"]) {
yourViewBorder = obj;
}
}];
[yourViewBorder removeFromSuperlayer];
}
static char addBordersKey;
- (void)mt_addBordersToEdge:(UIRectEdge)edge withColor:(UIColor *)color borderWidth:(CGFloat) borderWidth
{
NSMutableDictionary * borderMap = objc_getAssociatedObject(self, &addBordersKey) ?: [NSMutableDictionary dictionary];
objc_setAssociatedObject(self, &addBordersKey, borderMap, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
UIView * (^addSubViewBlock)(UIRectEdge edge,UIViewAutoresizing autoresizing) = ^(UIRectEdge edge , UIViewAutoresizing autoresizing){
UIView *border = [UIView new];
border.backgroundColor = color;
[border setAutoresizingMask:autoresizing];
[self addSubview:border];
if (borderMap[@(edge)]) {
UIView * v = borderMap[@(edge)];
[v removeFromSuperview];
}
borderMap[@(edge)] = border;
return border;
};
if (edge & UIRectEdgeTop) {
UIView *border = addSubViewBlock(UIRectEdgeTop,UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin);
border.frame = CGRectMake(0, 0, self.frame.size.width, borderWidth);
}
if (edge & UIRectEdgeLeft) {
UIView *border = addSubViewBlock(UIRectEdgeLeft,UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleRightMargin);
border.frame = CGRectMake(0, 0, borderWidth, self.frame.size.height);
}
if (edge & UIRectEdgeBottom) {
UIView *border = addSubViewBlock(UIRectEdgeBottom,UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin);
border.frame = CGRectMake(0, self.frame.size.height - borderWidth, self.frame.size.width, borderWidth);
}
if (edge & UIRectEdgeRight) {
UIView *border = addSubViewBlock(UIRectEdgeRight,UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin);
border.frame = CGRectMake(self.frame.size.width - borderWidth, 0, borderWidth, self.frame.size.height);
}
}
- (void)mt_removeBorderOfEdge:(UIRectEdge)edge
{
NSMutableDictionary * borderMap = objc_getAssociatedObject(self, &addBordersKey) ?: [NSMutableDictionary dictionary];
NSArray * edges = @[@(UIRectEdgeTop),@(UIRectEdgeLeft),@(UIRectEdgeBottom),@(UIRectEdgeRight)];
[edges enumerateObjectsUsingBlock:^(NSNumber * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if ([obj intValue] & edge) {
UIView * v = borderMap[@([obj intValue])];
[v removeFromSuperview];
}
}];
}
@end
@implementation UIColor (MTHelper)
// alphaScale
+ (UIColor *)mt_colorWithColor:(UIColor *)originColor alphaScale:(CGFloat)alphaScale
{
CGFloat r = 0,g = 0,b = 0,alpah = 0;
if (!originColor) {
return nil;
}
[originColor getRed:&r green:&g blue:&b alpha:&alpah];
return [UIColor colorWithRed:r green:g blue:b alpha:alpah * alphaScale];
}
@end