Multiline text (wordwrap) with drawAtPoint? - iphone

is there a way to draw multiline text with drawAtPoint?
I have tried UILineBreakModeWordWrap but doesnt seem to be working?
How would you convert this code to a working multiline text??
point = CGPointMake(boundsX, 50);
[self.heading drawAtPoint:point forWidth:labelWidth withFont:mainFont minFontSize:12.0 actualFontSize:NULL lineBreakMode:UILineBreakModeWordWrap baselineAdjustment:UIBaselineAdjustmentAlignBaselines];
Thank you!

drawAtPoint: doesn't support multiline text. You can use drawInRect: method instead.
Edit: (Copying #George Asda's comment below to here)
[self.heading drawInRect:(contentRect) withFont:mainFont
lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentCenter];

[_text drawWithRect:_textRect options:**NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingTruncatesLastVisibleLine** attributes:attributes context:nil];

This cannot be done with the NSString drawAtPoint method. From the documentation:
Draws the string in a single line at the specified point in the
current graphics context using the specified font and attributes.
Could you perhaps use a simple UILabel?
EDIT
You can calculate the height of a UILabel like this:
//Calculate the expected size based on the font and linebreak mode of your label
CGSize maximumLabelSize = CGSizeMake(296,9999);
CGSize expectedLabelSize = [yourString sizeWithFont:yourLabel.font
constrainedToSize:maximumLabelSize
lineBreakMode:yourLabel.lineBreakMode];
//adjust the label the the new height.
CGRect newFrame = yourLabel.frame;
newFrame.size.height = expectedLabelSize.height;
yourLabel.frame = newFrame;

Related

How to set the number of lines in uitextfield dynamically?

Actually i am trying to set the text in the textfield in next line when it becomes larger in the first line ad default. Can you please help me. my uitextfield is taken through the IB. and i am trying to write it and wants to post something like comment.
[viewFeedback setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"background_Feedback.png"]]];
CGSize size = [txtFeedback.text sizeWithFont:txtFeedback.font];
CGRect f = txtFeedback.frame;
f.size.height = ceil(size.width/f.size.width)*size.height;
txtFeedback.frame = f;
txtFeedback is the textfield name here..
Well this would be a Increasing the height of the UITextfield dynamically. Please follow this or add your custom frame
In your UITextViewDelegate implementation of textViewDidChange: you need to calculate the size of the text for the specific width and adjust accordingly.
-(void)textViewDidChange:(UITextView *)textView
{
NSString *text = [textView text];
NSFont *font = [textView font];
CGFloat width = [textView frame].size.width;
CGSize size = [text sizeWithFont:font constrainedToSize:CGSizeMake(width, 9999) lineBreakMode:UILineBreakModeWordWrap];
/* Adjust text view width with "size.height" */
}

Drawing text in UITableViewCell

