This commit is contained in:
启星
2025-08-11 10:43:19 +08:00
commit fb2c58d96f
8839 changed files with 709982 additions and 0 deletions

View File

@@ -0,0 +1,229 @@
//
// NSString+BMUtils.m
// buymore
//
// Created by yuebin on 2020/6/1.
// Copyright © 2020 JLC. All rights reserved.
//
#import "NSString+BMUtils.h"
@implementation NSString (BMUtils)
- (NSString *)formatPriceYuan {
BOOL haveDot = [self containsString:@"."];
if (haveDot) {
NSString *result = [NSString stringWithFormat:@"%.2f", self.floatValue];
if ([result containsString:@".00"]) {
result = [result stringByReplacingOccurrencesOfString:@".00" withString:@""];
return result;
}
if ([result hasSuffix:@"0"]) {
result = [result substringToIndex:result.length-1];
return result;
}
return result;
}
return self;
}
- (NSAttributedString *)formatCouponAttributestring {
NSString *fixRawPrice = self.formatPriceYuan;
NSString *fullString = [NSString stringWithFormat:@"券后价¥%@", fixRawPrice];
NSMutableAttributedString *result = [[NSMutableAttributedString alloc]initWithString:fullString];
[result addAttribute:NSFontAttributeName value:YBFont(11) range:(NSMakeRange(0, 4))];
if ([fullString containsString:@"."]) {
NSArray *coms = [fullString componentsSeparatedByString:@"."];
NSString *targetString = [@"." stringByAppendingString:coms.lastObject];
NSRange targetRange = [fullString rangeOfString:targetString];
[result addAttribute:NSFontAttributeName value:YBBoldFont(11) range:targetRange];
}
return result;
}
- (NSAttributedString *)formatNoCouponAttributestring {
NSString *fixRawPrice = self.formatPriceYuan;
NSString *fullString = [NSString stringWithFormat:@"¥%@", fixRawPrice];
NSMutableAttributedString *result = [[NSMutableAttributedString alloc]initWithString:fullString];
[result addAttribute:NSFontAttributeName value:YBFont(11) range:(NSMakeRange(0, 1))];
if ([fullString containsString:@"."]) {
NSArray *coms = [fullString componentsSeparatedByString:@"."];
NSString *targetString = [@"." stringByAppendingString:coms.lastObject];
NSRange targetRange = [fullString rangeOfString:targetString];
[result addAttribute:NSFontAttributeName value:YBBoldFont(11) range:targetRange];
}
return result;
}
- (NSAttributedString *)formatSymbolAndDeleteAttributestring {
NSString *fixRawPrice = self.formatPriceYuan;
NSString *fullString = [NSString stringWithFormat:@"¥%@", fixRawPrice];
NSMutableAttributedString *result = [[NSMutableAttributedString alloc]initWithString:fullString];
[result addAttribute:NSFontAttributeName value:YBFont(11) range:(NSMakeRange(0, 1))];
if ([fullString containsString:@"."]) {
NSArray *coms = [fullString componentsSeparatedByString:@"."];
NSString *targetString = [@"." stringByAppendingString:coms.lastObject];
NSRange targetRange = [fullString rangeOfString:targetString];
[result addAttribute:NSFontAttributeName value:YBBoldFont(11) range:targetRange];
}
[result addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlinePatternSolid | NSUnderlineStyleSingle] range:NSMakeRange(0, result.length)];
return result;
}
- (NSAttributedString *)formatDeleteString {
NSMutableAttributedString *result = [[NSMutableAttributedString alloc] initWithString:self];
[result addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlinePatternSolid | NSUnderlineStyleSingle] range:NSMakeRange(0, self.length)];
return result;
}
- (NSAttributedString *)zoomWithString:(NSString *)targetStr WithSize:(float)fontSize {
NSMutableAttributedString *result = [[NSMutableAttributedString alloc] initWithString:self];
NSRange targetRange = [self rangeOfString:targetStr];
[result addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:fontSize] range:targetRange];
return result;
}
- (NSAttributedString *)att_deleteWithString:(NSString *)targetStr {
NSMutableAttributedString *result = [[NSMutableAttributedString alloc] initWithString:self];
NSRange targetRange = [self rangeOfString:targetStr];
[result addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlinePatternSolid | NSUnderlineStyleSingle] range:targetRange];
return result;
}
- (NSMutableAttributedString *)att_changeColorAndFontSizeWithString:(NSString *)targetStr WithColor:(nullable UIColor *)fontColor WithFont:(nullable UIFont *)font {
NSMutableAttributedString *result = [[NSMutableAttributedString alloc] initWithString:self];
NSRange targetRange = [self rangeOfString:targetStr];
if (fontColor) {
[result addAttribute:NSForegroundColorAttributeName value:fontColor range:targetRange];
}
if (font) {
[result addAttribute:NSFontAttributeName value:font range:targetRange];
}
return result;
}
+ (NSAttributedString *)placeholderAttributeSringWithText:(NSString *)text img:(NSString *)imgName {
text = [NSString stringWithFormat:@" %@",text];
NSMutableAttributedString *result = [[NSMutableAttributedString alloc] initWithString:text attributes:@{NSForegroundColorAttributeName:HEXCOLOR(0x999999)}];
NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
attachment.image = [UIImage imageNamed:imgName];
attachment.bounds = CGRectMake(0, -2, 15, 15);
NSAttributedString *imgPart = [NSAttributedString attributedStringWithAttachment:attachment];
[result insertAttributedString:imgPart atIndex:2];
return result;
}
+ (NSString*)overtime:(NSInteger)sec {
NSInteger d = sec/60/60/24;
NSInteger h = sec/60/60%24;
NSInteger m = sec/60%60;
NSInteger s = sec%60;
NSString *_dStr = [NSString stringWithFormat:@"%ld天", (long)d];
NSString *_hStr = [NSString stringWithFormat:@"%02ld时", (long)h];
NSString *_mStr = [NSString stringWithFormat:@"%02ld分", (long)m];
NSString *_sStr = [NSString stringWithFormat:@"%02ld秒", (long)s];
NSString *result = [NSString stringWithFormat:@"%@%@%@", _hStr, _mStr, _sStr];
if (d>0) {
result = [NSString stringWithFormat:@"%@%@%@%@", _dStr, _hStr, _mStr, _sStr];
}
return result;
}
+ (NSArray<NSString *> *)arrOvertime:(NSInteger)sec {
NSInteger d = sec/60/60/24;
NSInteger h = sec/60/60%24;
NSInteger m = sec/60%60;
NSInteger s = sec%60;
NSString *_dStr = [NSString stringWithFormat:@"%ld", (long)d];
NSString *_hStr = [NSString stringWithFormat:@"%02ld", (long)h];
NSString *_mStr = [NSString stringWithFormat:@"%02ld", (long)m];
NSString *_sStr = [NSString stringWithFormat:@"%02ld", (long)s];
return @[_dStr, _hStr, _mStr, _sStr];
}
+ (NSString *)timeDayString {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyyMMdd";
return [formatter stringFromDate:NSDate.date];
}
- (BOOL)isValidPhone {
NSString *phoneRegex = @"^((13|14|15|16|17|18|19)[0-9])\\d{8}$";
NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", phoneRegex];
BOOL isValid = [phoneTest evaluateWithObject:self];
return isValid;
}
+ (NSString *)formatTimer:(NSString *)timer {
return @"";
}
- (NSString *)formatUrlString {
NSString *noSpaceString = [self stringByReplacingOccurrencesOfString:@" " withString:@""];
return [noSpaceString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
}
- (BOOL)versionIsBiggerThan:(NSString *)onlineVersion {
NSString *localVersion = self;
BOOL isBig = NO;
NSArray<NSString *> *localNums = [localVersion componentsSeparatedByString:@"."];
NSArray<NSString *> *onlineNums = [onlineVersion componentsSeparatedByString:@"."];
for (int i=0; i<localNums.count; i++) {
if (onlineNums.count > i) {
NSInteger eachLocalNum = localNums[i].integerValue;
NSInteger eachOnlineNum = onlineNums[i].integerValue;
if (eachLocalNum > eachOnlineNum) {
isBig = YES;
break;
}else if (eachLocalNum < eachOnlineNum) {
isBig = NO;
break;
}else {
//
if (i == localNums.count) {
isBig = NO;
}
continue;
}
}
//线
else {
isBig = YES;
break;
}
}
return isBig;
}
@end