您的位置:首页 > 移动开发 > IOS开发

【版本适配】iOS中文本的自适应高度

2014-07-08 21:42 288 查看
//文字高度
+ (float)height:(NSString *)lab
{

#ifdef __IPHONE_7_0

NSDictionary * dic = [NSDictionary
dictionaryWithObjectsAndKeys:[UIFont
systemFontOfSize:
K_FONT_SIZE],NSFontAttributeName,
nil];

if ([[[UIDevice
currentDevice]systemVersion]floatValue]>=7.0) {

CGRect rect = [lab
boundingRectWithSize:CGSizeMake(K_INTRODUCE_LABLE_WIDTH,
0) options:NSStringDrawingUsesLineFragmentOrigin
attributes:dic context:nil];

if ([lab isEqualToString:@""]) {
rect.size.height =
0;
}

return rect.size.height;
}

else
{

CGSize rect = [lab
sizeWithFont:[UIFont
systemFontOfSize:K_FONT_SIZE]
constrainedToSize:CGSizeMake(220,2000)
lineBreakMode:NSLineBreakByTruncatingHead];

if ([lab isEqualToString:@""]) {
rect.height =
0;
}

return rect.height;
}

#endif
}
- (void)setCellVal:(NSString *)cellVal
{

if (_cellVal != cellVal) {
[_cellVal
release];

_cellVal = [cellVal
retain];
}

self.cellLabel.text = cellVal;

#ifdef __IPHONE_7_0

//计算文本在限定范围内的高度

NSDictionary * dic = [NSDictionary
dictionaryWithObjectsAndKeys:[UIFont
systemFontOfSize:K_FONT_SIZE],
NSFontAttributeName, nil];

if ([[[UIDevice
currentDevice]systemVersion]floatValue]>=7.0) {

CGRect rect = [_cellLabel.text
boundingRectWithSize:CGSizeMake(K_INTRODUCE_LABLE_WIDTH,
0) options:NSStringDrawingUsesLineFragmentOrigin
attributes:dic context:nil];

_cellLabel.frame =
CGRectMake(_cellLabel.left,
_cellLabel.top,
_cellLabel.width, rect.size.height);
}

else
if ([[[UIDevice
currentDevice]systemVersion]floatValue] <
7.0){

CGSize rect = [_cellLabel.text
sizeWithFont:[UIFont
systemFontOfSize:K_FONT_SIZE]
constrainedToSize:CGSizeMake(220,2000)
lineBreakMode:NSLineBreakByTruncatingHead];

_cellLabel.frame =
CGRectMake(_cellLabel.left,
_cellLabel.top,
_cellLabel.width, rect.height);
}

#endif
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: