Can anyone provide any tips when it comes to implementing some text that scrolls horizontally?
Right now I have a ScrollView *scrollView with a UILabel *textLabel inside of it.
I have the position of the label changing within the view until it is outside the bounds of the screen. The label is then reset to its original position and it starts scrolling again.
The problem(s) I am having are when I say: [textLabel sizeToFit];
This takes away the labels ability to handle multiple lines of text because once I say [textLabel sizeToFit]; it changes it into one big string.
Is there a simpler way to achieve the desired effect?
ANY help is greatly appreciated
P.S. The text inside of the label will be parsed from a website... so the size/length of the string will not be consistant.
try this
CGSize maximumLabelSize = CGSizeMake(widthOfLabel, 9999);
CGSize expectedLabelSize = [#"Text" sizeWithFont:[UIFont fontWithName:#"Helvetica" size:14] constrainedToSize:maximumLabelSize lineBreakMode:UILineBreakModeWordWrap];
then you can use expectedLabelSize.height or expectedLabelSize.width and change the frame size of UILabel
Related
I've tried to search online, but haven't found a clear answer for my issue so I've come to ask for your expert advice. I have a view with 2 labels on it. Both label will display different string length from the plist.
When i run the app, the label will overlapped with other label depending on the string length.
Below is the screenshot for my problem
You have to change your secondLabel origin.
CGRect frame = secondLabel.frame;
frame.origin.y= firstLabel.frame.origin.y + firstLabel.frame.size.height;
[secondLabel setFrame:frame];
Better option is to use UITextView instead of UILabel but if you still want to go with lable then
with the use of below code you can find the height of the text and can set your lable's frame according to that height
NSString *text = [arr objectAtIndex:indexPath.row];
CGSize constraint = CGSizeMake(contentWidth - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:20] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];
CGFloat height = MAX(size.height, 44.0f);
here contentWidth is the width of your label and CELL_CONTENT_MARGIN = 10;
You need to set the 'Y' of second label. Take the Height of first label text and then set it to the Second Label 'Y'.
Hope it'll help you.
CGSize LblSize=[[Label1 text] sizeWithFont:[UIFont systemFontOfSize:14.0f] constrainedToSize:CGSizeMake(320.0f, 400.0f)];
UILabel *Label2=[[UILabel alloc] initWithFrame:Label2Rect];
CGRect Label2Rect=[Label2 Frame];
Label2Rect.origin.y=LblSize.height+30.0f; //add some extra spaces, I have added 30.0f here
[Label2 setFrame:Label2Rect];
Ey, you can solve it in many ways.
For example, you can fill your first label with the dessired text and then call to
[label1 sizeToFit]
With that call, your label now has the proper size, adapted to the lenght of your text. Now you can just place your second label after your first one.
label2.frame = CGRectMake (x, label1.frame.size.height + ..., .....)
Hope it helps!
I have a UISegmentControl with default style (White). I want to add text on it. But the text that i want to put on it is a long text.
I have to show the text in 2 lines of a segment. But i dont have to raise the width of the segment Because of screen width limit & no of segments.
I had tried to put a label on segment control programmatically, but my label is not displayed. Although we can put a label on segment control using XIB. but due to dynamic nature of text & segment control, I have to draw the segment control programmatically & also put the text on it.
Guidance will be appreciated.
Hi friend segment controller already have the label as the subview, so this code is helpful to achieve the multiline text to segment control
for (id segment in [segmentedControl subviews])
{
for (id label in [segment subviews])
{
if ([label isKindOfClass:[UILabel class]])
{
//hear u add any of delegate function to increase the height and other label functionality in this
[label setTextAlignment:UITextAlignmentCenter];
[label setFont:[UIFont boldSystemFontOfSize:12]];
//to adjust the label size manually with respect to text use below code
CGSize labelSize = CGSizeMake(100, 80);
CGSize theStringSize = [label.text sizeWithFont:label.font constrainedToSize:labelSize];
CGRect frame = label.frame;
frame.size = theStringSize;
}
}
}
Have a Good day
I have dynamic no of textviews and their size can also be dynamic, after each text view there are also dynamic no of labels, and each item is place on scroll view, so that scroll view also has dynamic size, So Someone guide me how to accomplish this task?
forgive me if this is repetitive question plz!
For setting dynamic height of UILabel or UITextView, you can implement following method
This example is for UILabel, Remember, you need to set noOfLines property before setting dynamic height, you can set noOfLines to max number.
NSString *text = #"Your text here";
CGSize size = [text sizeWithFont:lblName.font constrainedToSize:CGSizeMake(lblName.frame.size.width, 10000)];
scrollview.contentSize = CGSizeMake(scrollView.frame.size.width, size.height);
Hope this helps
What you need,
1 calculate textsize which you are going to to show on differnt controls.
for this use this line
[titleString sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(285,9999) lineBreakMode:UILineBreakModeWordWrap];
2 Also use labels instead of textView at each place if you only need to show text.
because textViews justify the text means your same line can be fit in one line but also in two lines
3 set the scrollView contentSize as above answers says.by adding all textSizes with consider some spaces between various controls.
you can set the size of scrollview using setContentSize: and query size [scrollView contentSize]
Is there a way to get the correct size of an NSString using:
- (CGSize)sizeWithFont:(UIFont *)font forWidth:(CGFloat)width lineBreakMode:(UILineBreakMode)lineBreakMode
that doesnt get thrown off by 2 or 3 hundred character strings. At the moment if I try to use this method on these long strings it incorrectly calculates them and I end up with lots of whitespace at the bottom of the UITextView.
I've tried using UILineBreakModeWordWrap and UILineBreakModeCharacterWrap.
the resizing is being done in
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
CGFloat result = 44.0f;
NSString* text = nil;
CGFloat width = 0;
CGFloat tableViewWidth;
CGRect bounds = [UIScreen mainScreen].bounds;
tableViewWidth = bounds.size.width;
width = tableViewWidth - 150;
text = stringWithLongWords;
if (text) {
CGSize textSize = { width, 20000.0f };
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:10.0f] constrainedToSize:textSize lineBreakMode:UILineBreakModeWordWrap];
size.height += 50.0f;
result = MAX(size.height, 44.0f+30.0f);
}
return result;
}
UITextView is not exactly like a UILabel wrapped in a UIScrollView. It has line spacing different from the font size and margins that sizeWithFont:constrainedToSize:linkBreakMode: doesn't account for.
Knowing your font size you might be able to calculate the # of lines and take line spacing into account. You can guess at the margins and try to trick sizeWithFont: to give a more useful answer.
The popular solutions seem to be:
just use a UILabel if you don't need any UITextView functionality
if you need hyperlinks, overlay UIButtons that look like hyperlinks over a UILabel
use an off-screen UITextView and its sizeToFit method to get a real answer
I had no luck w/ the 3rd option but it sounds like it should work, so perhaps I did something wrong.
I'm going to try using a UILabel and overlaying buttons for hyperlinks. We'll see how that turns out.
If that fails, there is always the option taken by Loren Brichter (of Tweetie fame): draw everything into a UIView yourself using CoreGraphics.
Good luck!
Check out this post How do I size a UITextView to its content?
It looks like textView.contentSize.height should work (with the caveat that the the correct contentSize is only available after the UITextView has been added to the view with addSubview)
You said that you have a UITableView with differing heights. Have you set the reuse identifier to the same thing for all of the cells? It could be that older cells with their height already set are being reused. If this is the problem, you should resize the cell again when it's being reused.
The best solution I have found so far is to have a separate hidden UITextView with the same font settings, and set its text. After that its contetSize should be accurate.
The width you are using is the width for your UITextView... but you aren't concerned with that width, you are concerned with the width of the actual text area nested inside the text view.
UITextViews, by default, have padding around their borders to produce a space in-between the typed text and the edge of the UITextView a few pixels wide (and long for the top)... To get the correct size you shouldn't use
textView.frame.size.width
but rather,
textView.frame.size.width-(textView.contentInset.left+textView.contentInset.right+textView.textContainerInset.left+textView.textContainerInset.right+textView.textContainer.lineFragmentPadding/*left*/+textView.textContainer.lineFragmentPadding/*right*/)
^Which takes the width of the UITextView and subtracts out all the padding so you are left with the width of just the type-able text area.
Same goes for height except for lineFragmentPadding doesn't have a bottom so you only subtract it out once instead of twice.
The final code is something like this:
CGSize textViewContentSize = CGSizeMake(theTextView.frame.size.width-(theTextView.contentInset.left+theTextView.contentInset.right+theTextView.textContainerInset.left+theTextView.textContainerInset.right+theTextView.textContainer.lineFragmentPadding/*left*/+theTextView.textContainer.lineFragmentPadding/*right*/), theTextView.frame.size.height-(theTextView.contentInset.top+theTextView.contentInset.bottom+theTextView.textContainerInset.top+theTextView.textContainerInset.bottom+theTextView.textContainer.lineFragmentPadding/*top*//*+theTextView.textContainer.lineFragmentPadding*//*there is no bottom padding*/));
CGSize calculatedSize = [theTextView.text sizeWithFont:theTextView.font
constrainedToSize:textViewContentSize
lineBreakMode:NSLineBreakByWordWrapping];
CGSize adjustedSize = CGSizeMake(ceilf(calculatedSize.width), ceilf(calculatedSize.height));
Inspired by #MrNickBarker's answer, here's my solution:
CGFloat width = 280.0f;
UITextView *t = [[UITextView alloc] init];
[t setFont:[UIFont systemFontOfSize:17]];
[label setText:#"some short or long text, works both"];
CGRect frame = CGRectMake(0, 0, width, 0);
[t setFrame:frame];
// Here's the trick: after applying the 0-frame, the content size is calculated and can be used in a second invocation
frame = CGRectMake(0, 0, width, t.contentSize.height);
[t setFrame:frame];
The only issue remaining for me is that this doesn't work with modified insets.
Still can't believe such twists are required, but since -[NSString sizeWithFont:forWidth:lineBreakMode:] does not respect insets, paddings, margins, line spacings and the like, it seems this is the only working solution at the moment (i.e. iOS 6).
This general topic has been asked here multiple times: how to render UITableViewCells with varying amount of text and thus varying height. The canonical answer is: you calculate the height in table view controller delegate in heightForRowAtIndexPath using sizeWithFont:constrainedToSize:lineBreakMode:. Later, the cell gets drawn, and you use something like [label sizeToFit] if needed, and all works like magic.
My problem: I am getting wrapping for some cells because sizeWithFont: returns different dimensions from actual drawing.
A specific example:
The text is this: "People forget that #BillGates had a sexy 1/4-inch thick slate back in 1993 from NEC. Whatever happens this week will NOT be about hardware!"
CGSize theSize = [text sizeWithFont:[UIFont systemFontOfSize:17.0f] constrainedToSize:CGSizeMake(310.0f, FLT_MAX) lineBreakMode:UILineBreakModeWordWrap];
NSLog(#"calculated size for %#: %f, %f",text, theSize.width, theSize.height);
This returns: 306.000000, 84.000000. (I.e 4 rows with 17px font and 4px linespacing, 21px leading.) Good.
However, later when actually drawing the cell:
label = (UILabel *)[cell viewWithTag:3];
label.text = [NSString stringWithFormat:#"%#", text];
label.lineBreakMode = UILineBreakModeWordWrap;
label.font = [UIFont systemFontOfSize:17.0f];
CGSize labelSize;
labelSize = label.frame.size;
NSLog(#"label size before resizing: %f, %f", labelSize.width, labelSize.height);
[label sizeToFit];
labelSize = label.frame.size;
NSLog(#"label size after resizing: %f, %f for text %#", labelSize.width, labelSize.height,text);
(UILabel is loaded as part of UITableViewCell from NIB. In IB I set it to 310px wide.)
This should return exactly the same size as above. Instead, I get 281.000000, 105.000000 as the dimensions after sizeToFit call. It is now 5 lines at drawing time instead of 4, and the text spills over, I see the spillover in the UI.
So, for the same text, I am getting two different dimensions calculated, and can't figure it out. Is it something about UILabel? Does it have some inner margins? This keeps happening for some texts but not others, and I have not traced it to something particular about the strings; seems random. This topic highlights that there are two processing passes: calculating height vs actual drawing. This is consistent with what I'm seeing. But I don't understand what exactly is going on or how to fix it.
The question: why am I seeing two different calculated sizes, and how do I fix it?
Of course, the solution is obvious 30 seconds after posting. Maybe useful to others too...
The size by sizeWithFont: was correct. The sizes I calculated in the above way were incorrect, because [label sizeToFit] reduces the width of the label's frame. At subsequent calls to the same code, it started off with the frame that may already have been reduced.
The fix was to simply reset the frame width to a known good width before sizing to fit:
CGRect labelFrame = label.frame;
labelFrame.size.width = 310;
label.frame = labelFrame;
[label sizeToFit];
For multiline labels you need set
cell.textLabel.numberOfLines = 0;
and then
[cell.textLabel sizeToFit];
But for pretty view you need add some padding pixels. And your app will be awesome!
titleSize = [title sizeWithFont:[UIFont systemFontOfSize:(CGFloat)17.0]
constrainedToSize:CGSizeMake(280, 2000)
lineBreakMode:NSLineBreakByWordWrapping];