I have a UILabel coded programmatically. I want to change the size of the label when i pressed a button. how to change the size of that label? this is my code
UILabel *theLabel11 = [[UILabel alloc] initWithFrame:CGRectMake(0,0,100,30)];
[theLabel11 setText:#"US"];
[theLabel11 setTextAlignment:UITextAlignmentCenter];
[theLabel11 setFont: [UIFont fontWithName:#"Arial" size:13.0f]];
[theLabel11 setBackgroundColor:[UIColor orangeColor]];
[theLabel11 setTextColor:[UIColor blackColor]];
[scroll1 addSubview:theLabel11];
You should declare your label as class property, so it can be accessed from other methods
To change the font size use
[theLabel11 setFont: [UIFont fontWithName:#"Arial" size:13.0f]];
To change the frame size of the label us
theLabel11.frame = CGRectMake(x, y, width, height);
A common idiom for adjusting the spatial information on a UIView is as below
label.frame = CGRectMake(
x,
y,
width,
height
);
You can get the old position and height via
label.frame.origin.x
label.frame.origin.y
label.frame.size.width
label.frame.size.height
If there is only one label added to scroll1 then, iterate the scrollview to get the label reference as follows in button action
for(UIView *subView in scroll1.subViews){
if([subView isKindOfClass:[UILabel class]]){
UILabel *lbl=(UILabel*)subView;
//change size of label here
}
}
if there are many labels assign a tag to each label while creating and check that in for loop
Related
I just want to highlight only text in UILabel, I have tried by giving backgroundColor for label, but it is highlighting the empty spaces also looks not good. So Is there any way to highlight text without resizing UILabel.
Please check the image, here labels are bigger than the text (center aligned)
Thanx.
Most of the other solutions don't consider text that spans multiple lines while still only highlighting the text, and they are all pretty hacky involving extra subviews.
An iOS 6 and later solution is to use attributed strings:
NSMutableAttributedString *s =
[[NSMutableAttributedString alloc] initWithString:yourString];
[s addAttribute:NSBackgroundColorAttributeName
value:[UIColor greenColor]
range:NSMakeRange(0, s.length)];
label.attributedText = s;
This will add a subview behind the text, with the correct size:
CGSize size= [[label text] sizeWithFont:[UIFont systemFontOfSize:18.0]];
NSLog(#"%.1f | %.1f", size.width, size.height);
NSLog(#"%.1f | %.1f", label.frame.size.width, label.frame.size.height);
UIView *highlightView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)];
[highlightView setBackgroundColor:[UIColor greenColor]];
[self.view insertSubview:highlightView belowSubview:label];
[highlightView setCenter:label.center];
And don't forget: [label setBackgroundColor:[UIColor clearColor]];
try this
MTLabel
MTLabel *label4 = [MTLabel labelWithFrame:CGRectMake(20, 270, 250, 60)
andText:#"This label is highlighted, has a custom line height, and adjusts font size to fit inside the label."];
[label4 setLineHeight:22];
[label4 setFont:[UIFont fontWithName:#"Helvetica" size:30]];
[label4 setAdjustSizeToFit:YES];
[label4 setMinimumFontSize:15];
[label4 setFontHighlightColor:[[UIColor orangeColor] colorWithAlphaComponent:0.5]];
[self.view addSubview:label4];
You can have a UIImageView, set its background color of your choice then place your UIlabel on it.
That will surely solve your problem.
Here is Workaround code :
UIImageView * imgView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 100, 40)];
[imgView setBackgroundColor:[UIColor brownColor]];
[self.view addSubview:imgView];
UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 40)];
[label setBackgroundColor:[UIColor clearColor]];
[label setTextAlignment:UITextAlignmentCenter];
label.text = #"My Label";
[imgView addSubview:label];
[label release];
[imgView release];
You can also achieve the same using Interface Builder.
I have a circle that turns over, how can i put on him some labels that will turn with him also?
my point is that the circle and labels should be on same angel.
Make the label a subview of the UIView you are using to present the circle.
In IB just drag the label onto the the UIView you are using for the circle image or if you are not using IB just add the label to the circle like this:
[circleUIView addSubview:theUILabel];
Download demo source code this url http://www.devedup.com/index.php/2010/03/03/iphone-animate-an-object-along-a-path/
Now changes into Canvas.m file's animateCicleAlongPath method.
Replace to image to label.
UILabel *label = [[UILabel alloc]init];
[label setText:#"test"];
[label setTextColor:[UIColor redColor]];
//Set the frame of the view - which is used to position it when we add it to our current UIView
label.frame = CGRectMake(1, 1, 20, 20);
label.backgroundColor = [UIColor clearColor];
[self addSubview:label];
[label.layer addAnimation:pathAnimation forKey:#"moveTheSquare"];
I have a need to display text that includes HTML tags etc and TTStyledTextLabel fits the bill.....but it does not scroll.
I placed one inside a UITextView but this refuses to scroll? If I enter the text directly in the UITextView it scrolls OK but then I see all the HTML unformatted.
Is there a way to set TTStyledTextLabel to scroll?
Thanks
Try putting the TTStyledTextLabel in a UIScrollView.
Alternately, you can consider using a UIWebView directly.
I finally got a suitable work around...
CGSize constraintSize;
CGSize stringSize;
// make an overly big size allowance
constraintSize.width = 300;
constraintSize.height = 2000;
NSString *s = #"this can be text as long or as short as required...;
UIFont *f = [UIFont fontWithName:#"Arial" size:14];
stringSize =[s sizeWithFont:f constrainedToSize: constraintSize lineBreakMode: UILineBreakModeWordWrap];
// create a label to accomodate the text
UILabel *l = [[UILabel alloc] initWithFrame:CGRectMake(14, 2, stringSize.width, stringSize.height)];
l.text = s;
[l setNumberOfLines:0];
[l sizeToFit];
// now create a TTStyledTextLabel to match the size we just obtained above
TTStyledTextLabel *tl = [[TTStyledTextLabel alloc] initWithFrame:[l frame]];
// set the text making use of links etc
tl.text = [TTStyledText textFromXHTML:l.text lineBreaks:YES URLs:YES];
[tl setBackgroundColor:[UIColor clearColor]];
tl.textColor = [UIColor whiteColor];
UIScrollView *sv = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 185, 320, 300)];
// adjust scrollview content size to accomodate the TTStyledTextLabel
[sv setContentSize:CGSizeMake(tl.frame.size.width, tl.frame.size.height)];
[sv addSubview:tl];
[self.view addSubview:sv];
Now I can have an auto sizing TTStyledTextLabel that scrolls ;-)
Can I know how can we insert more than one line of data into a single cell of a UITableView,
Just Like, I want the output in a single cell to be like :
KPK
Developer
Infosystems
INDIA
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 80)];
myLabel.text = #"KPK";
myLabel.textAlignment = UITextAlignmentCenter;
myLabel.textColor = [UIColor yellowColor];
[cell addSubview:myLabel];
UILabel *myLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 201, 200, 80)];
myLabel1.text = #"Developer";
myLabel1.textAlignment = UITextAlignmentCenter;
myLabel1.textColor = [UIColor yellowColor];
[cell addSubview:myLabel1];
Use \n as you are using in your string.
Set numberOfLines to 4 to allow for any number of lines.
label.numberOfLines = 4;
Update the label frame to match the size of the text using sizeWithFont:. If you don't do this your text will be vertically centered or cut off.
UILabel *label; // set frame to largest size you want
...
CGSize labelSize = [label.text sizeWithFont:label.font
constrainedToSize:label.frame.size
lineBreakMode:label.lineBreakMode];
label.frame = CGRectMake(
label.origin.x, label.orgin.y,
label.size.width, labelSize.height);
label.text=[NSString stringWithFormat:#"KPK\nDeveloper\nInfosystems\nIndia"];
Now you just need to add the label in your cell.
[cell addSubView:label];
you can place \n in text.Not sure if that works else you can make custom cell for multiple line records.
set tablerow height and Use label in your tableview
Create a custom cell, add 4 labels in it, to display all of your required info. Checkout the following tutorial to see how you can make custom cell.
http://adeem.me/blog/2009/05/30/iphone-sdk-tutorial-part-6-creating-custom-uitableviewcell-using-interface-builder-uitableview/
Use a uilabel and add that one as the subview if your tableview cell.you can specify the number of lines of uilabel
label.numberOfLines = 4;
I think that the best way is creating a custom UITableViewCell implementation, or programmatically adding UILabels to the cell's contentView.
I.e.:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 200, 30)];
label.text = #"MyText";
label.textColor = [UIColor blackColor];
[cell.contentView addSubview:label];
[label release]; // Never forget this!
When adding the additional rows you should update the first line definition to correctly offset the new rows. Eg. the second label should have something like this:
UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(5, 35, 200, 30)];
When you are done, you should implement the tableView:heightForRowAtIndexPath: method in the table's delegate, because you have to define the correct height for the new cell. In your example, for 4 labels you should implement something like this:
- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath)indexPath {
if (is_one_of_my_modified_cells) {
return 35*4 + 5;
}
return 44; // default height
}
Try it and let me know.
OK, I'm struggling to make this work but without success.
Basically I want to add a UILabel to an UIView and center it.
The code looks like this:
UIView *customTitleView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 44.0)];
[customTitleView setBackgroundColor:[UIColor clearColor]];
// Screen title
CGSize constraint = CGSizeMake(200.0, 44.0f);
CGSize size = [screenTitle sizeWithFont:[UIFont boldSystemFontOfSize:20.0]
constrainedToSize:constraint
lineBreakMode:UILineBreakModeCharacterWrap];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, size.width, size.height)];
[titleLabel setText:screenTitle];
[titleLabel setTextColor:[UIColor whiteColor]];
[titleLabel setFont:[UIFont boldSystemFontOfSize:20.0]];
[titleLabel setBackgroundColor:[UIColor clearColor]];
titleLabel.center = customTitleView.center;
[customTitleView addSubview:titleLabel];
[titleLabel release];
self.navigationItem.titleView = customTitleView;
[customTitleView release];
I expected that the UILabel would be centered within the UIView.
Well, it isn't. It is somehow right aligned and not even close to the center of the UIView.
What am I doing wrong?
So here's what's going wrong:
self.navigationItem.titleView = customTitleView;
When you set customTitleView as the title view of the navigation item the frame of customTitleView will change. customTitleView won't be 320 pixels wide anymore, but the frame of titleLabel will remain the same. Thus it will no longer be centered.
Can't you just set titleLabel as titleView? I think it should be centered automatically. If not, I know a more complicated way to solve it. Drop a comment and I'll tell you how.
EDIT: So here is how you realign the titleLabel after customTitleView's frame has changed.
When the frame of a view is changed layoutSubviews is called on that view. So instead of letting customTitleView be a regular UIView you need to make it a custom view that inherits from UIView. In that custom view you override layoutSubviews and in your implementation of layoutSubviews you make sure everything is aligned the way you want based on the new frame (self.frame). I'll leave the implementation to you.