In my page based application i want to use line-break in uilabel,I try with the following coding,
#"The farmer and his wife were entranced with joy and danced cheerfully because of their goose lays \n a golden egg",
self.titlestring.frame = CGRectMake(53, 620, 0, 37);
self.titlestringcop.frame = CGRectMake(53, 620, 918, 37);
[self fadein];
[self.titlestring setText:self.dataobject1];
[self.titlestringcop setText:self.dataobject1];
textstring.numberOfLines = 0;
titlestringcop.numberOfLines = 0;
CGSize labelSize = [textstring.text sizeWithFont:textstring.font
constrainedToSize:textstring.frame.size
lineBreakMode:textstring.lineBreakMode];
CGSize labelSize1 = [titlestringcop.text sizeWithFont:titlestringcop.font
constrainedToSize:titlestringcop.frame.size
lineBreakMode:titlestringcop.lineBreakMode];
textstring.frame = CGRectMake(
textstring.frame.origin.x, textstring.frame.origin.y,
textstring.frame.size.width, labelSize.height);
titlestringcop.frame = CGRectMake(
titlestringcop.frame.origin.x, titlestringcop.frame.origin.y,
titlestringcop.frame.size.width, labelSize1.height);
But in doesn't showing any difference?Whats wrong in my code?
Try this line add these lines also in code & check:
textstring.lineBreakMode = UILineBreakModeWordWrap;
titlestringcop.lineBreakMode = UILineBreakModeWordWrap;
Related
I have been trying to get this working for much longer than I care to admin, but I have searched just about every s.o article there is and none of the solutions seem to be doing anything...
My problem is that I am trying to resize a title (UILabel) based on the amount of text, after resizing the title I want to move the image 100 pixels below the title.
The image frame shows that the value has changed to what it has been calculated to be but there is never a change in the application, it only shows the design that was made in the storyboard.
Storyboard layout:
View
Scroll View
Image View
Label - TItle
The code below shows some printouts of values, without running it an example of those are:
Before: [0.000000]
After: [24.000000]
Label Expected Size: 280.000000, 96.000000
After Title Assign: [96.000000]
Image Y Before: [0.000000]
Image Y After: [196.000000]
Code:
self.Title.text = NewsCell.Title;
self.Image.image = NewsCell.Image;
// fit title
printf("Before: [%f]\n", self.Title.frame.size.height);
[self.Title sizeToFit];
printf("After: [%f]\n", self.Title.frame.size.height);
//self.Title.frame = CGRectMake(20, 5, 280, self.Title.frame.size.height);
self.Title.numberOfLines = 0;
CGSize maximumLabelSize = CGSizeMake(280, 100);
CGSize expectedLabelSize = [self.Title.text sizeWithFont:self.Title.font constrainedToSize:maximumLabelSize lineBreakMode:self.Title.lineBreakMode];
self.Title.frame = CGRectMake(20,0,expectedLabelSize.width, expectedLabelSize.height);
printf("Label Expected Size: %f, %f\n", expectedLabelSize.width, expectedLabelSize.height);
printf("After Title Assign: [%f]\n", self.Title.frame.size.height);
// set image location to be bototm of title + 100
printf("Image Y Before: [%f]\n", self.Image.frame.origin.y);
double titleBottom = self.Title.frame.origin.y + self.Title.frame.size.height + 100;
self.Image.frame = CGRectMake(0, titleBottom, 320, 200);
printf("Image Y After: [%f]\n", self.Image.frame.origin.y);
// set description location
double imageBottom = self.Image.frame.size.height + self.Image.frame.origin.y + 5;
// description expand
[self.Description.text stringByAppendingString: #"\n\n © 2013 Geekosystem, LLC"];
[self.Description sizeToFit];
self.Description.frame = CGRectMake(20, imageBottom, 280, self.Description.frame.size.height);
self.Description.numberOfLines = 0;
I am trying to adjust the size of the text in my UITextView so it fits. I have the following code:
NSString *storyTitle = [newsfeedToSubject.properties valueForKey:#"title"];
int currentFontSize = 18;
UIFont *currentFont = [UIFont fontWithName:kProximaNovaBold size:currentFontSize];
CGSize storyTitleSize = [storyTitle sizeWithFont:currentFont constrainedToSize:self.newsfeedStoryTitle_.frameSize lineBreakMode:UILineBreakModeWordWrap];
while (storyTitleSize.height >= self.newsfeedStoryTitle_.frameHeight){
currentFontSize--;
currentFont = [UIFont fontWithName:kProximaNovaBold size:currentFontSize];
storyTitleSize = [storyTitle sizeWithFont:currentFont constrainedToSize:self.newsfeedStoryTitle_.frameSize lineBreakMode:UILineBreakModeWordWrap];
}
[self.newsfeedStoryTitle_ setFont:currentFont];
[self.newsfeedStoryTitle_ setText:storyTitle];
[self.newsfeedStoryTitle_ setBackgroundColor:[UIColor redColor]];
However, here's what I am getting:
I tried with different line break mode and also set the autoresize to none, but it didn't help. Any idea?
Your while loop will not do what you want because you are using
storyTitleSize.height
to determine the size of the frame, which will not take into account the wrap around effect of the text. One suggestion would be to truncate the string and only display a certain amount of characters of the title. Otherwise, you will need to calculate how many line breaks you will have, based on the width of the frame and the length of the string, and use
while(storyTitleSize.height * numLineBreaks >= self.newsFeedStoryTitle_.frameHeight)
{
...
}
Hope that helps.
You can also dynamically resize your frame (UITextView) as in this thread
try these code
while (self.textView.contentSize.height >= frameHeight){
currentFontSize--;
currentFont = [UIFont fontWithName:kProximaNovaBold size:currentFontSize];
[self.textView setFont:currentFont];
self.textView.text = #"your string";
}
My UILabel can do a line break according to the current \n . But if the line itself is too long, it won't be able to automatically do a line break. Can I do more configurations to my UILabel to achieve that?
And I've already used:
aLabel.lineBreakMode = UILineBreakModeWordWrap;
aLabel.numberOfLines = 0;
[aLabel setFont:[UIFont fontWithName:#"MarkerFelt-Wide" size:24]];
aLabel.textAlignment = UITextAlignmentCenter;
CGRect labelFrame = aLabel.bounds;
labelFrame.size = [words sizeWithFont:aLabel.font constrainedToSize:CGSizeMake(LABEL_WIDTH, 100000) lineBreakMode:aLabel.lineBreakMode];
aLabel.frame = CGRectMake(0, 0, aLabel.frame.size.width-10, labelFrame.size.height);
words is a NSString
Set Number of line as you want to set.
aLabel.numberOfLines = 2;
if you are having text in a paragraph and want to place it in a lable with differ lines no need to use UILineBreakModeWordWrap.. simply write one line code as in how many lines you want to place the text as
labobj.numberOfLines = 90;
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
How can i fix my scrollView dynamically when i have more than one textViews in it?
I made a scrollView in interface builder and put 3 textViews and 2 button in that. Sometimes textViews have more text and sometimes less. i managed to give them flexible frame size. but if top textView has more text then some of its text comes over 2nd textView. i tried this code. i think it should work but its not working properly.
storyTitle.text = sTitle;
storyTitle.font = [UIFont boldSystemFontOfSize:18];
date.text = sDate;
date.font = [UIFont systemFontOfSize:16];
summary.text = sSummary;
summary.font = [UIFont systemFontOfSize:16];
CGRect titleFrame = storyTitle.frame;
titleFrame.size.height = storyTitle.contentSize.height;
storyTitle.frame = titleFrame;
CGRect dateFrame = date.frame;
dateFrame.size.height = label.contentSize.height;
date.frame = dateFrame;
CGRect summaryFrame = summary.frame;
summaryFrame.size.height = summary.contentSize.height;
summary.frame = summaryFrame;
CGRect topTextViewFrame = storyTitle.frame;
CGRect middleTextViewFrame = date.frame;
NSLog(#"size: %d",topTextViewFrame.origin.y);
middleTextViewFrame.origin.y = topTextViewFrame.origin.y + topTextViewFrame.size.height;
CGRect bottomTextViewFrame = summary.frame;
bottomTextViewFrame.origin.y = middleTextViewFrame.origin.y + middleTextViewFrame.size.height;
// then adjust other controls based on these frames, for example:
CGRect myButtonFrame = detailButton.frame;
myButtonFrame.origin.y = bottomTextViewFrame.origin.y + bottomTextViewFrame.size.height;
CGRect myButtonFrame2 = fbButton.frame;
myButtonFrame2.origin.y = myButtonFrame.origin.y + myButtonFrame.size.height;
// finally adjust the contentSize of the scrollview assuming the button is the bottom element
CGSize csize = scrollView.contentSize;
csize.height = myButtonFrame2.origin.y + myButtonFrame2.size.height;
scrollView.contentSize = csize;
can anyone tell me whats wrong with this? and how should i right it so it ll work fine. thanx in advance
hi if your text is not editable by user the try using UILabel instead of UITextView.If you set numberOfLines property of UILabel = 0 then it can go to more that one line and then u can calculate the required height for the label by the method
CGSize size = [label.text sizeWithFont:label.font constrainedToSize:CGSizeMake(label.frame.size.width, FLT_MAX) lineBreakMode:UILineBreakModeWordWrap ];
the u can set the frame of label according to height you get form here like
label.frame = CGRectMake(label.frame.origin.x,label.frame.origin.y,label.frame.size.width,size.height);
and if you have such multiple labels the uyou can do following
CGSize size = [label1.text sizeWithFont:label1.font constrainedToSize:CGSizeMake(label1.frame.size.width, FLT_MAX) lineBreakMode:UILineBreakModeWordWrap ];
label1.frame = CGRectMake(label1.frame.origin.x,label1.frame.origin.y,label1.frame.size.width,size.height);
size = [label2.text sizeWithFont:label2.font constrainedToSize:CGSizeMake(label2.frame.size.width, FLT_MAX) lineBreakMode:UILineBreakModeWordWrap ];
label2.frame = CGRectMake(label2.frame.origin.x,label1.frame.origin.y+label1.frame.size.height+10,label2.frame.size.width,size.height);
size = [label3.text sizeWithFont:label3.font constrainedToSize:CGSizeMake(label3.frame.size.width, FLT_MAX) lineBreakMode:UILineBreakModeWordWrap ];
label3.frame = CGRectMake(label3.frame.origin.x,label2.frame.origin.y+label2.frame.size.height+10,label3.frame.size.width,size.height);
and finaly you can adjust the content height of your scroll view according tho last label.
am being driven crazy trying to obtain how many lines a string will require as entered in a UITextView.
The code below for some reason does not split the string supplied over lines and returns a stringSize = (o, 32) WTF?
I enter a crazy long string that is way past 320 but still no expected result?
NSString *t = #"in my app this string is a long line of text......";
CGSize stringSize;
UIFont *f = [UIFont fontWithName:#"Heiti SC" size:30];
stringSize = [t sizeWithFont:f forWidth:320.0f lineBreakMode: UILineBreakModeWordWrap];
HELP .... please
Try the next line:
stringSize = [t sizeWithFont:f constrainedToSize:CGSizeMake(320, 10000) lineBreakMode:UILineBreakModeWordWrap];
It works for me.
If it doesn't work then try to change the font to:
UIFont *f = [UIFont systemFontOfSize:30];