Formatting UITableViewCell - iphone

I have a UITableView cell and I need to set it's label to "Sometext (number)". I can do that, but I need the "(number)" to be grayish, how can I do that? Here's an image which might make what I am trying to ask clearer:
I need the 12, 24, 48, 96 to be gray just like in this picture.
Thanks.

You need two UILabels, You'll have the
label1.textColor = [UIColor blackColor];
label2.textColor = [UIColor grayColor];
now then that's the most obvious part, when you set your text on your label1 you'll have to do a
label1.text = ...;
CGSize newsize = [label1.text sizeWithFont:label1.font constraintToSize:CGSizeMake(320, label1.frame.size.height)];
label2.frame = CGRectMake(label1.frame.origin.x + newsize.width + 5., label2.frame.origin.y, label2.frame.size.width, label2.frame.size.height);
so they stay close like they're one label.

you can set text label text color as -
[cellView.textLabel setTextColor:[UIColor grayColor]];

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];

Customize UILabel's Behavior

I am new to iphone.i found UILabel instance methodes very hard for me to implement.how can i use it.how can i customize the appearance of your text of a UIlabel further by subclassing UILabel.Plz i need little help to initiate.Foe example i have a label in my viewController how can i custmize it's text and hoe to subclass
.thanks in advance.
You can use so many properties of a UILabel like:
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 100, 40)];
lbl.font = [UIFont fontWithName:#"Helvetica" size:12.0]; // For setting font style with size
lbl.textColor = [UIColor whiteColor]; //For setting text color
lbl.backgroundColor = [UIColor clearColor]; // For setting background color
lbl.textAlignment = UITextAlignmentCenter; // For setting the horizontal text alignment
lbl.numberOfLines = 2; // For setting allowed number of lines in a label
lbl.lineBreakMode = UILineBreakModeWordWrap; // For setting line break mode
lbl.text = #"TitleText"; // For setting the text inside the label
Let me know if any thing other than this you want to know!!
The two methods
- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines
{
return CGRectInset(bounds, MARGIN, MARGIN);
}
- (void)drawTextInRect:(CGRect)rect
{
[super drawTextInRect: CGRectInset(self.bounds, MARGIN, MARGIN)];
}
We are using CGRectInset to create a rectangle that is either larger or smaller than an existing rectangle (bounds).
For Smaller rectangle, use positive values as MARGIN
For Larger rectangle, use positive values as MARGIN
All the best!!!

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..

How to change colors of a label in iphone sdk?

i am using one for loop for creating labels those are having text stored in nsarray.I want to display 1st labeltext color red and 2nd labeltext white and 3rd red ,4th white,5th red and 6th white ..........etc for all the labels that are stored in an array how to do this.help me on that ..can anyone shathanks in advance
If you want to alternate between two colors the simplest thing is to use the modulo operator (%) to figure out if you are on an even or odd label index.
Here is one way of doing this:
int labelIndex = 0;
for (NSString *labelText in labelArray)
{
UILabel *label = [[[UILabel alloc] init] autorelease];
label.text = labelText;
label.textColor = (labelIndex % 2) ? [UIColor whiteColor] : [UIColor redColor];
// Do something with the label here.
labelIndex++;
}
You can also use this technique to alternate between more than two different colors, but then I would suggest that you put the colors into an array of their own and index that array using (labelIndex % numColors) or something similar.
After you create a uilabel you can set the background color:
myLabel.backgroundcolor = [UIColor clearcolor]; // Specify what you want the
background color to be.
myLabel.textColor = [UIColor whiteColor]; // specify the text color for this property.
Give tags to labels starting from 1 and then just use this method.
for(int i=1;i<=[array count];i++)
{
UILabel *label = [[UILabel alloc] init];
label.tag = i;
[label setFrame:<your-frame>];
if(label.tag % 2 == 1)
{
[label setBackgoundColor:[UIColor blackColor]]; // This line may be optional for you
[label setTextColor:[UIColor redColor]];
}
else if(label.tag % 2 == 0)
{
[label setBackgoundColor:[UIColor blackColor]]; // This line may be optional for you
[label setTextColor:[UIColor whiteColor]];
}
}
Hope this helps you.
you can store UIColor objects in array and set on label while you set text from array in your loop. like this
label.textColor = [ColorsArray objectAtIndex:loopConuter];
You mean that you want to set colors on an array that is not constant sized?
maybe you can accomplish it with
[UIColor colorWithRed:<#(CGFloat)#> green:<#(CGFloat)#> blue:<#(CGFloat)#> alpha:<#(CGFloat)#>]
it takes 4 float values from 0 to 1, maybe you don't need the transparency. then you could write a function, which takes 1 int value and outputs 3 float values.
if you don't really want to have the colors set in a definite order, maybe you can generate 3 random numbers and than transform them into [0,1]

How to rotate viewForHeaderInSection

UITableView has custom header UIView, which contains UILabel. Since table has also section index at right side, I want to center the label in remaining space.
Problem is how to "center" label in same way, when device is rotated to landscape! I've tried both Interface Builder and code-only and something is always wrong.
Restrictions:
Label is as narrow as possible
Label must not be resized
Label must be always centered inside table width minus section index
One easy way to fix this would be making section header view detached from tableView right side. Tried to do it, failed. Here's some code from viewForHeaderInSection:
CGRect frame = CGRectMake(0, 0, tableView.bounds.size.width - 32, 50.0f);
// MAGIC 32 for SectionIndexTitles
UIView *view = [[[UIView alloc] initWithFrame:frame] autorelease];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
label.font = [UIFont fontWithName:#"Courier" size:20];
label.text = #"Section title long";
label.textAlignment = UITextAlignmentCenter;
label.backgroundColor = [UIColor redColor];
CGSize labelSize = [label.text sizeWithFont:label.font
constrainedToSize:frame.size
lineBreakMode:UILineBreakModeClip];
label.frame = CGRectMake(0, 0, labelSize.width, labelSize.height);
label.center = CGPointMake(frame.size.width/2.0f, kHeaderHeight/2.0f);
label.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
Any ideas welcome! Can't even recall how many different ways I have failed :)
Hey, set up as following in the IB:
Disable all autoresizing functionalities (no flexible width, height, nor any left, bottom, top, right).
Should do the job :)
To support the magic 32 pixels, try to position your Label as you need it.
That means:
Position it in the center of your UIView and move it 16 pixels to the left, now don't change the frame programmatically, else the "wonderful" auto-positiong will be manipulated what we don't really want to :)