iphone - Finding the correct width of a NSString -


on factors wrapping of text inside textview depends. width of textview 160 px , calculated width of incoming text using below mentioned code , comes out 157 px text wrapped in 3 lines... why so?

cgsize size = [mytext sizewithfont:textviewfont              constrainedtosize:textview.frame.size              linebreakmode:uilinebreakmodewordwrap];  cgfloat textwidth = size.width; 

i thought of dividing width of text width of textview number of lines. calculation returns me 1 whereas can see 3 lines coming in textview on simulator.

- (cgsize)sizewithfont:(uifont *)font      constrainedtosize:(cgsize)size          linebreakmode:(uilinebreakmode)linebreakmode 

calculates size string if drawn constrained size given argument, if string long given size constraint truncated fit.

to width of string if it's drawn on single line no constraints use:

- (cgsize)sizewithfont:(uifont *)font 

see: http://developer.apple.com/library/ios/#documentation/uikit/reference/nsstring_uikit_additions/reference/reference.html

note: of ios7

- (cgsize)sizewithfont:(uifont *)font 

is deprecated, instead use:

- (cgsize)sizewithattributes:(nsdictionary *)attrs 

Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -