On what factors does the wrapping of the text inside a textview depends. Width of my textview is 160 px and I calculated the width of the incoming text using below mentioned code and it comes out to be 157 px but this text is 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 with width of the textview to get the number of lines. But calculation returns me 1 whereas I can see 3 lines coming in textview on simulator.
- (CGSize)sizeWithFont:(UIFont *)font
constrainedToSize:(CGSize)size
lineBreakMode:(UILineBreakMode)lineBreakMode
Calculates the size for a string if it would be drawn constrained by the size given as an argument, if the string is too long for the given size constraint it will get truncated to fit.
To get the width of the string if it's drawn on a single line with no constraints use:
- (CGSize)sizeWithFont:(UIFont *)font
see: http://developer.apple.com/library/ios/#documentation/uikit/reference/NSString_UIKit_Additions/Reference/Reference.html
NOTE: As of iOS7
- (CGSize)sizeWithFont:(UIFont *)font
is deprecated, instead use:
- (CGSize)sizeWithAttributes:(NSDictionary *)attrs
Try setting the height of the size you pass into that method to be something huge. Otherwise the returned size will always show the actual height of the text view (assuming the full string does not fit in the given height).
Related
I have text "Testing of application". it looks line in cell is
"Tesing of
Application"
I want to add a Asteric right a text. for e.g
"Tesing of *
Application"
but it show after the width of Label.. I have written the following code.
CGSize maximumLabelSize = CGSizeMake(99999,9999);
CGSize textSize = [[_label text] sizeWithFont:_label.font
constrainedToSize:maximumLabelSize lineBreakMode:_label.lineBreakMode];
float startX = 2 + textSize.width;
label2.frame = CGRectMake(startX, label2.frame.origin.y, label2.frame.size.width, label2.frame.size.height);
When you use constrainedToSize you have to pass the size with real height or real width.
Let say you have a label on 300px width and you want to make the height according to text length you have to use CGSizeMake(300,9999). In this case the constrainedToSizemethod will return you the height need to draw that text.
For getting the width you can set a small height like 20px to get the width needed to draw the text.
I am currently drawing text on string, using drawInRect, restricted to a size using CGRectSize. It is also centred.
[text drawInRect:CGRectMake(0,0,nWidth,nHeight) withFont:font lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentCenter];
What I need to find out is the size and offset of the actual drawn text.
Because it is centred, there is a very good chance that there is an X offset before text is drawn.
I've seen the command boundingRectWithSize but I think it may not be supported on IOS?
Get the size of the text,
CGSize size = [text sizeWithFont:font constrainedToSize:CGRectMake(0,0,nWidth,nHeight) lineBreakMode: UILineBreakModeWordWrap];
(See documentation)
Then, since it is centered, the bounds will be
CGRect bounds = CGRectMake((nWidth - size.width)/2, ..., size.width, size.height);
You want the method from the UIKit category on NSString...
- (CGSize)sizeWithFont:(UIFont *)font
which gives you the size needed, and then you can compute your centering (or other) positions.
Hi I am new as a Iphone developer.
I am wondering how to get the length of a given string under one specific UIFont, WITHOUT line word wrap.
say I have a NSString* lpText = #"a unknon lenght string ..... string" //could be very long
and I am using the "Times New Rome" font for rendering...
Any idea?
Thanks in advance.
This should work (untested):
CGSize size = [string sizeWithFont:[UIFont systemFontOfSize:14.0] constrainedToSize:CGSizeMake(MAXFLOAT, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];
Set the font you need. The line break mode shouldn't matter here.
Look up the UIKit additions to NSString:
(CGSize)sizeWithFont:(UIFont *)font
Returns the size of the string if it were to be rendered with the specified font on a single line.
(CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size
Returns the size of the string if it were rendered and constrained to the specified size.
(CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size lineBreakMode:(UILineBreakMode)lineBreakMode
Returns the size of the string if it were rendered with the specified constraints.
A UILabel's text is drawn in a CGRect(x,y,size,width). The label can have multiple lines of text.
Is it possible to write a function to return a CGPoint within this rectangle that designates the position of the last character?
I ask because I am trying to wrap the text of a UILabel in an arbitrary prefix and suffix. The prefix/suffix need to be separate labels so I can set their font, color, etc. It's easy to find the start of the text, but finding the end of the text is tricky, to me at least.
Perhaps a UIWebView will do the trick. Populate it with [webview LoadHTMLString:#"<b>prefix/b>text<b>suffix</b>" baseURL:nil]. This is a bit of a sledgehammer solution, but you obviously get lots of other goodies thrown into the bargain.
The main way to measure text on the iPhone is using NSString's sizeWithFont:forWidth:lineBreakMode. It is possible to build on this to do what you want, but it would be a bit of work. First, measure the prefix using your specified text traits and remember the dimensions. Then, calculate word breaks and incrementally add the measured widths for each word, line wrapping as necessary. Finally, measure your suffix and draw everything in the rectangles you've calculated.
As sbooth mentioned, you can use the following to get a text rect:
UILabel *label = [UILabel new]; // Or whatever the label happens to be...
CGSize constraintSize = CGSizeMake(label.frame.size.with-10, MAXFLOAT); // 10 is the total size of the margins.
// Replace the 10 with 40 for UITableView usage because of the other subviews
CGSize labelSize = [label.text sizeWithFont:label.font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
To get a point for the last character, just isolate the last line and get its rect.
X: Get rect of last line and subtract 0.5*(width of last character)
Y: 0.5*labelSize.height
I have two possible widths for a string i want to display in a label in a table cell, and i need to compute the height so that the table cell's height is recorded correctly. however, no matter what I do for the constraining size i get the same height, which is incorrect in the case i want. The code i'm using:
CGFloat width = 300.0f;
NSString * value = #"LongText LongText LongText LongText LongText LongText";
CGSize contentSize = [value sizeWithFont: [UIFont systemFontOfSize: 14.0f]
constrainedToSize: CGSizeMake(width, CGFLOAT_MAX)
lineBreakMode: UILineBreakModeWordWrap];
When i inspect the contentSize variable, the width is 252 and the height is 36 which is expected. But if instead of 300.0f i plug in 222.0f into the width variable, the width is 189 but the height is still 36, and only the first 4 LongText words are displayed on 2 lines (the third line seems to be cut off somehow in the calculation). Does anyone know why this is happening?
Your code looks right. Look here and here to see if there is anything amiss in your code.
It turns out it is returning the correct size; the code i was working on was using incorrect widths instead of the widths from this method which is why the text was cut off and i was confused.