Mathemage
Mathemage

Reputation: 315

Getting width of text in UITextView (custom font)

I have UITextView with custom font (ttf file), it works perfect, but when I am trying to get text's width with following code

UITextView*textInput;



CGFloat width1 =  [textInput.text sizeWithFont:textInput.font].width;


frame.size.width=width1;
textInput.frame = frame;

it doesn't work correct: width1 is less then real width of text..

Upvotes: 0

Views: 1607

Answers (1)

Ramy Al Zuhouri
Ramy Al Zuhouri

Reputation: 22006

Now in your code I don't know what's textInput, but probably is a string without the real font that the UITextView uses. UITextView has the property attributedText, through which you can obtain a NSAttributedString.
After this you can obtain the size:

NSMutableAttributedString* attributedString=[myTextView attributedText];
NSSize size=[attributedString size];

Then size.width will be the width.

Upvotes: 4

Related Questions