@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;
}
有人可以告诉我我做错了什么吗?我希望我的问题很清楚..
最佳答案
我也遇到这个问题了。事实证明,boundingRectWithSize:
有两种类似的方法。一种用于 NSString
,另一种用于 NSAttributedString
。
以下 NSAttributedString 适用于 iOS 6 及更高版本:
- (CGRect)boundingRectWithSize:(CGSize)大小选项:(NSStringDrawingOptions)选项上下文:(NSStringDrawingContext *)上下文
另一个 NSString 方法(您当前正在使用的)仅在 iOS 7 上可用:
- (CGRect)boundingRectWithSize:(CGSize)尺寸选项:(NSStringDrawingOptions)选项属性:(NSDictionary *)属性上下文:(NSStringDrawingContext *)上下文
因此,只需调用 boundingRectWithSize:
方法,而无需在属性字符串上添加额外的 Attributes:
字段,它就可以在 iOS 6 上正常工作。
关于ios7 - -[__NSCFStringboundingRectWithSize :options:attributes:context:]: unrecognized selector sent to instance,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21155143/