Files
peko-ios/YuMi/Modules/YMRoom/View/BaseUIContainerView/XPRoomRankEntranceView.m

139 lines
3.9 KiB
Mathematica
Raw Normal View History

2023-07-14 18:50:55 +08:00
//
// YMRoomRankEntranceView.m
// YUMI
//
// Created by YUMI on 2022/4/27.
//
#import "XPRoomRankEntranceView.h"
///Third
#import <Masonry/Masonry.h>
#import "DJDKMIMOMColor.h"
///Tool
#import "NetImageView.h"
@interface XPRoomRankEntranceView ()
///
@property (nonatomic, strong) UIView *bgView;
///
@property (nonatomic, strong) UILabel *titleLabel;
///
@property (nonatomic, strong) UIImageView *iconImageView;
@end
@implementation XPRoomRankEntranceView
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
[self initSubViews];
[self initSubViewConstraints];
}
return self;
}
#pragma mark - Private Method
- (void)initSubViews {
[self addSubview:self.bgView];
2024-05-21 14:17:30 +08:00
[self.bgView addSubview:self.titleLabel];
[self.bgView addSubview:self.iconImageView];
2023-07-14 18:50:55 +08:00
}
2024-05-21 14:17:30 +08:00
- (void)initSubViewConstraints {
[self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.bottom.trailing.leading.equalTo(self);
}];
2023-09-27 12:35:29 +08:00
[self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
2024-05-21 14:17:30 +08:00
make.centerY.mas_equalTo(self.bgView);
2024-05-22 19:36:51 +08:00
make.leading.mas_equalTo(26);
2024-04-10 17:39:37 +08:00
make.trailing.mas_equalTo(-4);
2023-09-27 12:35:29 +08:00
}];
[self.iconImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
2024-05-22 19:36:51 +08:00
make.width.mas_equalTo(18);
make.height.mas_equalTo(15);
2024-05-21 14:17:30 +08:00
make.centerY.mas_equalTo(self.bgView);
2024-05-22 19:36:51 +08:00
make.leading.mas_equalTo(5);
2023-09-27 12:35:29 +08:00
}];
2023-07-14 18:50:55 +08:00
}
BOOL isNumeric(NSString *string) {
NSError *error = nil;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"^[0-9]+$" options:0 error:&error];
if (error) {
NSLog(@"Error creating regex: %@", error.localizedDescription);
return NO;
}
NSRange range = NSMakeRange(0, string.length);
NSTextCheckingResult *match = [regex firstMatchInString:string options:0 range:range];
return match != nil;
}
- (NSString *)formatCoins:(NSInteger)coins {
NSString *formattedString;
if (coins >= 1000000) {
// M1
double valueInMillions = coins / 1000000.0;
formattedString = [NSString stringWithFormat:@"%.1fM", valueInMillions];
} else if (coins >= 1000) {
// K1
double valueInThousands = coins / 1000.0;
formattedString = [NSString stringWithFormat:@"%.1fK", valueInThousands];
} else {
//
formattedString = [NSString stringWithFormat:@"%ld", (long)coins];
}
return formattedString;
}
2023-07-14 18:50:55 +08:00
#pragma mark - Getters And Setters
- (void)setTitle:(NSString *)title {
if (![title isKindOfClass:[NSString class]]) {
return;
}
if (title.length == 0) {
return;
}
if (isNumeric(title)) {
_titleLabel.text = [self formatCoins:[title integerValue]];
} else {
_titleLabel.text = title;
}
2023-07-14 18:50:55 +08:00
}
- (UIView *)bgView {
if (_bgView == nil) {
_bgView = [[UIView alloc] init];
2024-05-21 14:17:30 +08:00
_bgView.backgroundColor = UIColorRGBAlpha(0xFFFFFF, 0.2);
2023-07-14 18:50:55 +08:00
_bgView.layer.cornerRadius = 12;
_bgView.layer.masksToBounds = YES;
}
return _bgView;
}
- (UIImageView *)iconImageView {
if (_iconImageView == nil) {
_iconImageView = [[UIImageView alloc] init];
2024-05-21 14:17:30 +08:00
_iconImageView.image = [[UIImage imageNamed:@"room_rank_iocn"]ms_SetImageForRTL];
2023-07-14 18:50:55 +08:00
_iconImageView.contentMode = UIViewContentModeScaleAspectFit;
2024-04-10 17:39:37 +08:00
2023-07-14 18:50:55 +08:00
}
return _iconImageView;
}
- (UILabel *)titleLabel {
if (_titleLabel == nil) {
_titleLabel = [[UILabel alloc] init];
2024-05-21 14:17:30 +08:00
_titleLabel.textColor = [UIColor colorWithWhite:1 alpha:0.8];
_titleLabel.font = [UIFont systemFontOfSize:9 weight:UIFontWeightBold];
_titleLabel.text = @"0"; YMLocalizedString(@"XPRoomRankEntranceView0");
2024-04-10 17:39:37 +08:00
_titleLabel.numberOfLines = 2;
2023-07-14 18:50:55 +08:00
}
return _titleLabel;
}
@end