iOS版本号,build号获取,根据字符串格式长短动态计算uilabel的size等

本文详细介绍了如何从iOS应用中获取版本号和构建号,并提供了比较两个日期大小的方法以及计算UILabel中字符串尺寸的实现,旨在帮助开发者理解和解决iOS应用开发过程中的常见需求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 获得version版本号
[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString”];
<span style="font-family: HannotateSC-W5;">获得build号</span>
[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];


//比较两个日期的大小,只比较到具体的某一天

-(NSComparisonResult)compareDay:(NSDate *)oneDay anotherDay:(NSDate *)anotherDay{

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

    [dateFormatter setDateFormat:@"dd-MM-yyyy"];

    NSString *oneDayStr = [dateFormatter stringFromDate:oneDay];

    NSString *anotherDayStr = [dateFormatter stringFromDate:anotherDay];

    NSDate *dateA = [dateFormatter dateFromString:oneDayStr];

    NSDate *dateB = [dateFormatter dateFromString:anotherDayStr];

    NSComparisonResult result = [dateA compare:dateB];

    return result;

}

使用时[selfcompareDay:dateanotherDay:currentDay]==NSOrderedAscending 表示curentDay大于date



/*根据字符串的格式计算uilabel的宽高

*str —— 要计算size的字符串

*space —— 行间距

*strFont —— 字体

*maxSize —— 字符串允许的最大的宽高size

*/


-(CGSize)countStrSize:(NSString *)str lineSpace:(float)space font:(UIFont *)strFont maxSize:(CGSize)maxSize{

    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]init];

    if (str) {

        attributedString = [[NSMutableAttributedString allocinitWithString:str];

    }

    //maxSize CGSizeMake(320 - xybImgLeftMargin*3-imgViewWidth, MAXFLOAT)    

    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];

    [paragraphStyle setLineSpacing:space];//调整行间距

    NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:15],NSParagraphStyleAttributeName:paragraphStyle};

    [attributedString addAttributes:attributes range:NSMakeRange(0, [str length])];

    CGRect detailFrame =[attributedString boundingRectWithSize:maxSize 

    options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading context:nil];

    CGSize detailSize = detailFrame.size;

    //textLabel.attributedText = attributedString;


    return detailSize;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值