I want to add UILabel controls to my application dynamically from code (programmatically), please help me
You can do it the following way in one of the view controller methods:
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(...)]; // Create UILabel
myLabel.tag = someTag; // set label's tag to access this label in future via [view viewWithTag:someTag];
... // set label's properties like text, background and text color, font size etc (e.g. myLabel.textColor = [UIColor redColor];)
[view addSubView:myLabel]; // add label to your view
[myLabel release]; // view owns the label now so we can release it
Related
I am developing an iPhone application that allows user to draw emoticons and use it while comments with text (like iPhone allows user to use their emoji by enabling emoji keyboard from the settings). I want to use my own made emoticons. I store all emoticons in collection view. How can I enable that view from iPhone default keyboard, so that I can use my own custom emoticons with text?
When using a custom keyboard in your app, just use the inputView attribute of a UITextField.
You can do this in the following fashion:
#interface SCAViewController ()
#property (weak, nonatomic) IBOutlet UITextField *textField;
#property ( nonatomic) UIView *labelView;
#end
#implementation SCAViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// here we set the keyboard to become the first responder
// this is optional...
//[_textField becomeFirstResponder];
// creating the custom label keyboard
_labelView = [[UIView alloc] initWithFrame:CGRectMake(0, 568, 320, 568/3)];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 100)];
label.text = #"Label View!";
[_labelView addSubview:label];
_labelView.backgroundColor = [UIColor blueColor];
_textField.inputView = _labelView;
}
Now for the explanation:
The reason that the _labelView is set to those current dimensions is because i wanted a close match to the default keyboard. You can always change them to whatever you're needing.
I did a label being added onto the view, but since you're doing emojis, i'd create a method that will load the image into the textField by using buttons.
It would be list like the following:
create action method for button
create button
set background image of button
add button onto custom view
set inputView to the custom view
you can create the action method like so
// create the method for the button
- (IBAction) loadOntoKeyboard {
// load the image/text/whatever else into the textfield
}
you can create a button to add onto your custom view like
// creating custom button
UIButton *emoji1 = [UIButton buttonWithType:UIButtonTypeCustom];
// creating frame for button.
// x - the location of the button in x coordinate plane
// y - same as x, but on y plane
// width - width of button
// height - height of button
emoji1.frame = CGRectMake(x, y, width, height);
// just setting background image here
[emoji1 setBackgroundImage:[UIImage imageNamed:#"emoji_imageā¦"] forState:UIControlStateNormal];
// don't forget to add target of button
[emoji1 addTarget:self action:#selector(loadOntoKeyboard) forControlEvents:UIControlEventTouchUpInside];
// adding button onto view
[customView addSubview:emoji1];
// setting the inputView to the custom view where you added the buttons
textFieldVariable.inputView = customView;
Hopefully this all makes sense to you, and I hope that I helped you understand in a clear manner on how to implement the custom keyboard actions :)
Hope this helps!
How to achieve picker view like this? I have implemented all necessary delegate and dataSource methods to populate the data, but the thing I am not able to understand is how to add this titles adults, children and infants?
They are static and does not spin with the component!
Add the 3 labels to your view as subviews when you showing the picker view and then hiding them when the picker is dismissed.
You will have to position the labels on the band.
I got this done just using Interface Builder.
I created a container view and then put picker view inside it.
To be sure that my container size is the same as picker view I set space constraints: leading, trailing, top and bottom.
Then I put 3 labels above picker view (but they're still the subviews of the container) and set their frames to center it.
Also to achieve the same label visual effect as on the screenshot (it seems to be under selection bar) decrease label's alpha to about 0.7.
You could put the labels on a particular frame position and then make the labels background color as clearColor.
You need to add labels as subviews of the picker view. There is no functionality built into UIPickerView to make this easy.
Create your picker, create a label with a shadow, and push it to a picker's subview below the selectionIndicator view.
It would look something like this
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(135, 93, 80, 30)] autorelease];
label.text = #"Label";
label.font = [UIFont boldSystemFontOfSize:20];
label.backgroundColor = [UIColor clearColor];
label.shadowColor = [UIColor whiteColor];
label.shadowOffset = CGSizeMake (0,1);
[picker insertSubview:label aboveSubview:[picker.subviews objectAtIndex:5]];
//When you have multiple components (sections)...
//you will need to find which subview you need to actually get under
//so experiment with that 'objectAtIndex:5'
//
//you can do something like the following to find the view to get on top of
// define #class UIPickerTable;
// NSMutableArray *tables = [[NSMutableArray alloc] init];
// for (id i in picker.subviews) if([i isKindOfClass:[UIPickerTable class]]) [tables addObject:i];
// etc...
What i am trying to do is i read some content from a web service, each content has content type (text,image,button...).
I have to display dynamically the content in the UIView. So what i am trying to do is to create an IBoutlet UIView and check if the content type is a text i convert this uiview to UILable and display the text, if the content type is an image i convert the UIView to a UIImageView etc...
I need to work on the same UIView from the xib file, so how can i change the properties of the UIView to a UILable or a UIImageView.
Thank you
You cannot "change" an UIView to be something different.
I suggest you dynamically add subviews to your view:
UIView *newView = nil;
switch (content type) {
case 0: // text
newView = [[UILabel alloc] init];
((UILabel*)newView).text = #"someText";
[newView sizeToFit];
case 1: // button
{
UIButton *btn = [UIButton buttonWithType:...];
[btn setTititle:#"Ttile" forControlState:UIControlStateNormal]; // assign all needed properties
...
newView = btn;
}
case 2:
....
}
newView.frame = ... // calculate Frame using previously created content (e.g. remember y offset and add bounds.size.height after every iteration)
[self.view addSubView:newView];
....
I am an iOS development newbie. I have a settings screen which is a UITableView. I want to add some explanation to it. I am using the following code to do it, but it skews up the text completely. Any idea what I am doing wrong?
UILabel *subjectLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0, 300, 175)];
subjectLabel.font = [UIFont systemFontOfSize:16.0];
subjectLabel.numberOfLines = 0;
subjectLabel.font = [UIFont fontWithName:#"Arial Rounded MT Bold" size:(10.0)];
subjectLabel.backgroundColor = [UIColor clearColor];
//bodyLabel.textAlignment = UITextAlignmentLeft;
subjectLabel.text = #"mytext";
settingTableView = [[[UITableView alloc] initWithFrame:CGRectMake(0,0, 320, 370) style:UITableViewStyleGrouped] autorelease];
settingTableView.dataSource = self;
settingTableView.delegate = self;
[settingTableView addSubview:subjectLabel];
[self.view addSubview:settingTableView];
A tableViewHeader is a UIView which is set as the tableViewHeader property of a tableView. If you want to have a UILabel in a header view, make a separate UIView (either in code, or in a nib), and set it as the tableView.tableHeaderView property. More information can be found here: TableView Reference. Hope that helps!
create a view in your view controller and add your lable to that and bind it ...
IBOutlet UIView *headerView1;
and add this code
settingTableView.tableHeaderView = headerView1;
Suggestion1 : You could have create a separate view which contains your UILabel and place above the UITableView and place your tableView y position would be from the height of the UIView.
Remark : This is useful because when you scroll the tableView the default header will be stick to top.
Suggestion2 : you can use viewForHeaderInSection delegate method. where you can create a view and add the UILabel. viewForHeaderInSection returns the UIView, which you can return your view which contains the UILabel
Remark : when you scroll the tableView the default header will move along with your tableView
How can I set custom font in UINavigationBar ? I need the tahoma font.
- (void)viewDidLoad{
self.title =#"My text";
}
Totally possible, if a little tricky to do. Once you've found the font you need (either one of the alternatives already shipped with iOS, or a TTF file that you've got the correct licensing for), just create a UILabel with the size, formatting, font etc and then add it to the navigation items for that bar (or, if you're doing this in a view controller, set the .navigationItem.titleView of that controller to your label).
For example, I have a view controller inside a UINavigationController. To change the label in the top to a custom font, I simply do:
//...I have already setup a UILabel called navLabel that has the same style as a
// default navigation bar title, but I've changed the .font property to my custom font
self.navigationItem.titleView = navLabel;
[navLabel release];
This code should work. In uiviewcontroller which presents your main ui:
- (void)viewDidLoad
{
[super viewDidLoad];
int height = navigationController.navigationBar.frame.size.height;
int width = navigationController.navigationBar.frame.size.width;
UILabel *navLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, height)];
navLabel.backgroundColor = [UIColor clearColor];
navLabel.textColor = [UIColor whiteColor];
navLabel.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
navLabel.font = [UIFont boldSystemFontOfSize:15];
navLabel.textAlignment = UITextAlignmentCenter;
self.navigationItem.titleView = navLabel;
[navLabel release];
}
Note that resulting custom view has transparent background, so that you can add something more to your navigation bar with [navigationController.navigationBar addSubview:view]. This may be spinner in left corner of the bar or something else.
If you use custom view, you will not be able set the title with uiviewcontroller title anymore. You need to use the way available by your custom view.
Example:
((UILabel *)self.navigationItem.titleView).text = title;