开发者

Difference in label height displayed in iPhone 4.0 vs Simulator

开发者 https://www.devze.com 2023-04-05 01:38 出处:网络
I am using the following method to obtain the label height required for a specified text. //Obtain the label height required for the text

I am using the following method to obtain the label height required for a specified text.

//Obtain the label height required for the text
CGFloat answerLabelHeight = [CustomCell getHeightOfLabel:answer_text ofFontSize:ANSWER_FONT_SIZE withConstraint: ANSWER_CONSTRAINT];

//Retrieve the label from cell contentview
UILabel *thisAnswerLabel = (UILabel*开发者_开发问答)[self.contentView viewWithTag:ANSWERLABEL_TAG];

//Set the frame of the label depending on the label height obtained above
[thisAnswerLabel setFrame:CGRectMake(CELL_TEXT_LEFT_MARGIN + CELL_AVATAR_WIDTH + CELL_SPACING, currentYAxisValue, CELL_ANSWER_WIDTH, answerLabelHeight)];


//Method to obtain label height
+ (CGFloat) getHeightOfLabel:(NSString *)text ofFontSize:(CGFloat)fontSize withConstraint:(CGSize)constraint
{
    CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:fontSize] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];

    return size.height;
}

Strangely, I observe that there are times when the label height returned is not accurate i.e. there are still spaces above and below the text (see below). Also, the results returned for the iPhone 4 device and the simulator is different

Result on Simulator: (Result is accurate i.e. no extra paddings above and below text

Difference in label height displayed in iPhone 4.0 vs Simulator

Result on iPhone 4: (Result is different and slightly off i.e. extra paddings are seen above and below text

Difference in label height displayed in iPhone 4.0 vs Simulator

Can anyone advise me on 1) Why is there a difference in results between the iphone device and simulator 2) How can I remove the extra paddings for the iphone 4 scenario?

EDIT: Note that the extra padding only occurs for certain scenarios, seems like when the last word of the paragraph is close to the right side of the border. Else the height returned is ok. if this is due to the retina display of the iphone, how can I overcome this inconsistency?


@Zhen i guess you are not using simulator for ratina disply while iPhone 4 have ratina display.

You can remove extra padding by decreasing its height and set alignment as center.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号