iphone programming obj-c : removing the "..." - iphone

when you enter a too long sentence for the iphone it automaticaly add a "..." at the end to show you there is other stuff you don't see right. I want to delete those "...".
image :
alt text http://img691.imageshack.us/img691/2159/screenshot20100602at095.png

Well, I'm assuming you're using a label. Look into the "lineBreakMode" property. Your solution will probably involve some combination of that property in conjunction with the "numberOfLines" property. For example, setting the "numberOfLines" property to 0 will automatically increase the height of a label to fit all text. So using that with a UILineBreakModeWordWrap would probably do the trick.
UILabel *label = [[UILabel alloc] init];
label.numberOfLines = 0;
label.lineBreakMode = UILineBreakModeWordWrap;
label.text = #"Light beer 5% 10oz Glass served cold";
[label release];

You have several options for that:
Set label's lineBreakMode property to UILineBreakModeClip - that way your sentence will just be clipped without "..." on the end
Set label's adjustsFontSizeToFitWidth property to YES - label will automatically reduce font size to fit string into available space
Make your UILabel have multiple lines - set its numberOfLines property to 0 and lineBreakMode to UILineBreakModeWordWrap. Although with this approach your label's height must be big enough to contain several lines...

Related

How to estimate the proper height of a UITextField, to hold text of given font size

I want to display a single-line text field using UITextField and I need to know before displaying it, the proper size for its containing UICollectionViewCell. The text can be one of multiple font sizes and I need to get the right height for displaying it comfortably.
Since the text is not known in advance (it can be edited by the user), I can't use NSAttributedString's -size and -boundingRectWithSize:options:context: with anything but dummy text, in which case I can't really trust the resulting size to hold any piece of text, right?
I guess my question is: Is there a rule of thumb about typography in general, or some useful API I'm not aware of, that would allow me to determine that for displaying text at X pt, I need a text field with a height of Y px.
UITextField implements the sizeThatFits: method. So the most reliable way to get the size of your text field is to actually create one, set it up like you would set up your real text fields, and ask it for a suitable size. You don't even have to give it placeholder text, because UITextField will choose the size based on its font, not on its text.
UITextField *dummy = [[UITextField alloc] init];
dummy.font = [UIFont systemFontOfSize:20];
dummy.borderStyle = UITextBorderStyleRoundedRect;
CGFloat requiredHeight = [dummy sizeThatFits:CGSizeMake(HUGE_VALF, HUGE_VALF)].height;
// requiredHeight == 30 in my test
If your deployment target is iOS 6.0 or later, you can instead use the intrinsicContentSize property, like this:
CGFloat requiredHeight = dummy.intrinsicContentSize.height;
// requiredHeight == 30 in my test
Note that sizeThatFits: still works in iOS 6, but intrinsicContentSize is a little easier to understand.
iOS always returns a height of 30 units when the borderStyle is RoundedRect. If you want to compute the height required by a custom font, you have to change the borderStyle to any other value, compute the height, and change the borderStyle back.
textField.borderStyle = UITextBorderStyle.None;
let sizeThatFits = textField.sizeThatFits(CGSize(width:textField.bounds.width, height: CGFloat.max));
textField.bounds.height = sizeThatFits.height;
textField.borderStyle = UITextBorderStyle.RoundedRect;

how to consume string from middle and show in UILabel

