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

1

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

Guiscard answered 16/1, 2014 at 6:53 Comment(3)
I think the error message is quite clear... You are calling boundingRectWithSize on an object that doesn't recognise the method. Where is the error located on? Can you just remove your code that doesn't have anything to do with the error message?Longwinded
Are you running this code on iOS 7 simulator/device or previous? Running on previous iOS version will throw this error as boundingRectWithSize: method is introduced in iOS 7.Katzen
@Katzen This is not accurate. boundingRectWithSize: was introduced in iOS 6. Source: Apple Developer LibraryHabitforming
H
8

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.

Habitforming answered 12/2, 2014 at 11:42 Comment(2)
@Jack Dewhurst , I removed Attributes:, but error: No visible @interface for NSString declares the selector boundingRectWithSize:options:context:Hundley
Solved by convert NSString to NSAttributedStringHundley

© 2022 - 2024 — McMap. All rights reserved.