139 lines
3.9 KiB
Objective-C
139 lines
3.9 KiB
Objective-C
//
|
||
// 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];
|
||
[self.bgView addSubview:self.titleLabel];
|
||
[self.bgView addSubview:self.iconImageView];
|
||
|
||
}
|
||
|
||
- (void)initSubViewConstraints {
|
||
[self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
|
||
make.top.bottom.trailing.leading.equalTo(self);
|
||
}];
|
||
|
||
[self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
|
||
make.centerY.mas_equalTo(self.bgView);
|
||
make.leading.mas_equalTo(26);
|
||
make.trailing.mas_equalTo(-4);
|
||
}];
|
||
[self.iconImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
|
||
make.width.mas_equalTo(18);
|
||
make.height.mas_equalTo(15);
|
||
make.centerY.mas_equalTo(self.bgView);
|
||
make.leading.mas_equalTo(5);
|
||
}];
|
||
}
|
||
|
||
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) {
|
||
// 转换为M单位,保留小数点后1位
|
||
double valueInMillions = coins / 1000000.0;
|
||
formattedString = [NSString stringWithFormat:@"%.1fM", valueInMillions];
|
||
} else if (coins >= 1000) {
|
||
// 转换为K单位,保留小数点后1位
|
||
double valueInThousands = coins / 1000.0;
|
||
formattedString = [NSString stringWithFormat:@"%.1fK", valueInThousands];
|
||
} else {
|
||
// 保持原始值
|
||
formattedString = [NSString stringWithFormat:@"%ld", (long)coins];
|
||
}
|
||
return formattedString;
|
||
}
|
||
#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;
|
||
}
|
||
}
|
||
|
||
- (UIView *)bgView {
|
||
if (_bgView == nil) {
|
||
_bgView = [[UIView alloc] init];
|
||
_bgView.backgroundColor = UIColorRGBAlpha(0xFFFFFF, 0.2);
|
||
_bgView.layer.cornerRadius = 12;
|
||
_bgView.layer.masksToBounds = YES;
|
||
}
|
||
return _bgView;
|
||
}
|
||
|
||
- (UIImageView *)iconImageView {
|
||
if (_iconImageView == nil) {
|
||
_iconImageView = [[UIImageView alloc] init];
|
||
_iconImageView.image = [[UIImage imageNamed:@"room_rank_iocn"]ms_SetImageForRTL];
|
||
_iconImageView.contentMode = UIViewContentModeScaleAspectFit;
|
||
|
||
}
|
||
return _iconImageView;
|
||
}
|
||
|
||
- (UILabel *)titleLabel {
|
||
if (_titleLabel == nil) {
|
||
_titleLabel = [[UILabel alloc] init];
|
||
_titleLabel.textColor = [UIColor colorWithWhite:1 alpha:0.8];
|
||
_titleLabel.font = [UIFont systemFontOfSize:9 weight:UIFontWeightBold];
|
||
_titleLabel.text = @"0"; YMLocalizedString(@"XPRoomRankEntranceView0");
|
||
_titleLabel.numberOfLines = 2;
|
||
}
|
||
return _titleLabel;
|
||
}
|
||
|
||
|
||
@end
|