I used the following method to draw text in my custome cell, it's working fine but I just found that part of the text missing(Not showing all text in the cell):
- (void)drawContentView:(CGRect)rect {
UIColor * textColor = [UIColor blackColor];
if (self.selected || self.highlighted){
textColor = [UIColor whiteColor];
}
else
{
[[UIColor whiteColor] set];
UIRectFill(self.bounds);
}
[textColor set];
UIFont * textFont = [UIFont systemFontOfSize:16];
CGSize textSize = [text sizeWithFont:textFont constrainedToSize:rect.size];
[text drawInRect:CGRectMake(self.frame.size.width-(textSize.width+2 ) ,
(rect.size.height / 2) - (textSize.height / 2),
textSize.width, textSize.height) withFont:textFont lineBreakMode:NSLineBreakByWordWrapping alignment:NSTextAlignmentRight];
}
Thanks, please advice.
Try to make the space for drawing bigger than the textSize.width, textSize.heigh
UIFont * textFont = [UIFont systemFontOfSize:16];
CGSize sizeMe = CGSizeMake(300, rect.size.height*1.5);
CGSize textSize = [text sizeWithFont:textFont constrainedToSize:sizeMe];
[text drawInRect:CGRectMake(self.frame.size.width-(textSize.width+10 ) ,
(rect.size.height / 2) - (textSize.height / 2),
textSize.width, textSize.height+(textSize.height*1.5)) withFont:textFont lineBreakMode:NSLineBreakByWordWrapping alignment:NSTextAlignmentRight];
Perhaps you should change the line that determines text size to:
CGSize textSize = [text sizeWithFont:textFont constrainedToSize:rect.size lineBreakMode: NSLineBreakByWordWrapping];
You did not consider the lineBreakMode in sizeWithFont. You may want to use – sizeWithFont:constrainedToSize:lineBreakMode: instead for determine the actual size.
BTW, when your constraint is exactly the rect.size then the return value will never be larger than the actual rect.size. That may be what you want. Whenever I deal with strings of variable length in custom cells, I like to know whether the string fits in or not and may enlarge the individual cell (which requieres some more work of course).
If you do that then set the size.height of the constraints size to something very high. 10000.0f or so.
You are creating a custom cell .What is the need for drawing the text into it?That is the purpose of Label just add as the subview and use it.
drawInRect is a coregraphics drawing method
as per docs
Draws the string in the current graphics context using the specified
bounding rectangle and font. This method draws as much of the string
as possible using the given font and constraints. This method uses the
UILineBreakModeWordWrap line break mode and the UITextAlignmentLeft
alignment.
I dont see you get the context before drawing .Hence it doesnot have a valid context.and hence not drawing

UILabel size to fit

