UILabels have Faint Borders - ios

I thought the default was a border width of 0.0. So I'm surprised I'm seeing boarders around my UILabels. I even tried using a CALayer to set the border to 0.0 and to set it to white, but I still see faint outlines. Here's my code.
-(UIView *)calendarDay:(int)d date:(NSDate *)date width:(float)w height:(float)h
{
//d is the day number in the month of the 28/293 calendar. It should display in bold in the upper, left.
//date is the date of that day. It should display in the upper, right.
//w is the width of the UIView in which to insert the labels.
//h is the height of the UIView in which to insert the labels.
//First create an outer view with a red background color.
UIView *frameView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, w, h)];
[frameView setBackgroundColor:[UIColor redColor]];
//Next decrease the width and height slightly to make a smaller view that fits inside the larger one.
w = w*0.98;
h = h*0.98;
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MM-dd "];
UILabel *dayNumber = [[UILabel alloc] init];
dayNumber.text = [NSString stringWithFormat:#"%2d",d];
dayNumber.font = [UIFont boldSystemFontOfSize:16];
dayNumber.frame = CGRectMake(0,0,w/2,h/5);
dayNumber.backgroundColor = [UIColor whiteColor];
UILabel *gregDate = [[UILabel alloc] init];
gregDate.text = [dateFormat stringFromDate:date];
gregDate.textAlignment = NSTextAlignmentRight;
gregDate.frame = CGRectMake(w/2,0,w/2,h/5);
gregDate.backgroundColor = [UIColor whiteColor];
Following three lines were an attempt to make the borders disappear. It made no difference.
CALayer *gregDateLayer = [gregDate layer];
[gregDateLayer setBorderColor:[[UIColor whiteColor] CGColor]];
[gregDateLayer setBorderWidth:0.0];
[gregDate setTextColor:[UIColor blackColor]];
CGRect dayRect = CGRectMake(0, 0, w, h);
UIView *dayToReturn = [[UIView alloc] initWithFrame:dayRect];
[dayToReturn setBackgroundColor:[UIColor whiteColor]];
dayNumber.center = CGPointMake(w/4 ,h/4);
gregDate.center = CGPointMake(w*3/4,h/4);
[dayToReturn addSubview:dayNumber];
[dayToReturn addSubview:gregDate];
[frameView addSubview:dayToReturn];
dayToReturn.center = frameView.center;
return frameView;
}

I think that what you are thinking is a border is actually the background color of the UILabel.
Set the background color of your UILabels to [UIColor clearColor].

Related

Adding an Image with a Title in a UINavigationController

I would like to show a small icon next to the title in my UINavigationController.
Through the magic of Photoshop, like this:
I know I need to create a new view and build the image and title into it. Here is what I am doing:
In viewDidLoad in the UINavigationController view controller, I call the method
[self setTitleBar];
Which calls this method:
- (void) setTitleBar {
CGRect navBarFrame = self.navigationController.navigationBar.frame;
//UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(navBarFrame.origin.x, navBarFrame.origin.y, (leftButtonFrame.origin.x + leftButtonFrame.size.width) - rightButtonFrame.origin.x, navBarFrame.size.height)];
UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(navBarFrame.origin.x, navBarFrame.origin.y,self.view.frame.size.width,navBarFrame.size.height)];
titleView.backgroundColor = [UIColor clearColor];
CGPoint tvCenter = CGPointMake(titleView.frame.size.width/2, titleView.frame.size.height/2);
UIImage * icon = [UIImage imageNamed:#"star"];
UIImageView *iconView = [[UIImageView alloc] initWithImage:icon];
iconView.frame = CGRectMake(0, 0, icon.size.width, icon.size.height);
UILabel *title = [[UILabel alloc] init];
title.backgroundColor = [UIColor clearColor];
title.textColor = [UIColor whiteColor];
title.textAlignment = NSTextAlignmentCenter;
title.text = #"SOME TITLE";
title.frame = CGRectMake(0, 0, 100, titleView.frame.size.height);
[title sizeToFit];
iconView.center = CGPointMake(tvCenter.x - (icon.size.width/2), tvCenter.y);
[titleView addSubview:iconView];
[titleView addSubview:title];
self.navigationItem.titleView = titleView;
}
My logic in the titleView is: Get the left most button's frame and get the right most buttons frame. THEN do some math to figure out how big the view can be. That should be the titleView's frame size.
However, I can't seem to get it to work. If I plug in a frame size of 0,0,100,40; then it shows the frame but everything is squished together. But you see it. I know that 100 should be dynamic to ensure that the title is shown.
But I can't seem to figure it out.
Any help?
You can place objects on the Navigation Controller View, as subviews.
- (void) setTitleBar {
//Let's say your icon size is 20
int starSize = 20;
//Now you'll have to calculate where to place the ImageView respect the TextSize (for this you'll need to know the text and font of your UINavigationItem title)
CGSize textSize = [#"SOME TITLE" sizeWithAttributes:#{NSFontAttributeName:[UIFont fontWithName:#"navfontname" size:15]}];
UIImageView *startImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.navigationController.view.frame.size.width/2 - textSize.width/2, self.navigationController.view.frame.size.height/2 - starSize/2, starSize,starSize)];
startImageView.image = [UIImage imageNamed:#"star"];
[self.navigationController.view addSubview:startImageView];
}

