iSr*_*ini 29 xcode nstextview uikit ios
在Xcode 5上将项目从iOS5.0转换为iOS7/iOS6.下面的代码给出了编译时警告:
'sizeWithFont:constrainedToSize:lineBreakMode:'不推荐使用:首先在ios 7.0中弃用 - 使用 - boundingRectWithSize:options:attribiutes:context
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0)
{
self.lblHidden.frame = CGRectMake(58, 228, 945, 9999);
self.lblHidden.text = detailShareObj.pDesc;
CGSize size = [detailShareObj.pDesc sizeWithFont:self.lblHidden.font constrainedToSize:self.lblHidden.frame.size lineBreakMode:NSLineBreakByWordWrapping];
return 228.0+size.height+20;
}
else if (indexPath.section == 1)
{
NSString *tempPointStr = (self.shortDescArray)[indexPath.row];
self.lblHidden.frame = CGRectMake(58, 0, 945, 9999);
self.lblHidden.text = tempPointStr;
CGSize size = [tempPointStr sizeWithFont:self.lblHidden.font
constrainedToSize:self.lblHidden.frame.size
lineBreakMode:NSLineBreakByWordWrapping];
return 50.0f;
}
Run Code Online (Sandbox Code Playgroud)
我尝试了其他地方给出的一些建议,但是如果有人可以通过提供代码中所需的更正来提供帮助,那么没有什么可以解决的.
Mr.*_*. T 64
我不会掩盖已弃用的功能警告.他们因为某种原因弃用了它.我相信该函数已被弃用,因为该系列NSString + UIKit函数基于UIStringDrawing库,该库不是线程安全的.如果您尝试不在主线程上运行它们(就像任何其他UIKit功能一样),您将获得不可预测的行为.特别是,如果您同时在多个线程上运行该函数,它可能会使您的应用程序崩溃.这就是为什么在iOS 6中,他们boundingRectWithSize:...
为NSAttributedStrings 引入了一种方法.它建立在NSStringDrawing库之上,并且是线程安全的.
如果查看新的NSString boundingRectWithSize:...
函数,它会以与NSAttributeString相同的方式请求属性数组.如果我不得不猜测,iOS 7中的这个新的NSString函数只是iOS 6中NSAttributeString函数的包装器.
在这方面,如果您只支持iOS 6和iOS 7,那么我肯定会将所有NSString更改sizeWithFont:...
为NSAttributeString boundingRectWithSize
.如果您碰巧有一个奇怪的多线程角落案例,它会为您省去很多麻烦!以下是我转换NSString的方法sizeWithFont:constrainedToSize:
:
过去是什么:
NSString *text = ...;
CGFloat width = ...;
UIFont *font = ...;
CGSize size = [text sizeWithFont:font
constrainedToSize:(CGSize){width, CGFLOAT_MAX}];
Run Code Online (Sandbox Code Playgroud)
可以替换为:
NSString *text = ...;
CGFloat width = ...;
UIFont *font = ...;
NSAttributedString *attributedText =
[[NSAttributedString alloc]
initWithString:text
attributes:@
{
NSFontAttributeName: font
}];
CGRect rect = [attributedText boundingRectWithSize:(CGSize){width, CGFLOAT_MAX}
options:NSStringDrawingUsesLineFragmentOrigin
context:nil];
CGSize size = rect.size;
Run Code Online (Sandbox Code Playgroud)
请注意文档提到:
在iOS 7及更高版本中,此方法返回小数大小(在返回的CGRect的大小组件中); 要使用返回的大小来调整视图大小,必须使用ceil函数将其值提升到最接近的更高整数.
因此,要拉出用于调整视图大小的计算高度或宽度,我会使用:
CGFloat height = ceilf(size.height);
CGFloat width = ceilf(size.width);
Run Code Online (Sandbox Code Playgroud)
Kju*_*uly 44
如果您希望它兼容iOS7及其下面的版本,请尝试使用此版本(使用ARC):
CGSize size;
if ([tempPointStr respondsToSelector:
@selector(boundingRectWithSize:options:attributes:context:)])
{
NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
paragraphStyle.alignment = NSTextAlignmentLeft;
NSDictionary * attributes = @{NSFontAttributeName : self.lblHidden.font,
NSParagraphStyleAttributeName : paragraphStyle};
size = [tempPointStr boundingRectWithSize:self.lblHidden.frame.size
options:NSStringDrawingUsesFontLeading
|NSStringDrawingUsesLineFragmentOrigin
attributes:attributes
context:nil].size;
} else {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
size = [tempPointStr sizeWithFont:self.lblHidden.font
constrainedToSize:self.lblHidden.frame.size
lineBreakMode:NSLineBreakByWordWrapping];
#pragma clang diagnostic pop
}
Run Code Online (Sandbox Code Playgroud)
注意:这只是您的else-if
案例的一个示例,也许您需要根据您的需要进行一些修改.;)
Dav*_*ers 10
对于iOS7,请替换:
CGSize size = [tempPointStr sizeWithFont:self.lblHidden.font
constrainedToSize:self.lblHidden.frame.size
lineBreakMode:NSLineBreakByWordWrapping];
Run Code Online (Sandbox Code Playgroud)
附:
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping; //set the line break mode
NSDictionary *attrDict = [NSDictionary dictionaryWithObjectsAndKeys:self.lblHidden.font, NSFontAttributeName, paragraphStyle, NSParagraphStyleAttributeName, nil];
CGSize size = [tempPointStr boundingRectWithSize:self.lblHidden.frame.size
options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin
attributes:attrDict context:nil].size;
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
26576 次 |
最近记录: |