Evgeniy Kleban
Evgeniy Kleban

Reputation: 6940

Modifying deprecated sizeWithFont method to boundingRectWithSize:options:attributes:context

I was struggling with the problem of setting UITextView height that depends on its content.

Finally I find a solution that works pretty well but it warns me that method sizeWithFont that is used here is deprecated.

When I tried to modify the old method to new I got a "yellow" warning and I want to modify it to new boundingRectWithSize:options:attributes:context method.

There is my code I want to modify:

-(void)configureTextView {

    CGSize textViewSize = [self.descriptionStringShort sizeWithFont:[UIFont fontWithName:@"Marker Felt" size:20]
                           constrainedToSize:CGSizeMake(self.myTextView.frame.size.width, FLT_MAX)
                               lineBreakMode:UILineBreakModeTailTruncation];

    CGRect frame = self.myTextView.frame;
    frame.size.height = textViewSize.height;
    self.myTextView.frame = frame;
}

Upvotes: 0

Views: 220

Answers (2)

Anindya Sengupta
Anindya Sengupta

Reputation: 2579

How about:

NSDictionary *stringAttributes = [NSDictionary dictionaryWithObject:[UIFont fontWithName:@"Marker Felt"
                                                                                        size:20]
                                                                 forKey: NSFontAttributeName];
    CGSize maximumLabelSize = CGSizeMake(self.myTextView.frame.size.width, FLT_MAX);
    CGSize textViewSize = [self.descriptionStringShort boundingRectWithSize:maximumLabelSize
                                                                    options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin
                                                                 attributes:stringAttributes context:nil].size;

    CGRect frame = self.myTextView.frame;
    frame.size.height = textViewSize.height;
    self.myTextView.frame = frame;

Upvotes: 1

Zubair-Systematix
Zubair-Systematix

Reputation: 343

Try this

CGSize textViewSize = [self.descriptionStringShort sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(210.0f, 2000.0f)];

Upvotes: 1

Related Questions