IOS drawing text inside a subview

im trying to draw a text on a subview, the view appearing fine but the text not, this is what im doing, the view have black color, the text have white color, text and rect value given in other method:
NSString* sumText = [[NSString alloc]initWithFormat:#"%0.1f",sum];
CGRect textRect = CGRectMake(chartLocation.x+chartLength+10,
chartLocation.y,
20,
20);
[self drawTextRect:sumText inRect:textRect];
-(void)drawTextRect:(NSString *)sumText inRect:(CGRect)textRect {
UIFont* font=[UIFont fontWithName:#"Arial" size:(20/2)];
UIColor* textColor = [UIColor whiteColor];
NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle]mutableCopy];
paragraphStyle.alignment = NSTextAlignmentCenter;
NSDictionary* stringAttr=#{NSFontAttributeName:font,NSForegroundColorAttributeName:textColor,NSParagraphStyleAttributeName:paragraphStyle};
UIView* textView =[[UIView alloc] initWithFrame:textRect];
textView.backgroundColor = [UIColor blackColor];
[sumText drawInRect:textRect withAttributes:stringAttr];
[self.view addSubview:textView];}
am i do anything wrong? thanks
The drawInRect:withAttributes: method draws the string in the current graphics context. You have not defined a context, so your drawing goes nowhere.
If you want to draw the string yourself, you should subclass UIView, add the required properties (sumText), and overwrite drawRect:. A graphics context is automatically created for you in drawRect: so you can just go ahead and draw.
But really, you probably just want to use a UILabel instead.
there is another way you can achieve what you want .
UILabel* lblText = [[UILabel alloc]initWithFrame:CGRectMake(50, 50, 200, 200)];
lblText.text = #"Hello World!";
lblText.backgroundColor = [UIColor blackColor];
lblText.textColor = [UIColor whiteColor];
[self.view addSubview:lblText];
And Output is:

iOS Calc X position to place elements on a View

