-[__NSCFString boundingRectWithSize:options:attributes:context:]: unrecognized selector sent to instance

半腔热情 提交于 2019-12-23 12:42:21

问题


@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..


回答1:


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

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

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

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

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



来源:https://stackoverflow.com/questions/21155143/nscfstring-boundingrectwithsizeoptionsattributescontext-unrecognized-s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!