sizeWithFont method is deprecated. boundingRectWithSize returns an unexpected value

前端 未结 6 1372
傲寒
傲寒 2020-12-02 06:38

In iOS7, sizeWithFont is deprecated, so I am using boundingRectWithSize(which returns a CGRect value). My code:

 UIFont *fontText =         


        
6条回答
  •  臣服心动
    2020-12-02 07:25

    for finding size of label run time sizewithfont is deprecated for iOS 7.0 instead of that you have to use -boundingRectWithSize:options:attributes:context: method

    you can use it like below code

    CGSize constraint = CGSizeMake(MAXIMUM_WIDHT, TEMP_HEIGHT);
    NSRange range = NSMakeRange(0, [[self.message body] length]);
    
    NSDictionary *attributes = [YOUR_LABEL.attributedText attributesAtIndex:0 effectiveRange:&range];
    CGSize boundingBox = [myString boundingRectWithSize:constraint options:NSStringDrawingUsesFontLeading attributes:attributes context:nil].size;
    int numberOfLine = ceil((boundingBox.width) / YOUR_LABEL.frame.size.width);
    CGSize descSize = CGSizeMake(ceil(boundingBox.width), ceil(self.lblMessageDetail.frame.size.height*numberOfLine));
    
    CGRect frame=YOUR_LABEL.frame;
    frame.size.height=descSize.height;
    YOUR_LABEL.frame=frame;
    

    here you have to give width to maximum length for finding height or width.

    try this it is working for me.

提交回复
热议问题