@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..
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With