I wan to design a settings page, in which I have to draw lines between multiple labels, what is the best way to do this, I have googled around, got to know about CGContextRef approach. Is this the proper way, I need to have a line between labels (consecutively). Can I go ahead with this approach or any other best way is there.
I have given base view as dark color and I am adding labels as white, I am giving a line gap between two labels its looking like a line. No extra work :)
May be just add UIView between labels? Something Like this:
UILabel *topLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 20)];
//Label settings
[self addSubview:topLabel];
[topLabel release];
UIView *separator = [[UIView alloc] initWithFrame:CGRectMake(topLabel.frame.origin.x, CGRectGetMaxY(topLabel.frame), topLabel.frame.size.width, 2)];
separator.backgroundColor = [UIColor blackColor];
UILabel *bottomLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(separator.frame), 320, 20)];
//Label settings
[self addSubview:bottomLabel];
[bottomLabel release];
Couldn't you just create a custom UIView class with a UILabel and a UIView as the subviews of this custom view class?
class CustomView : UIView
{
UIView *line;
UILabel *label;
}
#property(nonatomic, retain) UIView *line;
#property(nonatomic, retain) UILabel *label;
For the UIView subview, you can tell it to use the width of the parent UIView.
Related
I have a TableViewController which should have a "floating" header at the top of the table view which should not scroll with the content.
Is there a way to achieve this using a UITableViewController (I know this could be done with nested a Tableview inside a plain UIViewController but I would prefer to have a UITableViewController as the main ViewController so I can use the benefits of Storyboard TableView-Goodies.
Make a nib with a UITableViewController or a subclass of it and simply rearrange the table view to make room for the header view. A table view controller with a slightly different layout and more views is still a table view controller.
It might be considered a hack, but this seems to do the job! (tested with the default Master-Detail project template)
First change the type of the MasterViewController to a UIViewController and add the protocol references for the delegate and datasource. Also add an IBOutlet for a UITableView.
#interface MasterViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
#property (weak, nonatomic) IBOutlet UITableView *tableView;
#end
Next, wire up that IBOutlet to the UITableView in the Storyboard editor.
Now add the following code to viewDidLoad:
UIView *mainView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
UILabel *header = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
header.text = #"HEADER";
header.backgroundColor = [UIColor greenColor];
[mainView addSubview:header];
self.tableView.frame = CGRectMake(0, 40, 320, 440);
[mainView addSubview:self.tableView];
self.view = mainView;
Is there a way where i could add a UILabel on top of a UIImageView programmatically ?
So finally there should be a label displayed on top of a Image.
UIImageView is a subclass of UIView so you can add any UI object onto it.
UILabel *xlabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 20)];
xlabel.text = #"xxx";
[myImageView addSubview:xLabel];
As simple as
[imageView addSubview:yourLabel];
I'm trying to create my own UIView subclass. I've placed it on my view in Interface Builder by dragging out a UIView, then specifying my subclass's name in the Class Identity field. Yet my UIView does not draw itself.
Here--in a simplified example--is the code in my UIView subclass:
- (id)initWithCoder:(NSCoder *)aDecoder
{
if (self = [super initWithCoder:aDecoder])
{
UILabel* label = [[[UILabel alloc] initWithFrame:self.frame] autorelease];
label.text = #"Hello.";
label.textColor = [UIColor whiteColor];
[self addSubview:label];
}
return self;
}
I've seen reference to overriding drawRect:, but honestly I have no idea what I'd do in that method. I'm sure I'm doing something obviously wrong, but I have no idea what.
Any suggestions would be greatly appreciated!
Thanks.
Try using self.bounds instead of self.frame:
UILabel* label = [[[UILabel alloc] initWithFrame:self.bounds] autorelease];
Your view's frame is probably not at origin {0 0}, which means that the label would end up outside your view's visible area.
Is it possible to allow user input text on a UIImageView, just like the text tool in painter?
I cannot find any resource on this topic?
UIImageView is not designed to hold any text, but you could add a UILabel or UITextField either within it or on top / below it, depending on what you want to do.
For example, suppose you want to allow the user to edit a piece of text inside an image. You could do something like this:
UIImage* image = [UIImage imageNamed:#"my_image.png"];
UIImageView* imageView = [[UIImageView alloc] initWithImage:image];
imageView.userInteractionEnabled = YES;
UITextField* textField = [[UITextField alloc]
initWithFrame:CGRectMake(10, 10, 50, 20)];
textField.placeholder = #"type here";
[imageView addSubview:textField];
// You might also want to set the imageView's frame.
[self.view addSubview:imageView];
If you add a UITextField as a subview of a UIImageView, it's important to set the userInteractionEnabled to YES, since it defaults to NO for that superview (it's usually YES by default in most UIViews).
Addendum
If you want the user to be able to click anywhere in the image to edit the text, here is one way to do it: subclass UIControl and add a UIImageView and a UITextField as subviews of it, and connect the clicking action of the UIControl to the UITextField. Something like this (WARNING: not tested code, but it conveys the general idea):
#interface ImageAndTextView : UIControl {
UIImageView* imageView;
UITextField* textField;
}
#property (nonatomic, retain) UIImageView* imageView;
#property (nonatomic, retain) UITextField* textField;
- (void) click;
#end
#implementation ImageAndTextView
#synthesize imageView, textField;
- (id) initWithFrame: (CGRect) frame_ {
if (self = [super initWithFrame:frame_]) {
UIImage* image = [UIImage imageNamed:#"my_image.png"];
self.imageView = [[[UIImageView alloc] initWithImage:image] autorelease];
imageView.userInteractionEnabled = YES;
[self addSubview:imageView];
CGRect textFrame = CGRectMake(10, 10, 50, 20); // whatever frame you want
self.textField = [[[UITextField alloc]
initWithFrame:textFrame] autorelease];
[self addSubview:textField];
// Now register an event to happen if the user clicks anywhere.
[self addTarget:self action:#selector(click)
forEvent:UIControlEventTouchUpInside];
}
return self;
}
- (void) click {
[textField becomeFirstResponder];
}
#end
This is not possible with the current iPhone API (3.1). You will need to create your own custom uitextfields and your own image rendering methods to combine the layers into a captioned photo.
i want to get a multiple line string from a UITextView and draw it on a image,
the position of text in the image must exactly same as the position in UITextView but i dont want jus add the textView to UIImageView, i need a new image consists of those text, is it possible? any suggestion to do that?
You can capture the contents of any UIView into a UIImage. First, create an empty UIView and position your UIImageView and UITextView as subviews:
// Assumes you have UIImageView *myImageView and UITextField *myTextField
UIView *parentView = [[UIView alloc] initWithFrame:CGRectZero];
[parentView addSubview:myImageView];
[myImageView setFrame:CGRectMake(0, 0, 100, 100)];
[parentView addSubview:myTextField];
[myTextField setFrame:CGRectMake(20, 20, 80, 20)];
[parentView sizeToFit];
Now create a graphics context and draw parentView into it:
UIGraphicsBeginImageContext([parentView bounds].size);
[[parentView layer] renderInContext:UIGraphicsGetCurrentContext()];
UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
You now have a UIImage with the contents of your UIImageView and UITextField to display, save, or send over the network.
This was a good solution, thanks.
I would like to add that you need to add
#import <QuartzCore/QuartzCore.h>
To your imports so the warnings go away.