i want to append two label texts(which are in different colors) in One label.any help please?
how can i do it?
you can use one UILabel with two other UILabels with their own setups as a subviews. smth like this
UILabel* mainLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 120, 40)];
UILabel* firstSublabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 60, 40)];
[firstSublabel setText:#"asd"];
[firstSublabel setTextColor:[UIColor redColor]];
[mainLabel addSubview:firstSublabel];
[firstSublabel release];
UILabel* secondSublabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 60, 60, 40)];
[secondSublabel setText:#"dfg"];
[secondSublabel setTextColor:[UIColor greenColor]];
[mainLabel addSubview:secondSublabel];
[secondSublabel release];
[self.view addSubview:mainLabel];
[mainLabel release];
Not really in a nice way.
There are some ideas for replacements in this question: Why is there no NSAttributedString on the iPhone?
Related
can any body help me how to add two buttons to the navigation title view by using the title view property. I tried but, able to add only one button by using the following code.
UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
customView.backgroundColor = [UIColor redColor];
UIButton *b1 = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
UIButton *b2 = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
[b1 setTitle:#"Hai" forState:UIControlStateNormal];
[b2 setTitle:#"Hello" forState:UIControlStateNormal];
[customView insertSubview:b1 atIndex:0];
[customView insertSubview:b2 atIndex:1];
self.navigationItem.titleView = customView;
use this code:
UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
customView.backgroundColor = [UIColor redColor];
UIButton *b1 = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
UIButton *b2 = [[UIButton alloc] initWithFrame:CGRectMake(50, 0, 50, 50)];
[b1 setTitle:#"Hai" forState:UIControlStateNormal];
[b2 setTitle:#"Hello" forState:UIControlStateNormal];
[customView insertSubview:b1 atIndex:0];
[customView insertSubview:b2 atIndex:1];
self.navigationItem.titleView = customView;
Outputs:
It's because you have added the same frame for both Buttons. So, the Buttons are actually overlapped. You should use this code instead :
UIButton *b1=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
UIButton *b2=[[UIButton alloc]initWithFrame:CGRectMake(60, 0, 50, 50)];
Then you can simply add these buttons to your custom view.
[cusotmView addSubView:b1];
[cusotmView addSubView:b2];
Reason for Error :
You can't directly add UIButtons. You need to wrap them as UIBarButtonItems first.
Sample Code :
UIBarButtonItem *btn1 = [[UIBarButtonItem alloc] initWithCustomView:b1];
UIBarButtonItem *btn2 = [[UIBarButtonItem alloc] initWithCustomView:b2];
[cusotmView addSubView:btn1];
[cusotmView addSubView:btn2];
I am developing one application for iphone and in that i have some issues regarding adding more then one UILabel in navigation bar at a specific position.
What i want is in following images
in above image there is one backbar button and one imageview as shown with arrow-mark. Other then that all white box is for difrent UILabels to show in navigation bar.
There is only one UIImageView in the navigation bar and one left bar button. Now problem is that i don't know how to add multiple UILabel in navigation bar at a specific position.
Till now what i have worked is to add only button and UIImageView with the right bar button array or left bar button array, but that only add items in sequnce.
So can anyone please guide me that how can anyone add UILabels or any other item at a specific position..
you can add Multiple UIlabel or Image as look like bellow image:-
this can do using bellow code you can change Label and imageView frame and put as your requirement
- (void)viewDidLoad
{
UIView *btn = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 60)];
UILabel *label;
label = [[UILabel alloc] initWithFrame:CGRectMake(5, 25, 200, 16)];
label.tag = 1;
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:16];
label.adjustsFontSizeToFitWidth = NO;
label.textAlignment = UITextAlignmentLeft;
label.textColor = [UIColor whiteColor];
label.text = #"My first lable";
label.highlightedTextColor = [UIColor whiteColor];
[btn addSubview:label];
[label release];
label = [[UILabel alloc] initWithFrame:CGRectMake(0, 30, 200, 16)];
label.tag = 2;
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:16];
label.adjustsFontSizeToFitWidth = NO;
label.textAlignment = UITextAlignmentRight;
label.textColor = [UIColor whiteColor];
label.text = #"second line";
label.highlightedTextColor = [UIColor whiteColor];
[btn addSubview:label];
[label release];
UIImageView *imgviw=[[UIImageView alloc]initWithFrame:CGRectMake(170, 10, 20, 20)];
imgviw.backgroundColor = [UIColor blackColor];
imgviw.image=[UIImage imageNamed:#"a59117c2eb511d911cbf62cf97a34d56.png"];
[btn addSubview:imgviw];
self.navigationItem.titleView = btn;
}
You can add subviews directly to the navigationBar -
UINavigationBar *navBar = navController.navigationBar;
[navBar addSubview: yourLabel];
yourLabel frame origin will be relative to the navigation bar
Try this code
UIView *buttonContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
buttonContainer.backgroundColor = [UIColor clearColor];
UIButton *button0 = [UIButton buttonWithType:UIButtonTypeCustom];
[button0 setFrame: CGRectMake(160, 7 , 40, 30)];
[button0 setTitle:#"Sort" forState:UIControlStateNormal];
button0.titleLabel.font = [UIFont fontWithName:#"Helvetica" size:12];
[button0 setBackgroundImage:[UIImage imageNamed:#"Btn_Sort_Btn_Sort_Places.png"] forState:UIControlStateNormal];
[button0 addTarget:self action:#selector(sortAction) forControlEvents:UIControlEventTouchUpInside];
[button0 setShowsTouchWhenHighlighted:YES];
[buttonContainer addSubview:button0];
self.navigationItem.titleView = buttonContainer;
UILabel *lbl_title = [[UILabel alloc] initWithFrame:CGRectMake(10, 0 , 140, 40)];
lbl_title.text = str_HeaderTitle;
lbl_title.textColor = [UIColor whiteColor];
lbl_title.font = [UIFont fontWithName:#"Helvetica-Bold" size:16];
lbl_title.backgroundColor = [UIColor clearColor];
lbl_title.textAlignment = NSTextAlignmentCenter;
[buttonContainer addSubview:lbl_title];
You can set an arbitrary view instead of a view controller's title:
myViewController.navigationItem.titleView = someView;
Note that most likely the view's frame will be restricted to make room for leftBarButtonItem and rightBarButtonItem.
UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(50, 2, 20, 15)];
[label setBackgroundColor:[UIColor greenColor]];
UIImageView *imgView=[[UIImageView alloc] initWithFrame:CGRectMake(40, 20, 150, 10)];
[imgView setBackgroundColor:[UIColor redColor]];
[[self.navigationController navigationBar] addSubview:label];
[self.navigationController.navigationBar addSubview:imgView];
Have a look at this piece of code. With some modifications it should solve your problem:
UIButton *imageButton = [UIButton buttonWithType:UIButtonTypeCustom];
[imageButton setFrame:CGRectMake(15, 0, 57, 44)];
[imageButton setBackgroundImage:[UIImage imageNamed:#"someImage.png"] forState:UIControlStateNormal];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(140, 0, 250, 44)];
[titleLabel setText:#"some title"];
[titleLabel setBackgroundColor:[UIColor clearColor]];
[titleLabel setTextColor:[UIColor whiteColor]];
[titleLabel setFont:[UIFont boldSystemFontOfSize:20]];
[self.navigationController.navigationBar addSubview:imageButton];
[self.navigationController.navigationBar addSubview:titleLabel];
Just modify the values in the CGRectMake() to fit your needs.
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 made a Stock Tiker to display continuous stock objects. And Working fine for first Instance.
The implementation of the ticket code is as follows:
- (void)viewDidLoad
{
tickerView=[[StockTiker alloc] init];
[tickerView setFrame:CGRectMake(0, 0, 320, 20)];
tickerView.delegate=self;
UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 150, 20)];
[label setBackgroundColor:[UIColor redColor]];
label.text=#"First Object";
UILabel *label2=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 20)];
[label2 setBackgroundColor:[UIColor grayColor]];
label2.text=#"Second Object";
UILabel *label3=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 20)];
[label3 setBackgroundColor:[UIColor magentaColor]];
label3.text=#"Third Object";
UILabel *label4=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 75, 20)];
[label4 setBackgroundColor:[UIColor yellowColor]];
label4.text=#"Fourth Object";
UILabel *label5=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 75, 20)];
[label5 setBackgroundColor:[UIColor cyanColor]];
label5.text=#"Fifth Object";
viewArray=[[NSArray alloc] initWithObjects:label,label2,label3,label4,label5,nil];
[self.view addSubview:tickerView];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
#pragma mark- Ticker Delegate..
-(UIView*)viewForRow:(int)row inTicker:(StockTiker*)stock_Ticker
{
return [viewArray objectAtIndex:row];
}
-(int)numberOfRowsForStockTiker:(StockTiker*)stock_Ticker
{
return [viewArray count];
}
//Output is fine
But when I made second Instance of Ticker class it overlapped to each other.
Output with two instance managed using ticker.tag
Any Idea?
How can I solve this error.
Thanks in Advance!
Hey I have uploaded an example please check it Horizontal List
Added objectArray2.
Duplicated BSE_sticker method
in second_Sticker did all the same code of BSE_Sticker except add it to objectArray2.
Return objectARray2 for stkticker2 in delegate based on tag
Project here
Edit
I looked into issue and solved repeating instance/ wobbling
Your ticker class uses static int count.
Static variables are instantiated only once per class. and therefore
you coding regarding counter leads the object to check for instance
multiple times.
you should change the static variable to a normal ivar and instantiate it to 0 in start method.
Then it will work fine
I am trying to add label (headerLabel) to another subview (introPadTutorial) here and it is not showing up on the screen. Can some please help me where am I making mistake?
//Header file
#property (nonatomic, retain) UIView *introPadTutorial;
//Implementation file
#synthesize introPadTutorial;
UIView *v = [_appDelegate getCurrentView];
CGRect tutorialFrame = [self getTableViewFrame];
introPadTutorial = [[UIView alloc] initWithFrame:CGRectMake(v.frame.size.width, 80, -tutorialFrame.size.width, v.frame.size.height)];
[introPadTutorial setBackgroundColor:[UIColor uberLightGray]];
[introPadTutorial setUserInteractionEnabled:YES];
UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 180, 180)];
[headerLabel setText:#"test test test"];
[headerLabel setBackgroundColor:[UIColor greenColor]];
[headerLabel setFont:[UIFont boldSystemFontOfSize:16.0f]];
[headerLabel setTextColor:[UIColor redColor]];
[headerLabel setShadowColor:[UIColor whiteColor]];
[headerLabel setShadowOffset:CGSizeMake(0, 1)];
[self.introPadTutorial addSubview:headerLabel];
[headerLabel release];
[v addSubview:introPadTutorial];
[introPadTutorial release];
Don't use a negative width to change the POSITION, use the origin to change the position.
Use this:
introPadTutorial = [[UIView alloc] initWithFrame:CGRectMake(v.frame.size.width - tutorialFrame.size.width, 80, tutorialFrame.size.width, v.frame.size.height)];
I think that should help make the subview actually show up.
May be your...
introPadTutorial = [[UIView alloc] initWithFrame:CGRectMake(v.frame.size.width, 80, -tutorialFrame.size.width, v.frame.size.height)];
should actually be...
introPadTutorial = [[UIView alloc] initWithFrame:CGRectMake(0, 80, tutorialFrame.size.width, v.frame.size.height)];
Is your introPadTutorial view showing up? You have a negative width in it's initWithFrame call.