How can I set/customize setTitleVerticalPositionAdjustment in iOS 4.0? I need suggestion.
- (void)viewDidLoad
{
label = [[UILabel alloc] init];
label.backgroundColor = [UIColor clearColor];
label.textAlignment = UITextAlignmentRight;
label.textColor=[UIColor whiteColor];
label.font = [ UIFont fontWithName: #"XXII DIRTY-ARMY" size: 32.0 ];
label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.0f];
label.text=NSLocalizedString(#"HomeTitle", #"Home");
self.navigationController.navigationBar.tintColor = [UIColor newBrownLight];
label.frame=CGRectMake(0, 0, 290, 44);
self.navigationItem.titleView = label;
self.navigationItem.title=label.text;
}
Related
I need to align UILabel text center on UINavigationBar. I have implemented all the steps but some how its not center Align. Below is code.
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
titleLabel.backgroundColor = [UIColor clearColor];
[titleLabel setTextColor:[UIColor whiteColor]];
self.titleLabel.text = title;
self.titleLabel.lineBreakMode = NSLineBreakByTruncatingMiddle;
self.titleLabel.adjustsFontSizeToFitWidth = NO;
self.titleLabel.adjustsLetterSpacingToFitWidth = NO;
self.titleLabel.textAlignment = NSTextAlignmentCenter;
[self.titleLabel sizeToFit];
float titlePadding = 60.0f;
self.titleLabel.left = titlePadding;
self.titleLabel.width = self.width - 2 * titlePadding;
self.titleLabel.top = ceilf(self.frame.size.height / 2 - self.titleLabel.frame.size.height / 2);
Please help me with this
Try This It will help
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:20.0];
label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
label.textAlignment = NSTextAlignmentCenter;
// ^-Use UITextAlignmentCenter for older SDKs.
label.textColor = [UIColor yellowColor]; // change this color
self.navigationItem.titleView = label;
label.text = NSLocalizedString(#"PageThreeTitle", #"");
[label sizeToFit];
I want add UIScrollView with label and make it scroll vertical if i have more labels i am adding following code but it is not scrolling. I have more 5 labels to add in it also.
In Header file
IBOutlet UIScrollView*scrollView;
#property(nonatomic,retain) IBOutlet UIScrollView*scrollView;
In implementation file in viewDidLoad
titleLabel = [[UILabel alloc ] initWithFrame:CGRectMake(892,257,206,84)];
titleLabel.textAlignment = UITextAlignmentRight;
titleLabel.textColor = [UIColor blackColor];
titleLabel.font = [UIFont fontWithName:#"Helvetica" size:14];
[scrollView addSubview:titleLabel];
titleLabel = [[UILabel alloc ] initWithFrame:CGRectMake(10,20,206,84)];
titleLabel.textAlignment = UITextAlignmentRight;
titleLabel.textColor = [UIColor blackColor];
titleLabel.font = [UIFont fontWithName:#"Helvetica" size:14];
[scrollView addSubview:titleLabel];
titleLabel1 = [[UILabel alloc ] initWithFrame:CGRectMake(10,110,206,84)];
titleLabel1.textAlignment = UITextAlignmentRight;
titleLabel1.textColor = [UIColor blackColor];
titleLabel1.font = [UIFont fontWithName:#"Helvetica" size:14];
titleLabel1.text=#"tilte Working Fine";
[scrollView addSubview:titleLabel1];
titleLabel2 = [[UILabel alloc ] initWithFrame:CGRectMake(10,190,206,84)];
titleLabel2.textAlignment = UITextAlignmentRight;
titleLabel2.textColor = [UIColor blackColor];
titleLabel2.text=#"tilte Working Fine";
titleLabel2.font = [UIFont fontWithName:#"Helvetica" size:14];
[scrollView addSubview:titleLabel2];
titleLabel3 = [[UILabel alloc ] initWithFrame:CGRectMake(10,270,206,84)];
titleLabel3.textAlignment = UITextAlignmentRight;
titleLabel3.textColor = [UIColor blackColor];
titleLabel3.font = [UIFont fontWithName:#"Helvetica" size:14];
titleLabel3.text=#"tilte Working Fine";
[scrollView addSubview:titleLabel3];
titleLabel4 = [[UILabel alloc ] initWithFrame:CGRectMake(10,360,206,84)];
titleLabel4.textAlignment = UITextAlignmentRight;
titleLabel4.textColor = [UIColor blackColor];
titleLabel4.font = [UIFont fontWithName:#"Helvetica" size:14];
titleLabel4.text=#"tilte Working Fine";
[scrollView addSubview:titleLabel4];
titleLabel5 = [[UILabel alloc ] initWithFrame:CGRectMake(10,450,206,84)];
titleLabel5.textAlignment = UITextAlignmentRight;
titleLabel5.textColor = [UIColor blackColor];
titleLabel5.font = [UIFont fontWithName:#"Helvetica" size:14];
titleLabel5.text=#"tilte Working Fine";
[scrollView addSubview:titleLabel5];
titleLabel6 = [[UILabel alloc ] initWithFrame:CGRectMake(10,530,206,84)];
titleLabel6.textAlignment = UITextAlignmentRight;
titleLabel6.text=#"tilte Working Fine";
titleLabel6.textColor = [UIColor blackColor];
titleLabel6.font = [UIFont fontWithName:#"Helvetica" size:14];
[scrollView addSubview:titleLabel6];
You need to increase the content size of the scroll view its diffrent from its frame size if content size is more then frame size it will scroll.
I have a grouped UITableView. I am trying to make a custom UITableViewCell background:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.contentView.opaque = YES;
self.contentView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"rowbg#2x"]];
//Change Indicator
CGRect rect;
rect = CGRectMake(0, 0, 10, 50);
changeImageIndicator = [[UIImageView alloc] initWithFrame:rect];
[self.contentView addSubview: changeImageIndicator];
//Symbol
rect = CGRectMake(10, 10, 200, 20);
symbolLabel = [[UILabel alloc] initWithFrame:rect];
symbolLabel.textAlignment = UITextAlignmentLeft;
symbolLabel.font = [UIFont fontWithName:#"Arial" size:22];
symbolLabel.textColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0];
symbolLabel.backgroundColor = [UIColor clearColor];
[self.contentView addSubview: symbolLabel];
//Company
rect = CGRectMake(10, 30, 180, 20);
companyLabel = [[UILabel alloc] initWithFrame:rect];
companyLabel.textAlignment = UITextAlignmentLeft;
companyLabel.font = [UIFont fontWithName:#"Arial" size:13];
companyLabel.adjustsFontSizeToFitWidth = YES;
companyLabel.minimumFontSize = 10.0;
companyLabel.backgroundColor = [UIColor clearColor];
companyLabel.textColor = [UIColor colorWithRed:118.0/255.0 green:118.0/255.0 blue:118.0/255.0 alpha:1.0];
[self.contentView addSubview: companyLabel];
//Price
rect = CGRectMake(190, 10, 100, 20);
priceLabel = [[UILabel alloc] initWithFrame:rect];
priceLabel.textAlignment = UITextAlignmentRight;
priceLabel.font = [UIFont fontWithName:#"Arial" size:20];
priceLabel.textColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0];
priceLabel.backgroundColor = [UIColor clearColor];
[self.contentView addSubview: priceLabel];
//Change
rect = CGRectMake(190, 30, 100, 20);
changeLabel = [[UILabel alloc] initWithFrame:rect];
changeLabel.textAlignment = UITextAlignmentRight;
changeLabel.font = [UIFont fontWithName:#"Arial" size:15];
changeLabel.textColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0];
changeLabel.adjustsFontSizeToFitWidth = YES;
changeLabel.backgroundColor = [UIColor clearColor];
changeLabel.minimumFontSize = 10.0; //adjust to preference obviously
[self.contentView addSubview: changeLabel];
}
return self;
}
The background color bleeds past the rounded corners. See image:
How can I make this not bleed?
This works for iOS 3.0 and later:
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.backgroundColor = [UIColor redColor];
}
What about self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; ?
This worked for me when nothing else did. Set the background color in IB to the grouped Table view color (default). Then in code, set the color to clearColor. I also have the cells marked opaque=NO and clearsContextBeforeDrawing=NO, but those settings alone didn't change anything until I added the clearColor by code – Bdebeez
self.contentView.superview.opaque = YES;
self.contentView.superview.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"rowbg#2x"]];
Have you tried setting the backgroundColor of the backgroundView rather than the contentView?
I had to create a rounded top, middle and button graphic image and set it to the background view of the cell depending on which row it is.
Set the cell background color in the tableView:cellForRowAtIndexPath:
cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"Gradient.png"]];
I am reproducing the original UITableView section header using custom graphics.
What is the font size, font color, shadow color & offset in UITableView section header ?
Thanks.
Try this formula:
headerLabel.font = [UIFont boldSystemFontOfSize:17];
headerLabel.shadowColor = [UIColor colorWithWhite:1.0 alpha:1.0];
headerLabel.shadowOffset = CGSizeMake(0, 1);
headerLabel.textColor = [UIColor colorWithRed:0.265 green:0.294 blue:0.367 alpha:1.0];
UILabel * headerLabel = [[UILabel alloc] initWithFrame:CGRectZero];
headerLabel.backgroundColor = [UIColor clearColor];
headerLabel.opaque = NO;
headerLabel.textColor = [UIColor blackColor];
headerLabel.highlightedTextColor = [UIColor whiteColor];
headerLabel.font = [UIFont boldSystemFontOfSize:20];
headerLabel.frame = CGRectMake(10.0, 0.0, 300.0, 44.0);
Hope this will fulfill your requirements...
If you mean the pre-iOS7 look, I got good results with:
UITableViewHeaderFooterView *headerView = [[UITableViewHeaderFooterView alloc] init];
headerView.textLabel.text = title;
headerView.textLabel.textColor = [UIColor whiteColor];
headerView.textLabel.shadowColor = [UIColor colorWithRed:0.40 green:0.42 blue:0.43 alpha:1.0];
headerView.textLabel.shadowOffset = CGSizeMake(0, 1);
If you're not using UITableViewHeaderFooterView, then create one as a test and grab its label's other attributes (font, etc.).
I have used this code to change a NavigationItem’s title:
self.navigationItem.title = #"My Name is ABC";
How can I change the font size and font as well?
Sample Code :
CGRect frame = CGRectMake(0, 0, 400, 44);
UILabel *label = [[[UILabel alloc] initWithFrame:frame] autorelease];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:8.0];
label.textAlignment = UITextAlignmentCenter;
label.textColor = [UIColor whiteColor];
label.text = #"Sample custom Title With small Fonts ";
self.navigationItem.titleView = label;
As of iOS5+, you can use the UIAppearance protocol to change the font of the nav title, here's code:
NSMutableDictionary *titleBarAttributes = [NSMutableDictionary dictionaryWithDictionary: [[UINavigationBar appearance] titleTextAttributes]];
[titleBarAttributes setValue:[UIFont fontWithName:#"Helvetica-Bold" size:18] forKey:UITextAttributeFont];
[[UINavigationBar appearance] setTitleTextAttributes:titleBarAttributes];
Since iOS5 you can use the new titleTextAttributes property like so:
NSMutableDictionary *newAttributes = [[NSMutableDictionary alloc] init];
[newAttributes setObject:[UIFont boldSystemFontOfSize:18] forKey:UITextAttributeFont];
[self.navigationController.navigationBar setTitleTextAttributes:newAttributes];
Possible keys are:
UITextAttributeFont
UITextAttributeTextColor
UITextAttributeTextShadowColor
UITextAttributeTextShadowOffset
To make the above example centered you have to have the size be only as wide as the text, this should do the trick
UIFont * font = [UIFont fontWithName:#"Helvetica" size:18.0f];
CGRect frame = CGRectMake(0, 0, [#"Lorem Ipsum" sizeWithFont:font].width, 44);
As of iOS5, you can do this in 1 line:
[[UINavigationBar appearance] setTitleTextAttributes: #{UITextAttributeFont: [UIFont fontWithName:#"Helvetica" size:20.0f]}];
Swift 4
in AppDelegate.swift
UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedStringKey.font: UIFont(name: "Helvetica-Bold", size: 15)!], for: .normal)
Here is the modern Swift 2 solution:
let customFont = UIFont(name: "Helvetica", size: 17)
UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName : customFont!]
//Appearance setup for navigation title font throught application...
let customFont = UIFont(name: "Helvetica-Bold", size: 15)
UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.font : customFont!]
I have found some good answer for this .I think it's similar to the accepted answer however please find it in some more detail.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self){
// this will appear as the title in the navigation bar
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:20.0];
label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
label.textAlignment = UITextAlignmentCenter;
label.textColor = [UIColor yellowColor]; // change this color
self.navigationItem.titleView = label;
label.text = NSLocalizedString(#"PageThreeTitle", #"");
[label sizeToFit];
}
return self;
}
For more details, Please refer this Link
For swift 3
self.navigationController?.navigationBar.titleTextAttributes = [NSFontAttributeName :UIfont(named:"test") ]