UILabel autoresizingMask in UITableViewCell - iphone

I add UILabel to simple UITableViewCell, but when i rotate device to landscape, UILabel doesn't move to right side,even with myLabel.autoresizingMask = UIViewAutoResizingFlexibleRightMargin; it's in the middle of the UITableViewCell.
In portrait mode myLabel is on the right side of the UITableViewCell. Here is code:
myLabel = [[UILabel alloc] initWithFrame:CGRectMake(252, 12, 60, 20)];
myLabel.font = [UIFont fontWithName:#"Helvetica" size:11];
myLabel.textColor = tableViewCell.detailTextLabel.textColor;
myLabel.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
myLabel.highlightedTextColor = [UIColor whiteColor];
myLabel.text = video.duration;
myLabel.tag = 999;
[cell.contentView addSubview:durationLabel];
[myLabel release];
How to move this UILabel to right side of the cell when i rotate my device to landscape mode?

If you want to move it to the right side, you need to set UIViewAutoresizingFlexibleLeftMargin (because the left margin will increase)

Related

UITextField as sublayer

I am adding a UITextField's layer as a sublayer to an existing layer that is actually a UIImageView. I apply perspective rotation on the image view and it works fine. But the UITextField does not become active. The beginFirstResponder message to it does not show the keyboard. Tapping it doesn't work, it stays inactive from user interaction. I also tried setting the zPosition for the textfield's layer so that it is above the imageview layer and it's zPosition.
rightband = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"right-band.png"]];
rightband.layer.frame = CGRectMake(623.5, 310.5, 624, 210);
rightband.layer.zPosition = 40.0f;
usernameField = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 260, 26)];
usernameField.clearButtonMode = UITextFieldViewModeWhileEditing;
usernameField.keyboardType = UIKeyboardTypeDefault;
usernameField.autocapitalizationType = UITextAutocapitalizationTypeNone;
usernameField.borderStyle = UITextBorderStyleNone;
usernameField.backgroundColor = [UIColor clearColor];
usernameField.font = [UIFont fontWithName:#"HelveticaNeue" size:17];
usernameField.textColor = UIColorFromRGB(0x303030);
usernameField.textAlignment = UITextAlignmentLeft;
usernameField.placeholder = #"Username";
usernameField.autocorrectionType = UITextAutocorrectionTypeNo;
usernameField.returnKeyType = UIReturnKeyNext;
usernameField.delegate = self;
usernameField.layer.anchorPoint = CGPointMake(1.0f, 0.5f);
usernameField.layer.zPosition = 100.0f;
usernameField.layer.frame = CGRectMake(10, 10, 260, 26);
[rightband.layer addSublayer:usernameField.layer];
You should add the text field view in order for it to respond to touches.
So do :
[rightband addSubview:usernameField];
Can you try to change the view heirarchy to the following
UIView -> UIImageView and UITextField
Dont add the UITextField as a child to UIImageView
The UITextField and UIImageView will be both child's to a UIView

image in title of navigation bar in objective c

I have made an iPad application, in that I used navigation control,
now in the title bar , I want to put image on left side, so I hide title bar with label, but label is not covering entire width of title bar,
IL APP IN THE SCREEN SHOT,
here is the code snippet,
UILabel *titleView = (UILabel *)self.navigationItem.titleView;
if (!titleView) {
titleView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 800, 50)];
titleView.backgroundColor = [UIColor clearColor];
titleView.font = [UIFont boldSystemFontOfSize:20.0];
titleView.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
titleView.textColor = [UIColor yellowColor]; // Change to desired color
titleView.backgroundColor=[UIColor blackColor];
titleView.text = #"IL APP";
titleView.textAlignment=UITextAlignmentCenter;
//self.navigationItem.titleView = titleView;
[self.navigationItem setTitleView:titleView];
[titleView release];
}
#Gama as far as your question is concerned you are asking to put an Image but your real issue being described is that the label is not covering up the title bar. To cover that properly you can use
UILabel *titleView = (UILabel *)self.navigationItem.titleView;
if (!titleView) {
titleView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 32)];
titleView.backgroundColor = [UIColor clearColor];
titleView.font = [UIFont boldSystemFontOfSize:20.0];
titleView.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
titleView.textColor = [UIColor yellowColor]; // Change to desired color
//titleView.backgroundColor=[UIColor blackColor];
titleView.text = #"IL APP";
titleView.textAlignment=UITextAlignmentCenter;
[self.navigationItem setTitleView:titleView];
[titleView release];
}
[self.navigationController.navigationBar setTintColor:[UIColor blackColor]];
For using an image you can use an image view instead of a label to be assigned as the titleView for navigationItem.
Hope it helps
I tried to solved your problem but I get the same result.
But you can hide navigation bar and add the UILabel in to your ViewController
self.navigationController.navigationBarHidden = FALSE;
[self.view addSubview:titleview];
UIImageView *imageNav = [[UIImageView alloc] initWithImage: [UIImage imageNamed: #"navImage.png"]];
[self.navigationController.navigationBar addSubview:imagenav];
[self.navigationController.navigationBar sendSubviewToBack:imageNav];
[imageNav release];
Hi i wanted to do the same thing and i saw your posting, i basically used some of the code you provided and modified it a little and got it to work see below:
//declare and initialize your imageView
UIImageView *titleImage = (UIImageView *)self.navigationItem.titleView;
titleImage = [[UIImageView alloc]
initWithFrame:CGRectMake((self.navigationController.navigationBar.frame.size.width/2)
- (100/2), 0, 100, self.navigationController.navigationBar.frame.size.height)];
//setting the image for UIImageView
titleImage.image = [UIImage imageName:#"photo.jpg"];
//NOTE: you have to do this line at the bottom to add image to the navigation bar Try it it WORKS!
self.navigationItem.titleView = titleImage;

Autoresizingmask in not working for allignment of LAbels on Rotation

I am doing an application where i am creating a UILabels programmitically,As i will be creating some 15 labels with different frames so i using only one method and calling it by parameter passing.
when i rotate it to Landscape view the position of the labels should be changed as there will lot of space left empty at right side..so i am trying to use Autoresizing mask but its not working,so please suggest what i should do so that label frame changes and fits at right position when i turn to landscape orientation. please provide me a sample code.The present code i am using is given below.
{
[self.view addSubview:[self createLabel:CGRectMake(450,140,60,20):#"Ratings:"]];
[self.view addSubview:[self createLabel:CGRectMake(450,170,60,20):#"Reviews:"]];
[self.view addSubview:[self createLabel:CGRectMake(50,170,50,20):#"Hours:"]];
[self.view addSubview:[self createLabel:CGRectMake(50,200,50,20):#"Phone:"]];
self.view.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth;
[super viewDidLoad];
}
-(UILabel*)createLabel:(CGRect)frame :(NSString*)labelTitle
{
UILabel *myLabel = [[UILabel alloc] initWithFrame:frame];
[UIFont fontWithName:#"Arial" size:13];
myLabel.textAlignment = UITextAlignmentLeft;
myLabel.font = [UIFont boldSystemFontOfSize:13];
myLabel.text = labelTitle;
[myLabel autorelease];
return myLabel;
}
You should set also autoresizingMask to all subview of self.view - add, for example, next line in the -(UILabel*)createLabel:(CGRect)frame :(NSString*)labelTitle method:
myLabel.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth;
or
myLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleWidth;

UIScrollView Inside UITableViewCell

I'm trying to add a UIScrollView inside my TableViewCell. It appears to be going well, however when the label comes in from the right as I'm scrolling, it comes in above the cell, but as the text moves to the left edge of the label, it disappears behind the label like I want it to.
Any suggestions?
UIScrollView *previewScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, cell.contentView.frame.size.width, 80.0)];
previewScrollView.backgroundColor = [UIColor clearColor];
[previewScrollView setContentSize:CGSizeMake(500, 60.0)];
previewScrollView.showsHorizontalScrollIndicator = YES;
UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(15.0, 3.0, 500.0, 20.0)];
headerLabel.font = [UIFont boldSystemFontOfSize:14.0];
headerLabel.backgroundColor = [UIColor clearColor];
headerLabel.text = #"My long text string that I hope work out ok";
[previewScrollView addSubview:headerLabel];
[[cell contentView] addSubview:previewScrollView];
//[cell addSubview:previewScrollView];
[previewScrollView release];
from the right as I'm scrolling, it comes in above the cell
Above the cell as in y or z axis?
If it is above in the y axis, you could try setting clipsToBounds on the scroll view. That makes it so subviews are only drawn within the area of their superview.
If it is above in the z axis, where something in front should be behind, try using:
[[cell contentView] insertSubview:previewScrollView atIndex:0];
inserting at index 0 ensures a view is behind all other subviews in the superview.

Setting font and font-size of title bar text in a UITableViewController

I have a simple navigation based application for the iphone/objective-c
within various UIViewControllers that are pushed into view, I can set the text in the title bar using something like
self.title = #"blah blah blah"
Is there a way to control the font and font-size of the title in the title bar text?
thanks!
the proper way to resize the title text of a navcontroller is to set the titleView property of the navigationItem
like this (in viewDidLoad)
UILabel* tlabel=[[UILabel alloc] initWithFrame:CGRectMake(0,0, 300, 40)];
tlabel.text=self.navigationItem.title;
tlabel.textColor=[UIColor whiteColor];
tlabel.backgroundColor =[UIColor clearColor];
tlabel.adjustsFontSizeToFitWidth=YES;
self.navigationItem.titleView=tlabel;
You may want to emboss the label so it doesn't look fuzzy and flat:
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect frame = CGRectMake(0, 0, 400, 44);
UILabel *label = [[[UILabel alloc] initWithFrame:frame] autorelease];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:18.0];
label.textAlignment = UITextAlignmentCenter;
label.textColor = [UIColor whiteColor];
label.text = self.navigationItem.title;
// emboss so that the label looks OK
[label setShadowColor:[UIColor darkGrayColor]];
[label setShadowOffset:CGSizeMake(0, -0.5)];
self.navigationItem.titleView = label;
}
IF you want this to work both on iphone and ipad, and also want to get the title centered then use the following code.
- (void)viewDidLoad
{
[super viewDidLoad];
UILabel* label=[[UILabel alloc] initWithFrame:CGRectMake(0,0, self.navigationItem.titleView.frame.size.width, 40)];
label.text=self.navigationItem.title;
label.textColor=[UIColor whiteColor];
label.backgroundColor =[UIColor clearColor];
label.adjustsFontSizeToFitWidth=YES;
label.font = [AppHelper titleFont];
label.textAlignment = NSTextAlignmentCenter;
self.navigationItem.titleView=label;
}
You can assign any UIView to a navcontroller's title area.
Create a UILabel and set its font and size anyway you want, then assign it to the UIViewController's navigationItem.titleView property. Make sure the UILabel's backgroundColor is set to clearColor.
This only works on the top-level nav-view. As the user drills down into the view controller hierarchy and the "back" button is shown the alternate titleView is ignored and the regular text label is shown.