I have a problem involving UILabel's sizeToFit method:
UILabel *questionLabel = [[UILabel alloc]initWithFrame:CGRectMake(0,0,320,320)];
questionLabel.lineBreakMode = UILineBreakModeWordWrap;
questionLabel.backgroundColor=[UIColor clearColor];
questionLabel.textAlignment=UITextAlignmentLeft;
questionLabel.textColor=[UIColor blackColor];
questionLabel.tag=1;
questionLabel.font=[UIFont systemFontOfSize:13];
questionLabel.numberOfLines = 0;
[questionLabel sizeToFit];
[myView addSubview:questionLabel];
I had written this code for displaying my data. But if I write: [questionLabel sizeToFit] my data does not display properly. If I remove [questionLabel sizeToFit] then it is displaying but it only shows half the data.
Thanks and Regards.
NSString *yourString = #"write your label text here";
CGSize s = [yourString sizeWithFont:[UIFont systemFontOfSize:12] constrainedToSize:CGSizeMake(width, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];
questionLabel.frame = CGRectMake(0, 0, s.width, s.height);
Check if it helps.
I think it's best to use taus-iDeveloper answer to compute the size of a label.
I just want to say that the reason your code is not working is because you didn't set text to your UILabel so sizeToFit returns CGSizeZero (so it doesn't appear on screen). You have to set text before using sizeToFit.
I found that if AutoLayout is on then size to fit not work
i googled d above problem and came across some info that sizeToFit seems to be a bug and it has been reported to apple already.
So as a workaround u can use this code:
NSString * myText = [NSString stringWithString:#"some text"];
CGFloat constrainedSize = 265.0f;
UIFont * myFont = [UIFont fontWithName:#"Arial" size:19];
CGSize textSize = [myText sizeWithFont: myFont
constrainedToSize:CGSizeMake(constrainedSize, CGFLOAT_MAX)
lineBreakMode:UILineBreakModeWordWrap];
CGRect labelFrame = CGRectMake (0, 0, textSize.width, textSize.height);
UILabel *label = [[UILabel alloc] initWithFrame:labelFrame];
[label setFont:myFont];
[label setText:myText];
I've found that when using sizeToFit with a UILabel, in InterfaceBuilder you need to change the Autoshrink property from 'Fixed Font Size' to 'Minimum Font Size'. I usually then set its value to 0.5 to be sure its working properly.
just make sure you increase the number of lines of the label. Instead of
questionLable.numberOfLines = 0; make use of
questionLable.numberOfLines = 4;
As it will force the system to compute the smallest width possible for 4 lines.
You can achieve this via the Xib file too..

EDIT: UITextView Label is Cut in Half (Horizontally)

I need help with this peculiar problem. I have a multiple choice question app and I have the choices as UITextview. Sometimes, choice D gets cut in half for whatever reason.
Screenshot:
Not sure what's going on here. I basically have the UITextView frame adjust to its contentSize.
CGRect dFrame = choiceD.frame;
dFrame.size.height = choiceD.contentSize.height;
choiceD.frame = dFrame;
Any ideas? Thanks in advance.
Caculate the size of string:
NSString *choiceDString = #"Equal the present value....";
CGSize size = [choiceDString sizeWithFont:[UIFont systemFontOfSize:CHOICE_FONT_SIZE] constrainedToSize:CGSizeMake(CHOICE_WIDTH, 100000)];
Init a label to content the choice string:
UILabel *choiceDLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0,size.width,size.height)];
choiceDLabel.text= choiceDString;
Add the subview label for button:
[button addSubview:choiceLabel];
Use this code..Yo have define height of label according to your text length...
NSString *summary;
summary = #" your text";
CGSize sizeofbuttonorlable = [summary sizeWithFont:[UIFont systemFontOfSize:30]
constrainedToSize:CGSizeMake(self.view.bounds.size.width - 40, MAXFLOAT)
lineBreakMode:UILineBreakModeWordWrap];
CGRect frame = CGRectMake(0.0f, 0.0f, 320.0f, sizeofbuttonorlable.height);
UILabel *choiceDLabel = [[UILabel alloc] initWithFrame:frame];
choiceDLabel.text= summary;
[button addSubview:choiceLabel];
Hope, this will help you...chill
My suggestion is to first Calculate the size of the text entered by you in the textView like:-
//Give the maximum size of label which that label can have.
CGSize maximumLabelSize = CGSizeMake(300,500);
CGSize expectedLabelSize = [Label.text sizeWithFont:Label.font constrainedToSize:maximumLabelSize lineBreakMode:UILineBreakModeWordWrap];
//adjust the label the new height.
CGRect newDescFrame = Label.frame;
newLabelFrame.size.height = expectedLabelSize.height;
NSLog(#"%f",newLabelFrame.size.height);
//adjust the label the new width.
newLabelFrame.size.width = expectedLabelSize.width;
NSLog(#"%f",newLabelFrame.size.width);
//Set the label size according to the new height and width.
label.frame = newLabelFrame;
Write above mention code after entering the text in the textView.
Hope it helps.Thanks :)

UIlabel linebreak with scrollview not correctly display if too long (iphone retina simulator)

i have UIlabel using below code, i don't know maybe its too long or not. but when it is scrolled, the uilabel get this result. How can i solve the problem? are there any limitation using uilabel?
CGSize maximumLabelSize = CGSizeMake(100,9999);
CGSize expectedLabelSize = [summaryBlogParsed sizeWithFont: contentSummaryLabel.font
constrainedToSize:maximumLabelSize
lineBreakMode: contentSummaryLabel.lineBreakMode];
//adjust the label the the new height.
CGRect newFrame = contentSummaryLabel.frame;
newFrame.size.height = expectedLabelSize.height;
contentSummaryLabel.frame = newFrame;
contentSummaryLabel.numberOfLines = 0;
contentSummaryLabel.text = summaryBlogParsed;
[contentSummaryLabel sizeToFit];
contentScrollView.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.width*2 + CGRectGetMaxY(contentSummaryLabel.frame));
Edit:
This problem occur when i use Iphone Retina simulator.
It looks like labels overlaying on top of each other. You can try setting their backgroundColour to [UIColor clearColor] and their opaque property to NO to check that it is indeed the case. If it is then you will have to check your frame setting code.
If the whole thing is one label then you probably don't want a single label with so many lines, but you can try calling setNeedsDisplay to force redrawing.