Append "..." at the end of fourth line - iphone

I am displaying text using UILabel. I want to display only 4 lines of text, if there is more text i have to append "..." at the end of fourth line. Any help on this is greatly appreciated. Thanks in advance.

label.numberOfLines = 4;
label.lineBreakMode = NSLineBreakByTruncatingTail;
or in case you support iOS <= 5.
label.lineBreakMode = UILineBreakModeTailTruncation;

lbl.lineBreakMode = UILineBreakModeTailTruncation;
lbl.numberOfLines = 4;
For iOS >= 6,
lbl.lineBreakMode = NSLineBreakByTruncatingTail;
lbl.numberOfLines = 4;

Related

How to decrease the UIFont size and show large sentence one line UILabel iPhone?

I have an UILabel in my iPhone app. I want to show large sentence in UILabel in a single line. That means I want to decrease the font size and show the full text in a visible area. I don't want to show it in multiple lines. Can anyone please help me to do this?
My code is:
textViewLabel = [[UILabel alloc] initWithFrame:CGRectMake(8, 5, 194, 20)];
textViewLabel.text = #"I have an UILabel in my iPhone app. I have want";
textViewLabel.textColor = [UIColor lightGrayColor];
textViewLabel.font = [UIFont fontWithName:#"Helvetica" size:14];
textViewLabel.numberOfLines = 0;
textViewLabel.backgroundColor = [UIColor clearColor];
CGSize maximumLabelsize2 = CGSizeMake(194,20);
CGSize expectedLabelsize2 = [result sizeWithFont:textViewLabel.font constrainedToSize:maximumLabelsize2 lineBreakMode:textViewLabel.lineBreakMode];
CGRect messagesFrame = textViewLabel.frame;
messagesFrame.size.height = expectedLabelsize2.height;
textViewLabel.frame = messagesFrame;
Any one please help me to do this? Thanks in advance.
change numberOfLines property from
textViewLabel.numberOfLines = 0; // 0 means as many lines as needed
to
textViewLabel.numberOfLines = 1;
add
textViewLabel.adjustsFontSizeToFitWidth = YES;
Conside setting
textViewLabel.minimumFontSize = someValue; // default is 0.0
Try this property:
myLabel.adjustsFontSizeToFitWidth = YES;
try this :
[textViewLabel setAutoresizingMask:UIViewAutoresizingFlexibleWidth];

UILabel Letters being cut off

I am trying to use a special character in the UILabel. But it is chopping off the labels. I put in a red background to show where the edge of the label is. Any reason why this may be happening? I have maskToBounds and clipToBounds set to NO;
CGSize lblSize = [FontHelper sizeForText:text withFont:[self getBodyFont] withWidth:kColumnWidth andNumberOfLines:1000];
UILabel*lbl = [[UILabel alloc] initWithFrame:CGRectMake(x,
y + body.frame.size.height - columnHeight - kBottomMargin,
kColumnWidth,
lblSize.height)];
lbl.clipsToBounds = NO;
lbl.layer.masksToBounds = NO;
lbl.text = text;
lbl.backgroundColor = [UIColor redColor];
lbl.font = [self getBodyFont];
lbl.textColor = [UIColor blackColor];
lbl.numberOfLines = 0;
lbl.textAlignment = UITextAlignmentLeft;
lbl.lineBreakMode = UILineBreakModeWordWrap;
I think I found the solution. The client had give me the above text in a Word Doc and I had copy and pasted into the file. The space between the dot and the text appeared to be a space but was other special character. So copy/paste flared its ugly head.
For anyone else who stumbles on this question, I had the same problem and in my case there were a few tab characters in the UILabel, which caused the same effect.

UILabel getting displayed differently on 3rd and 4th gen iPods

I have a UILabel which behaves differently on 3rd & 4th Gen iPods.
My UILabel is:
<UILabel: 0x881e80; frame = (10 76; 70 28); text = 'Produits Indisponibles'; clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x863fe0>>
Now, on 4th gen iPod it works fine and text wraps into 2 lines properly but on 3rd gen iPod text wraps but second word (Indisponibles) disappears. What could be the reason?
If I increase the frame width by 1 more pixel, it works fine. Here is my code:
self.titleLabel = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
self.titleLabel.text = #"Produits Indisponibles";
self.titleLabel.isAccessibilityElement = NO;
self.titleLabel.backgroundColor = [UIColor clearColor];
self.titleLabel.opaque = NO;
self.titleLabel.textColor = [UIColor whiteColor];
self.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
self.titleLabel.numberOfLines = 2;
self.titleLabel.highlightedTextColor = [UIColor whiteColor];
self.titleLabel.baselineAdjustment = UIBaselineAdjustmentAlignBaselines;
self.titleLabel.textAlignment = UITextAlignmentCenter;
self.titleLabel.font = [UIFont boldSystemFontOfSize:11];
CGSize aLabelSize = CGSizeMake(self.frame.size.width - 20, 28);
CGSize aStringSize = [self.titleLabel.text sizeWithFont:self.titleLabel.font constrainedToSize:aLabelSize lineBreakMode:UILineBreakModeWordWrap];
CGFloat theYOrigin = self.bounds.size.height - 34;
if (aStringSize.height < 15) {
theYOrigin = theYOrigin + 14;
}
self.titleLabel.frame = CGRectMake(10, theYOrigin, aLabelSize.width, aStringSize.height);
[self addSubview:self.titleLabel];
Retina displays use Helvetica Neue as the system font, while older models use normal Helvetica. It's probably related to the different font metrics. Hard-code a font you know is available on both to see if they display consistently.
I do not see the reason but when I increased the width of the frame by 2 more pixels it worked. Anyone know the reason for this will be enlightening.

How to break a string into two parts

I am parsing a string from my plist and trying to break it into two parts to display them in two or three lines as per the break.
NSArray * parts = [text componentsSeparatedByString:#"\n"]; // this text is coming from plist.
int nthLine = 0;
for(NSString *str in parts)
{
CGRect outerFrame = CGRectMake(frame1.origin.x, frame1.origin.y + 45*nthLine, frame1.size.width, 45);
SFNDoorStyledView * question = [[SFNDoorStyledView alloc]initWithFrame:outerFrame];
question.backgroundColor = [UIColor clearColor];
question.tag = 2;
[question drawString:text inRect:CGRectMake(0,0,500,150) usingFontNamed:#"Futura-Bold" size:40.0 lineSpacing:40.0 kernValue:-3.0 color:#"#7d7066"];
[self.view addSubview:question];
}
nthLine = nthLine +1;
Be careful what \n means in different contexts.
In this context
NSArray * parts = [text componentsSeparatedByString:#"\n"];
\n is interpreted as a newline character.
In your plist \n will be the actual characters \ and n
You could use option-enter to add the newline character to the plist and then use your code above or better yet:
NSArray * parts = [text componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
Or if you want to use this custom '\n' delimiter you could use
NSArray * parts = [text componentsSeparatedByString:#"\\n"];
Consider using a UILabel to hold your text, which you can size dynamically based on the string itself. No need to include \n in the string. Just size/constrain your label to the width you need, and the height will be calculated for you based on other attributes like font and font size. Here's a representative snippet.
// Size the label based on the font and actual text
UILabel *l = [[[UILabel alloc] initWithFrame:CGRectMake(10, 5, 300, 50)] autorelease];
l.font = [UIFont boldSystemFontOfSize:14.0];
l.lineBreakMode = UILineBreakModeWordWrap;
l.textAlignment = UITextAlignmentCenter;
l.numberOfLines = 0; // Allow for as many lines as needed
l.backgroundColor = [UIColor clearColor];
CGSize constraintSize = CGSizeMake(300, MAXFLOAT);
CGSize labelSize = [l.text sizeWithFont:l.font
constrainedToSize:constraintSize
lineBreakMode:UILineBreakModeWordWrap];
CGRect frame = l.frame;
frame.size = labelSize;
// Center it
frame.origin.x = floor((320.0 - labelSize.width) / 2.0);
l.frame = frame;
[v addSubview:l];
Check if the line separator is actually \n. It could also be \r. So using componentsSeparatedByString:#"\r\n" might help.
Actually plist saves your string as a text. SO if you enter \n it takes it as a text and not as linebreak. In spite of doing it i break the string in plist only. For ex: if you have a string , What is your current income monthly? and you want to break it from income. Then you can write What is your and then press alt+enter and enter the text in new line.

iPhone UILabel sizeWithFont:

I'm trying to measure the visual size of a NSString that takes into account the number of lines I can render. However, sizeWithFont doesn't take into account numberOfLines property? So my layout algorithm positions everything lower than they actually need to be.
_price = [[UILabel alloc] init];
_price.text = myPriceValue;
_price.lineBreakMode = UILineBreakModeWordWrap;
_price.numberOfLines = 3;
_price.backgroundColor = [UIColor clearColor];
_price.textColor = TTSTYLEVAR(colorPrice);
/// the follow code ignores numberOfLines and just tells me the size of the whole block.
// I'd like it to be aware of numberOfLines
//
CGSize priceSize = [_price.text sizeWithFont:_price.font
constrainedToSize:CGSizeMake(maxWidth, CGFLOAT_MAX)
lineBreakMode:UILineBreakModeWordWrap];
Does anyone know how to do this using the iPhone SDK?
Instead of CGFLOAT_MAX for the max height of your text calculation, try getting the size of one line with this:
[_price.text sizeWithFont:_price.font].height
and then multiplying that by the maximum # of lines you want, then plugging that into the height of the size you are constraining yourself to. It'd probably look like this:
_price = [[UILabel alloc] init];
_price.text = myPriceValue;
_price.lineBreakMode = UILineBreakModeWordWrap;
_price.numberOfLines = 3;
_price.backgroundColor = [UIColor clearColor];
_price.textColor = TTSTYLEVAR(colorPrice);
CGFloat lineHeight = [_price.text sizeWithFont:_price.font].height;
CGSize priceSize = [_price.text sizeWithFont:_price.font
constrainedToSize:CGSizeMake(maxWidth, lineHeight * _price.numberOfLines)
lineBreakMode:UILineBreakModeWordWrap];
Don't use this if you ever set number of lines to 0 as your max height will be 0 in that case; you should use CGFLOAT_MAX then.
Use the UILabel's sizeToFit instead of sizeWithFont: to layout a multi-line UILabel, since sizeWithFont: will truncate the string (see apple docs). The following code reduces the font size of a label until the text fit into a the specified size... multiple lines of text will be used as soon as they fit into the specified height:
-(void)setFontSizeOfMultiLineLabel: (UILabel*)label
toFitSize: (CGSize) size
forMaxFontSize: (CGFloat) maxFontSize
andMinFontSize: (CGFloat) minFontSize
startCharacterWrapAtSize: (CGFloat)characterWrapSize{
CGRect constraintSize = CGRectMake(0, 0, size.width, 0);
label.frame = constraintSize;
label.lineBreakMode = UILineBreakModeWordWrap;
label.numberOfLines = 0; // allow any number of lines
for (int i = maxFontSize; i > minFontSize; i--) {
if((i < characterWrapSize) && (label.lineBreakMode == UILineBreakModeWordWrap)){
// start over again with lineBreakeMode set to character wrap
i = maxFontSize;
label.lineBreakMode = UILineBreakModeCharacterWrap;
}
label.font = [label.font fontWithSize:i];
[label sizeToFit];
if(label.frame.size.height < size.height){
break;
}
label.frame = constraintSize;
}
}
Call this with a label that has your favorite text and font:
UILabel *label = [[UILabel alloc] initWithFrame: CGRectZero];
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor whiteColor];
label.text = text;
label.font = [UIFont fontWithName: #"Helvetica" size: 16];
[self setFontSizeOfMultiLineLabel: label toFitSize: CGSizeMake(200, 44) forMaxFontSize: 16 andMinFontSize: 8 startCharacterWrapAtSize: 11];
The startCharacterWrapAtSize parameter lets you choose to use characterWrap starting at the giving font size. This should save space in the case wordWrap would use really small fonts.
edit: bugfix
Instead of trying to do it in one call, do something like this (pardon the pseudocode, it's late):
NSString *s = _price.text;
UIFont *font = _price.font;
CGFloat fontSize = font.pointSize;
while (TRUE)
{
CGSize priceSize = [s sizeWithFont: font constrainedToSize:
CGSizeMake(maxWidth, fontSize) lineBreakMode: UILineBreakModeWordWrap];
if ( /* priceSize is satisfactory */ )
{
break; // Make sure this exits, eventually!!!
}
fontSize -= 1.0; // or a smaller decrement if you like
font = // new, smaller font
}
The correct answer is, of course, you need to set numberOfLines to 0, which will cause the framework to compute the result with however many lines it needs. See also this question.
Of course it doesn't take it into account, since nothing being called or passed in has that information. You're strictly working with strings, sizes, and fonts. It's the label that has the number of lines in it.
I'm not sure what exactly your problem is; are you getting a size that's too tall or too short, or what? You can find out the number of lines of text by dividing the height of the result by the height of the font, which is the value of the ascender plus the descender, I believe.