How to adjust and make the width of a UILabel to fit the text size?

后端 未结 8 2240
被撕碎了的回忆
被撕碎了的回忆 2020-12-13 06:01

In my project, there is a UILabel with text. The font size is 16pt. The text contents are changed depending on different cases. I hope it can automatically adju

8条回答
  •  爱一瞬间的悲伤
    2020-12-13 06:21

    As sizeWithFont is depreciated in IOS 7.0 then you below code

    #define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
    
    
    
     if (SYSTEM_VERSION_LESS_THAN(@"7.0")) {
            // code here for iOS 5.0,6.0 and so on
            CGSize fontSize = [itemCat_text sizeWithFont:[UIFont fontWithName:@"Helvetica" size:12]];
        } else {
            // code here for iOS 7.0
             fontSize = [itemCat_text sizeWithAttributes:
                               @{NSFontAttributeName:
                                     [UIFont fontWithName:@"Helvetica" size:12]}];
        }
    

提交回复
热议问题