在iPhone SDK中获取iOS 7的正确文本高度 [英] Get correct text height for iOS 7 in iPhone SDK
问题描述
我需要获得文本高度并根据文本高度增加单元格的高度,文本高度会动态增加或减少。我现在成功地为iOS 6和之前的版本获得正确的文本高度,但我无法获得正确的iOS 7及更高版本。我用Google搜索并找到了许多解决方案,但这不起作用。你可以看到相同单元格的附加图像,看到iOS 7文本中的差异是截断的,在iOS 6中它显示正确的大小。我用 UILabel
。按照我的代码,我正在做什么。请仔细研究并指导我更好的解决方案,如果有人事先经历过这类事情。
I need to get text height and grow cell's height depending upon text height which increase or decrease dynamically. I now successfully getting correct height of text for iOS 6 and prior but I am not able to get correct height iOS 7 and later. I googled and found many solution but this doesn't worked. You can see attached Image of same cell and see difference in iOS 7 text is truncating and in iOS 6 its showing correct size. I used UILabel
. Follow my code of what exactly I am doing. Kindly look into it and guide me better solution if any one experienced this sort of thing earlier.
- (CGSize)getSizeOfText:(NSString *)text withFont:(UIFont *)font widthOftext:(int )txtWidth
{
CGSize boundingSize = CGSizeMake(txtWidth, 9999);
CGSize size;
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7)
{
CGRect textRect = [text boundingRectWithSize:boundingSize
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{NSFontAttributeName:font}
context:nil];
size=textRect.size;
}
else
{
CGSize requiredSize = [text sizeWithFont:font constrainedToSize:boundingSize lineBreakMode:UILineBreakModeWordWrap];
size=requiredSize;
}
return size;
}
在iOS 6设备上。
在iOS 7设备上。
推荐答案
请尝试此代码
在标题中声明#define kMaxHeight 1000.0f。
Declare #define kMaxHeight 1000.0f in header.
CGSize sizeNew = [YourText sizeWithFont:[UIFont fontWithName:Fontname size:intFontSize]
constrainedToSize:CGSizeMake(320, kMaxHeight)
lineBreakMode:NSLineBreakByWordWrapping];
txtText.numberOfLines = 0;
如果您有任何疑问,请告知我们。
If you have any question let m e know.
这篇关于在iPhone SDK中获取iOS 7的正确文本高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!