I want to add a Label and an Image in titleView of my NavigationItem. I am adding UILabel and UIImageView to a UIView and setting it as titleView of the navigation Item. Things are being added, but I am not able to calculate the length of label and place image next to it.
My code is :-
// Title Name + Image
UIView *titView = [[UIView alloc] initWithFrame:self.navigationItem.titleView.bounds];
self.navigationItem.titleView = titView;
UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(-10, -20, 150, 30)];
title.text = #"Bunny ";
[title setTextColor:[UIColor orangeColor]];
[titView addSubview:title];
float x2 = 30; //+ title.bounds.size.width;
UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"return" ]];
img.frame = CGRectMake(x2, -20, img.bounds.size.width, 30);
[titView addSubview:img];
I am looking for some way to calc the width text and set accordingly the width of the label. Right now I have set the width of the label as 150. And secondly, calc the x position to place the image just next to the label.
Tried many ways, but nothing works as expected. How can I achieve this ? Can you he give some guidelines such that regardless of length of text of label, things works as expected and UIView gets placed in the center of navigation item.
Any help is highly appreciated. Thanks.
You can call
[title sizeToFit];
after setting its properties and it will size itself to match the contained string.
Set the imageViews x origin to
CGRectGetMaxX(title.frame) + desiredSpacing;
And it could help to subclass UIView to achieve the final results by overwriting layoutSubviews and placing your views there, since the titleView's frame can be adjusted by the navigationBar...basically like this:
#implementation TitleView{
UILabel *_titleLabel;
UIImageView *_img;
}
- (id)initWithTitle:(NSString *)title andImage:(UIImage *)image
{
self = [super init];
if (self) {
_titleLabel = [[UILabel alloc] init];
_titleLabel.text = title;
[_titleLabel setTextColor:[UIColor orangeColor]];
[_titleLabel sizeToFit];
[self addSubview:_titleLabel];
_img = [[UIImageView alloc] initWithImage:image];
[self addSubview:_img];
}
return self;
}
- (void)layoutSubviews{
CGFloat spacingBetweenTextAndImage = 0;
CGFloat width = CGRectGetWidth(_titleLabel.frame)+CGRectGetWidth(_img.frame)+spacingBetweenTextAndImage;
CGFloat x = (CGRectGetWidth(self.bounds)-width)/2;
_titleLabel.frame = CGRectMake(x, (CGRectGetHeight(self.bounds)-CGRectGetHeight(_titleLabel.bounds))/2, CGRectGetWidth(_titleLabel.bounds), CGRectGetHeight(_titleLabel.bounds));
x+=CGRectGetWidth(_titleLabel.bounds)+spacingBetweenTextAndImage;
_img.frame = CGRectMake(x, (CGRectGetHeight(self.bounds)-CGRectGetHeight(_img.bounds))/2, CGRectGetWidth(_img.bounds), CGRectGetHeight(_img.bounds));
}
#end
use in your viewController:
UIView *titleView = [[TitleView alloc] initWithTitle:#"Bunny" andImage:[UIImage imageNamed:#"return" ]];
self.navigationItem.titleView = titleView;

Setting frame for background colour of Selected tableviewCell working fine in iOS 7 but not in iOS 6

I want to set frame for background colour of selected tableviewCell. I am using the following code.
UIView * cellBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
cellBackgroundView.backgroundColor = [UIColor clearColor];
UIView * selectedCellColor = [[UIView alloc] initWithFrame:CGRectMake(5, 0, 300, 44)];
selectedCellColor.backgroundColor = [UIColor colorWithRed:50.0/255.0 green:50.0/255.0 blue:50.0/255.0 alpha:1.0];
[cellBackgroundView addSubview:selectedCellColor];
cell.selectedBackgroundView = cellBackgroundView;
Which is working fine in iOS7. Even Tableview selection also not working in iOS6.
Following code working fine in Both iOS6 and iOS 7. But here i am not able to set frame for background colour of selected tableViewCell.
UIView *selectedCellColor = [[UIView alloc] init];
[selectedCellColor setFrame:CGRectMake(0, 0, 320, 44)];
selectedCellColor.backgroundColor = [UIColor colorWithRed:50.0/255.0
green:50.0/255.0
blue:50.0/255.0
alpha:1.0];
cell.selectedBackgroundView=selectedCellColor;
In cellForRowAtIndexPath:, call dequeueReusableCellWithIdentifier:forIndexPath: (not simply dequeueReusableCellWithIdentifier:. The reason is that now your cell is the correct size. Do not hard-code the cell size; get it as the bounds of the cell you are handed.
Now draw a UIImage in that size, constructing your frame-like drawing (the image itself is clear to begin with). Put it in a UIImageView and set the cell's selected background view to that image view.
This will work in iOS 6 and iOS 7.
Example (color and size arbitrary, just for illustration purposes):
UIGraphicsBeginImageContextWithOptions(cell.bounds.size, NO, 0);
// change this color as you like
[[UIColor redColor] setFill];
// fiddle with this size as you like
[[UIBezierPath bezierPathWithRect:CGRectMake(10,10,300,25)] fill];
UIImage* im = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageView* iv = [[UIImageView alloc] initWithImage:im];
cell.selectedBackgroundView = iv;
EDIT
Try this instead:
[cell.contentView setBackgroundColor:[UIColor redColor]];
UIView *selectedBG = [[UIView alloc]initWithFrame:cell.contentView.frame];
[selectedBG setBackgroundColor:[UIColor greenColor]];
[cell.selectedBackgroundView addSubview:selectedBG];

Adding a badge icon to round rect button?

Is there a convenient way to make a button with a badge? Not on the icon... I want to do it within the app. A button or an image with a badge selected by the program. Or do I have to build a library of photoshop images?
You'll need to use resizableImageWithCapInsets to achieve this without a library of photoshop images. There are some great threads (here and here) that explain its use.
Here's an example I just made to give you an idea:
//Create a label (width/height not important at this stage)
UILabel *yourLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 1, 1)];
yourLabel.text = #"7+";
[yourLabel sizeToFit];
CGRect labelFrame = yourLabel.frame;
//Here we create a UIImage that is resizable, but will not resize the areas concerned with the cap insets you've defined
UIImage *badgeImage = [[UIImage imageNamed:#"badge.png"]resizableImageWithCapInsets:UIEdgeInsetsMake(5, 5, 5, 5)];
UIImageView *badgeImageView = [[UIImageView alloc]initWithImage:badgeImage];
badgeImageView.contentMode = UIViewContentModeScaleToFill;
badgeImageView.backgroundColor = [UIColor clearColor];
labelFrame.size.width += 5; //This is the 'padding' on the right and left (added together)
//If your badge edges are completely circular then you don't want to change the height, but if they're not then go ahead in the same way with the width. If your badge has a static height, you'll need to make sure the font size doesn't exceed this height; better start off with a small font-size
badgeImageView.frame = labelFrame; //The badge is now the right width with padding taken into account
//Center the label on the badge image view
yourLabel.center = CGPointMake(badgeImageView.frame.size.width/2, badgeImageView.frame.size.height/2);
//Finally we add the label to the badge image view
[badgeImageView addSubview:yourLabel];
//Add your badge to the main view
[self.view addSubview:badgeImageView];
[badgeImageView release];
[yourLabel release];
UILabel *lbl_notification_count = [[UILabel alloc]initWithFrame:CGRectMake(5,0, 18, 18)];
lbl_notification_count.textColor = [UIColor whiteColor];
lbl_notification_count.textAlignment = NSTextAlignmentCenter;
lbl_notification_count.text = [NSString stringWithFormat:#"%d",appDel.NotificationBadge];
lbl_notification_count.adjustsFontSizeToFitWidth=YES;
lbl_notification_count.layer.borderWidth = 1;
lbl_notification_count.layer.cornerRadius = lbl_notification_count.layer.frame.size.height/2;
lbl_notification_count.layer.masksToBounds = YES;
lbl_notification_count.layer.borderColor =[[UIColor colorWithRed:241.0/255.0 green:84.0/255.0 blue:67.0/255.0 alpha:1.0] CGColor];
lbl_notification_count.backgroundColor = [UIColor colorWithRed:241.0/255.0 green:84.0/255.0 blue:67.0/255.0 alpha:1.0];
lbl_notification_count.font = CustomFontMediumWithSize(12);
if (appDel.NotificationBadge >= 1) {
[btnNotification addSubview:lbl_notification_count];
[lbl_notification_count setHidden:NO];
}else{
[lbl_notification_count setHidden:YES];
}

Resources