I have an String that is "WAKEFIELD - TRINITYIGINY - (3.15 miles)" that need to display like this in a UILabel.That means char is consume from middle.
Note that,it should be dynamic and need to display into UITableViewCell.The strings length is not fix.It is clear that,its only string.
thanks in advance
UILabel has a property for truncation (lineBreakMode)
http://developer.apple.com/library/ios/#documentation/uikit/reference/UILabel_Class/Reference/UILabel.html
If you set it to NSLineBreakByTruncatingMiddleit will truncate in the middle.
You can yet the components from your original string using [myString componentsSeparatedByString:#" - "], then you get an array. Then, create three UILabel with different font in your cell and set lineBreakMode for the second.
If you want to create this three label with cellWidth, you probably creates your cellWidth, you put your first label to the left side of the cell, the third to the right side, and you have some place left in the middle probably. This is the place of your second label. You can calculate the size of your first and third label:
CGSize labelSize = [label.text sizeWithFont:label.font constrainedToSize:maxPossibleSizeOfTheLabel];
Use the lineBreakMode property of UILabel:
UILabel *label = ...
label.lineBreakMode = NSLineBreakByTruncatingMiddle;

UILabel and numberOfLines and sizeToFit: [duplicate]

This question already has answers here:
Vertically align text to top within a UILabel
(51 answers)
Closed 8 years ago.
On iOS 5.
I have a UILabel element that is originally placed in a nib. I want the x placement to stay fixed. I want it to take either 1 line, or 2 lines. If more than two lines, it should use the line break setting to show the ellipsis.
I use numberOfLines property and -sizeToFit
If I set the UILabel's numberOfLines property to 0, it will correctly see that for some text, there is not enough room and will wrap it to a second line after calling -sizeToFit but in the rare case that the line is long enough to stretch to 3 lines, I get three lines, which I don't want. If I set numberOfLines property to 2, it actually stretches the whole thing out onto one line and elongates my initial frame set in the nib to be much wider.
CGRect titleFrame = [[self titleLabel] frame];
[[self titleLabel] setNumberOfLines:0];
[[self titleLabel] setText:newProductTitleText];
[[self titleLabel] sizeToFit];
CGRect newTitleFrame = [[self titleLabel] frame];
The CGRect are just there for me to be able to calculate things after the fact. So setting numberOfLines to 0 works, and will not change the original origin.x in the frame, and will break long text into multiple lines, but will not constrain it to 2 lines. Setting numberOfLines property to 2, which, when I read the Apple docs
This property controls the maximum number of lines to use in order to fit the label’s text into its bounding rectangle. The default value for this property is 1. To remove any maximum limit, and use as many lines as needed, set the value of this property to 0.
It seems I should be able set this to two and still have it work. I would expect sizeToFit to expand in a positive X and Y direction when expanding to fit all the text but it is expanding in a negative X direction when numberOfLines is set to other than 0.
ETA: the "Autosize" struts are set to upper and left to fix it at a min x,y.
Thanks for any insight.
I had a similar problem where -[UILabel sizeToFit] was not respecting the max width I set when numberOfLines was set to 2. Here's how I solved that problem:
CGFloat titleMaxWidth = 200;
CGFloat titleMinHeight = 30;
CGFloat titleMaxHeight = 40;
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, titleMaxWidth, titleMaxHeight)]; // alternatively, you could do this in a nib
titleLabel.numberOfLines = 0;
titleLabel.text = #"The title label will be sized appropriately with this technique.";
titleLabel.font = [UIFont boldSystemFontOfSize:16];
[titleLabel sizeToFit];
titleLabel.numberOfLines = 2;
if (titleLabel.height > titleMaxHeight)
{
titleLabel.height = titleMaxHeight;
}
else if (titleLabel.height < titleMinHeight)
{
titleLabel.height = titleMinHeight;
}
As you can see, I also wanted a minimum height for my label, as -sizeToFit often makes the label really small, but you could disregard that code if you don't care about a minimum height. The "magic number" of 40 for the titleMaxHeight comes from experimentation and finding out that a 2 line label with this font really only needs 40px. In this code, -sizeToFit is mainly used to keep the text within the width and determine whether the initial height of 40 can be reduced when we have a short string of text.
I used the UIFont property lineHeight:
CGFloat labelHeight = label.font.lineHeight*label.numberOfLines;

How to display dots in place of long text in UITableViewCell

I have a table view with 2 rows.
in these rows in detail TextLabel i have long text i didn't want to display all the text but
I need to display dots for last characters if it is a long text
for ex:
text in cell.detailTextLabel is :'DATABASE Entered in to the cell'
I want like as : 'DATA BASE Ente.......'
How it is possible?
use label to display and set the property like
cell.textLabel.lineBreakMode = UILineBreakModeTailTruncation;
cell.textLabel.numberOfLines = 1;
dot will come automatically.
if your text is big compre to label size. then dot will come automatically.
ya you have to fix size of font.
You can set the property of UILabel:
#property(nonatomic) UILineBreakMode lineBreakMode
Set lineBreakMode of UILabel to UILineBreakModeTailTruncation, but it's the default value already. :-)
Try setting adjustFontSizeToFitWidth to NO and set lineBreakMode to UILineBreakModeTailTruncation

Best way to add a large chunk of text to a UIScrollView?

What is the best way to display a large chunk of text (taken from a .txt file in the app) in a UIScrollView so a user can scroll about it? Length of the text is variable.
On Interface Builder open the Attributes Inspector (if not already open - command-1) and uncheck "Editable".
Also notice there's a Scroll View section below. Make sure "Scrolling" is checked.
Hope this helps somebody (the post is a year old so I guess by now the one who posted it doesn't need this info).
I came here looking for an answer and found that all answers are bad - or flat out wrong.
The proper way to do this is using UITextView by itself. Since it is a descendant of UIScrollView, it has scrolling built-in and lots of features for adjusting formatting such as the insets etc.
If you intend to only show text, you need to explicitly disable editing. You do this by setting the "editable" property to false.
And if you want to disable the text selection mechanism, set the "selectable" property to false.
In newer versions of iOS, UITextView has added support for NSTextContainer which gives you even greater control over formatting.
One way I had working for me is to create UILabel, set text and then set content size of scrollview by it size.
Here is an example
Quote:
// alocate and initialize scroll
UIScrollView *myScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 460.0f)];
// alocate and initialize label
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 460.0f)];
// add long text to label
myLabel.text = #"Lorem ipsum... long text here";
// set line break mode to word wrap
myLabel.lineBreakMode = UILineBreakModeWordWrap;
// set number of lines to zero
myLabel.numberOfLines = 0;
// resize label
[myLabel sizeToFit];
// set scroll view size
myScroll.contentSize = CGSizeMake(myScroll.contentSize.width, myLabel.frame.size.height);
// add myLabel
[myScroll addSubview:myLabel];
// add scroll view to main view
[self.view addSubview:myScroll];
Usage of the UITextView into the UIScrollView. I could not recommend this because UITextView is the subclass of UIScrollView. Apple is also recommending the same.
Use UILabel in this case as a sub-view,
Put the UITextView into the UIScrollView.