本文介绍了 - [__ NSCFString boundingRectWithSize:options:attributes:context:]:发送到实例的无法识别的选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@interface PromotionsListViewController : UITableViewController 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"PromotionCell";
PromotionCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[PromotionCell alloc] init];
}

// Configure the cell...
Promotion *promotion = [self.promotionList objectAtIndex:indexPath.row];

[cell.imgView setImageWithURL:[NSURL URLWithString:promotion.imageURL] placeholderImage:[UIImage imageNamed:@""]];
cell.lblTitle.text = promotion.promotionTitle;

return cell;
}
@interface PromotionCell : UITableViewCell

@property(nonatomic, strong) UIImageView *imgView;
@property(nonatomic, strong) UILabel *lblTitle;

@end
- (void)layoutSubviews {

if (self.lblTitle.text) {

    CGSize maxsize = CGSizeMake(300, CGFLOAT_MAX);
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;

    CGRect calculateRect = [_lblTitle.text boundingRectWithSize:maxsize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:_lblTitle.font, NSParagraphStyleAttributeName:paragraphStyle.copy} context:nil];

    _lblTitle.frame = CGRectMake(_lblTitle.frame.origin.x, 300 - calculateRect.size.height - 5, _lblTitle.frame.size.width, calculateRect.size.height);

} else {
    _lblTitle.frame = CGRectZero;
}
}
- (UILabel *)lblTitle {

if (!_lblTitle) {

    _lblTitle = [[UILabel alloc] initWithFrame:CGRectMake(10, 250, 300, 50)];
    _lblTitle.font = [UIFont boldSystemFontOfSize:22.0f];
    _lblTitle.numberOfLines = 0;
    _lblTitle.lineBreakMode = NSLineBreakByWordWrapping;
    [self.contentView addSubview:_lblTitle];
}
return _lblTitle;
}

任何人都可以告诉我,我做错了什么?我希望我的问题很清楚..

Can anybody plz tell me what am I doing wrong? I hope my Question is clear..

推荐答案

我也有这个问题。事实证明, boundingRectWithSize有两种类似的方法:。一个用于 NSString ,一个用于 NSAttributedString

I had this problem too. Turns out there are two similar methods for boundingRectWithSize:. One for NSString and one for NSAttributedString.

以下针对NSAttributedString适用于iOS 6及更高版本:

The following for NSAttributedString is available for iOS 6 and above:

- (CGRect)boundingRectWithSize:(CGSize)大小选项:(NSStringDrawingOptions)选项上下文: (NSStringDrawingContext *)context

另一个NSString方法(您当前使用的)仅适用于iOS 7:

The other NSString method (which you're currently using) is only available on iOS 7:

- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options attributes:(NSDictionary *)attributes context:(NSStringDrawingContext *)context

所以只需调用 boundingRectWithSize:方法,无需额外的属性:你的Attributed String字段,它在iOS 6上运行正常。

So just call the boundingRectWithSize: method without an extra Attributes: field on your Attributed String and it'll work fine on iOS 6.

这篇关于 - [__ NSCFString boundingRectWithSize:options:attributes:context:]:发送到实例的无法识别的选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 18:57