Text Adjust to Fit in code? - iphone

Is there an equivalent of Adjust to Fit for UILabel text created dynamically in Xcode?
ie:
UILabel *someLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 35, 18)];
someLabel.font = [UIFont boldSystemFontOfSize:14];
someLabel.FOO_SIZETOFIT = YES; //FOO CODE
[someLabel release];

add this line
someLabel.adjustsFontSizeToFitWidth = YES;

Related

Vertically align text in a UILabel

I am new to iPhone,
How do I vertically align my text in the UILabel?
Here is my Code snippet,
UILabel *lbl=[[UILabel alloc]init];
lbl.frame=CGRectMake(10,10,100,100);
lbl.backgroundColor=[UIColor clearColor];
lbl.font=[UIFont systemFontOfSize:13];
lbl.text=#"123456";
[scrollVw addSubview:lbl];
text displayed is in the format of 123456 but i want text should be display vertically like,
6
5
4
3
2
1
Any help will be appriciated.
Its impossible to align the text in UILabel vertically. But, you can dynamically change the height of the label using sizeWithFont: method of NSString, and just set its x and y as you want.
As an alternative you can use UITextField. It supports the contentVerticalAlignment peoperty as it is a subclass of UIControl. You have to set its userInteractionEnabled to NO to prevent user from typing text on it.
EDIT 1
Well instead of a UILabel , Make a UITableView like :-
TableActivityLevel=[[UITableView alloc] initWithFrame:CGRectMake(224, 203, 27, 0) style:UITableViewStylePlain];
TableActivityLevel.delegate=self;
TableActivityLevel.dataSource=self;
TableActivityLevel.autoresizingMask = UIViewAutoresizingFlexibleHeight;
TableActivityLevel.rowHeight = 17;
TableActivityLevel.backgroundColor=[UIColor clearColor];
TableActivityLevel.layer.borderColor = [[UIColor clearColor]CGColor];
TableActivityLevel.separatorStyle = UITableViewCellSeparatorStyleNone;
EDIT 2 Found the method using UILabels too !! :) Check this out....
UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(0.0, 0.0, 10.0, 100.0)];
[label1 setBackgroundColor:[UIColor clearColor]];
[label1 setNumberOfLines:0];
[label1 setCenter:self.view.center];
[label1 setFont:[UIFont fontWithName:#"Helvetica" size:12.0]];
[label1 setTextColor:[UIColor whiteColor]];
[label1 setTextAlignment:UITextAlignmentCenter];
[label1 setText:#"1 2 3 4 5 6"];
[self.view addSubview:label1];
If it just a single lined text as in your example you can use various workarounds. I personally would create a UILabel subclass as follows,
#implementation VerticalLabel
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
self.numberOfLines = 0;
}
return self;
}
- (void)setText:(NSString *)text {
NSMutableString *newString = [NSMutableString string];
for (int i = text.length-1; i >= 0; i--) {
[newString appendFormat:#"%c\n", [text characterAtIndex:i]];
}
super.text = newString;
}
#end
You now just have to replace
UILabel *lbl = [[UILabel alloc] init];
with
UILabel *lbl = [[VerticalLabel alloc] init];
to get vertical text.
Hi the following code is work well
UILabel *lbl=[[UILabel alloc]init];
lbl.frame=CGRectMake(10,10,10,300);
lbl.backgroundColor=[UIColor clearColor];
lbl.numberOfLines=6; // Use one to 6 numbers
lbl.font=[UIFont systemFontOfSize:13];
lbl.text=#"123456";
NSString *reverse=lbl.text;
NSMutableString *reversedString = [NSMutableString string];
NSInteger charIndex = [reverse length];
while (charIndex > 0) {
charIndex--;
NSRange subStrRange = NSMakeRange(charIndex, 1);
[reversedString appendString:[reverse substringWithRange:subStrRange]];
}
NSLog(#"%#", reversedString);
CGSize labelSize = [lbl.text sizeWithFont:lbl.font
constrainedToSize:lbl.frame.size
lineBreakMode:lbl.lineBreakMode];
lbl.frame = CGRectMake(
lbl.frame.origin.x, lbl.frame.origin.y,
lbl.frame.size.width, labelSize.height);
lbl.text=reversedString;
[scrollview addSubview:lbl];
UILabel *lbl=[[UILabel alloc]init];
lbl.frame=CGRectMake(10,10,10,100);
lbl.backgroundColor=[UIColor clearColor];
lbl.font=[UIFont systemFontOfSize:13];
lbl.text=#"123456";
lbl.numberOfLines = 10;
[self.view addSubview:lbl];
You cant do vertical allignment but use another way to display it
UILabel *lbl=[[UILabel alloc]init];
lbl.frame=CGRectMake(10,10,100,400);
lbl.backgroundColor=[UIColor clearColor];
lbl.font=[UIFont systemFontOfSize:13];
lbl.text=#"1\n2\n3\n4\n5\n6";
[scrollVw addSubview:lbl];

iPhone/Objective - C - How to display the whole text of a long string in a UILabel

I have a very long string which is something like that below...
[[sqrt(221)-10,-sqrt(221)-10],[11,11]],[[rootof([[474368400,237184200,-125944810200,258530778000],[1,0,-472,884,42215]])/(sqrt(221)*782707860000)+rootof([[409681800,9099248400,-99876110400,-1875803589000],[1,0,-472,884,42215]])/(sqrt(221)*782707860000)*3,rootof([[-3439170900,-50638826700,864180632700,9670592794500],[1,0,-472,884,42215]])/(sqrt(221)*782707860000)+rootof([[1477010700,22974524100,-369910322100,-4442729593500],[1,0,-472,884,42215]])/(sqrt(221)*782707860000)*3],[rootof([[474368400,237184200,-125944810200,258530778000],[1,0,-472,884,42215]])/(sqrt(221)*782707860000)*2+rootof([[409681800,9099248400,-99876110400,-1875803589000],[1,0,-472,884,42215]])/(sqrt(221)*782707860000)*4,rootof([[-3439170900,-50638826700,864180632700,9670592794500],[1,0,-472,884,42215]])/(sqrt(221)*782707860000)*2+rootof([[1477010700,22974524100,-369910322100,-4442729593500],[1,0,-472,884,42215]])/(sqrt(221)*782707860000)*4]],[sqrt(sqrt(221)+15),sqrt(-sqrt(221)+15)]
How can i display the whole thing in a UILabel with more than one lines (0)?
Also, although there are no linebreaks ("\n") in the string, the result is displayed like below....
[[sqrt(221)-10,-sqrt(221)- 10],[11,11]],[[rootof([[474368400,2 37184200,- 125944810200,258530778000],[1, 0,- 472,884,42215]])/(sqrt(221)*78270
Any help?
PS I could upload an image but i am not allowed beacause i am a new user...
textLabel.lineBreakMode = UILineBreakModeWordWrap;
textLabel.numberOfLines = 0;
set the UILabel as a multiline
MyLbl.numberOfLines = xxx;
if you have not enough space for set the UIlabel then also set the textView As an not editable property:
Mytxt = [[UITextView alloc]initWithFrame:CGRectMake(xx.0, x.0, xx.0,xx.0)];
Mytxt.transform = transform;
Mytxt.adjustsFontSizeToFitWidth = YES;
Mytxt.textColor = [UIColor blackColor];
Mytxt.autoresizingMask=UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[Mytxt setFont:[UIFont fontWithName:#"Arial" size:xx.0]];
Mytxt.backgroundColor = [UIColor clearColor];
Mytxt.delegate = self;
Mytxt.userInteractionEnabled=YES;
Mytxt.editable=NO;
i hope you get the answer.
You can use UITextView with auto size with non editable mode instead of UILabel (or)
CGSize labelSize = yourString sizeWithFont:cellFont
constrainedToSize:CGSizeMake(190.0f, MAXFLOAT); lineBreakMode:UILineBreakModeWordWrap];
This labelsize will dynamically define the label height you can assign to your label
try this code
NSString *yourString = #"[[sqrt(221)-10,-sqrt(221)-10],[11,11]],[[rootof([[474368400,237184200,-125944810200,258530778000],[1,0,-472,884,42215]])/(sqrt(221)*782707860000)+rootof([[409681800,9099248400,-99876110400,-1875803589000],[1,0,-472,884,42215]])";
UILabel *lblNameDesc=[[UILabel alloc] initWithFrame:CGRectMake(55, 10, 250, 21)];
[lblNameDesc setNumberOfLines:0];
lblNameDesc.text=yourString;
[lblNameDesc setBackgroundColor:[UIColor clearColor]];
[lblNameDesc setTextColor:[UIColor colorWithRed:64.0/255.0 green:64.0/255.0 blue:64.0/255.0 alpha:1.0f]];
[self.view addSubview:lblNameDesc];
// [lblNameDesc release];
[lblNameDesc sizeToFit];
supoose your string is. make your fond and width what ever you want:-
NSString longString = #"dsjkghsdkjnfgvsdjgsl,v.........................";
CGSize maximumLabelSize = CGSizeMake(200,MAXFLOAT);
CGSize expectedLabelSize = [longString sizeWithFont:[UIFont fontWithName:#"Aial-BoldMT" size:15.0] constrainedToSize:maximumLabelSize
lineBreakMode:UILineBreakModeWordWrap];
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, 200, expectedLabelSize.height)];
myLabel.lineBreakMode = UILineBreakModeWordWrap;
myLabel.numberOfLines = 0;
myLabel.text = longString ;
may this will help you
Try This...
textLabel.lineBreakMode = YES;
textLabel.numberOfLines = 10; // or many that you need...
You want UILineBreakModeCharacterWrap if you want your text to be evenly distributed along several lines (so it doesn't try to break at work boundaries).

customize section header uilabel background and text

I am currently creating my own custom section headers but I have never dont any text editing via code.. and my new method that I am using to populate my custom header is doing some weird things as shown below
I would like to change the text to white and be slightly bolder and also make the white background transparent..
this is the code I am using to do this
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)];
[headerView setBackgroundColor:[UIColor grayColor]];
// Add the label
UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.5, 20, 20)];
// do whatever headerLabel configuration you want here
headerLabel.text = [self tableView:tableView titleForHeaderInSection:section];
[headerView addSubview:headerLabel];
// Return the headerView
return headerView;
}
I have tried this
[headerLabel.backgroundColor:[UIColor clearColor]];
etc but its not working :(
I would like to change the text to white...
UILabel's textColor property is your friend here.
And slightly bolder...
No problem! headerLabel.font = [UIFont boldSystemFontOfSize:mySize];
And make a white transparent background.
Whoa, whoa, that is the worst setter syntax ive ever seen!!! My lord, myLabel.backgroundColor is a getter, change:
[headerLabel.backgroundColor:[UIColor clearColor]];
to:
[headerLabel setBackgroundColor:[UIColor clearColor]];
Lucky for you, using your syntax would have just sent a message to nil, which is defaulting the background color of your label to white.
Use following code...
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UILabel *headername = [[UILabel alloc]initWithFrame:CGRectMake(20, 5, 270, 34)];
headername.backgroundColor = [UIColor clearColor];
headername.textColor = [UIColor blackColor];
if(section == 0)
{
headername.text = #"Name that u wish";
}
else
{
headername.text = #"Name that u wish";
}
UIView *headerView = [[UIView alloc] init];
UIImageView *tempimage = [[UIImageView alloc]initWithFrame:CGRectMake(10, 5, 300,34)];
tempimage.image = [UIImage imageNamed:#"whitebackground.png"];
[headerView addSubview:tempimage];
[headerView addSubview:headername];
return headerView;
}
Hope, this will help you...chill
all i have done to get the customized header in table is as following and it is working fine for me
UIView *containerView =
[[[UIView alloc]
initWithFrame:CGRectMake(0, 0, 300, 60)]
autorelease];
UILabel *headerLabel =
[[[UILabel alloc]
initWithFrame:CGRectMake(10, 20, 300, 40)]
autorelease];
headerLabel.text = NSLocalizedString(#"Header for the table", #"");
headerLabel.textColor = [UIColor whiteColor];
headerLabel.shadowColor = [UIColor blackColor];
headerLabel.shadowOffset = CGSizeMake(0, 1);
headerLabel.font = [UIFont boldSystemFontOfSize:22];
headerLabel.backgroundColor = [UIColor clearColor];
[containerView addSubview:headerLabel];
self.tableView.tableHeaderView = containerView;
i just put it in viewDidLoad: method

ContainerView hide UILabel

I want to use UILabel in ContainerView.
So I am using this code for that.
UILabel *myLabel = [[[UILabel alloc] initWithFrame:CGRectMake(16, 60, 300, 150)] autorelease];
myLabel.numberOfLines = 0;
myLabel.font = [UIFont systemFontOfSize:13.5];
myLabel.text = [theQuiz objectAtIndex:row+3] ;
myLabel.lineBreakMode = UILineBreakModeWordWrap;
myLabel.backgroundColor = [UIColor clearColor];
myLabel.layer.cornerRadius = 8.0;
[myLabel sizeToFit];
[self.view addSubview:myLabel];
//ContainerView
UIView *ChallengeView = [[UIView alloc] initWithFrame:CGRectMake(8, 55, 300, 10 + Challenge.frame.size.height)];
ChallengeView.layer.borderColor = [[UIColor purpleColor ] CGColor];
[ChallengeView setBackgroundColor:[UIColor whiteColor]];
ChallengeView.layer.cornerRadius = 8 ;
ChallengeView.layer.borderWidth = 1.5;
[self.view addSubview:ChallengeView];
[ChallengeView release];
Now problem is that when i set background color for ContainerView it hides the text of myLabel
Any Solution ??
What is happening is that you containerView is being added above label either you add label after containerView or do this:
[self.view bringSubviewToFront:myLabel];
You add the ChallengeView first , Then add the myLabel.
Otherwise u can do as like #xs2bush said,
[self.view bringSubviewToFront:myLabel];
Bcz the ChallengeView hides the label.

UITextField Black Pixels at Edges

I've created a UITextField programmatically with the following code:
self._maxPriceField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, labelWidth, labelHeight)];
self._maxPriceField.borderStyle = UITextBorderStyleRoundedRect;
self._maxPriceField.clearButtonMode = UITextFieldViewModeWhileEditing;
self._maxPriceField.font = fieldFont;
self._maxPriceField.delegate = self;
The problem I'm having is that my UITextField ends up having these strange black pixels on the edges. This happens both on the device and in the simulator. You can see in the screenshot below:
When I create the same UITextField using IB, with the same specs and background, I have no problem. Unfortunately I need to create this UITextField programmatically.
Has anybody seen this before? What to do?
It seems this is the way textfields are drawn on the screen. I played around with your code a little bit, and if I set the text field height lower than about 20, you start to see a noticeable "shadow" that is clearly not drawn correctly.
My suggestion is to either use a height of 20 or higher for the text field, or to use a different style, such as Bezel or Line and set the background to white.
Here is a screenshot to demonstrate:
And here is the code I used to draw these:
int labelWidth = 100;
int labelHeight = 10;
_maxPriceField = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, labelWidth, labelHeight)];
_maxPriceField.borderStyle = UITextBorderStyleRoundedRect;
_maxPriceField.clearButtonMode = UITextFieldViewModeWhileEditing;
//_maxPriceField.font = fieldFont;
//_maxPriceField.delegate = self;
[self.view addSubview:_maxPriceField];
UITextField *secondField = [[UITextField alloc] initWithFrame:CGRectMake(10, 40, labelWidth, labelHeight + 10)];
secondField.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:secondField];
[secondField release];
UITextField *thirdField = [[UITextField alloc] initWithFrame:CGRectMake(10, 70, labelWidth, labelHeight + 20)];
thirdField.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:thirdField];
[thirdField release];
UITextField *fourthField = [[UITextField alloc] initWithFrame:CGRectMake(10, 110, labelWidth, labelHeight + 30)];
fourthField.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:fourthField];
[fourthField release];
UITextField *noRoundFirst = [[UITextField alloc] initWithFrame:CGRectMake(10, 160, labelWidth, labelHeight)];
noRoundFirst.borderStyle = UITextBorderStyleBezel;
noRoundFirst.backgroundColor = [UIColor whiteColor];
[self.view addSubview:noRoundFirst];
[noRoundFirst release];
Hope this helps.
Mk