UINavigationBar styling issue? - objective-c

I have a UINavigationBar I am having style issues. To start out, I use this code (a class inside my navigation controller) to style my UINavigationBar.
#import...
#implementation UINavigationBar (UINavigationBarCustomDraw)
- (void) drawRect:(CGRect)rect {
[self setTintColor:[UIColor colorWithRed:0.9f green: 0.9f blue:0.9f alpha:1]];
if ([self.topItem.title length] > 0 && ![self.topItem.title isEqualToString:#"Back to ..."]) {
[[UIImage imageNamed:#"UINavigationBar_background.png"] drawInRect:rect];
CGRect frame = CGRectMake(0, 0, 320, 44);
UILabel *label = [[[UILabel alloc] initWithFrame:frame] autorelease];
[label setBackgroundColor:[UIColor clearColor]];
label.font = [UIFont boldSystemFontOfSize: 20.0];
label.shadowColor = [UIColor clearColor];
label.textAlignment = UITextAlignmentCenter;
label.textColor = [UIColor colorWithRed:(70.0 / 255.0) green:(70.0 / 255.0) blue:(70.0 / 255.0) alpha: 1];
label.text = self.topItem.title;
self.topItem.titleView = label;
} else {
//[[UIImage imageNamed:#"login_button.png"] drawInRect:rect];
self.topItem.titleView = [[[UIView alloc] init] autorelease];
}
}
#end
#implementation...
Here's my two issues:
When I apply this code in another document with different form and styles, it changes everything. Why is this happening?
When moving from one view to another, the textColor changes to white. It shows great on start up and when I go back in the navigation controller it works fine.
Thanks in advance!
PS: If you need any more code, please ask!

Related

Search bar rounded corner

How to change the border of search bar to rounded corner. Below is my code and I need the orange coloured border to be rounded not as sharp rectangle. Please help
// Search bar
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 60, 270, 41)];
self.searchBar.delegate = self;
self.searchBar.placeholder = MEGLocalizedString(#"search_hint_home_screen", nil);
UIColor *searchBarBackgroundColor = [UIColor clearColor];
self.searchBar.backgroundImage = [UIImage imageFromColor:searchBarBackgroundColor];
[self.searchBar setImage:[[UIImage imageNamed:#"search_icon_general.png"] imageTintedWithColor:primaryEventColor] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
UIView *textField = [self.searchBar subviewWithKindOfClass:[UITextField class]];
textField.backgroundColor = [UIColor orangeColor];
// textField.layer.borderWidth = 1;
textField.layer.cornerRadius = 14;
[(UITextField *)textField setBorderStyle:UITextBorderStyleNone];
// Change the search bar placeholder text color
[textField setValue:[UIColor whiteColor] forKeyPath:#"_placeholderLabel.textColor"];
}
self.searchBar.layer.cornerRadius = 14;
self.searchBar.layer.borderColor = [UIColor orangeColor].CGColor;
This solve your problem
textField.layer.masksToBounds = true

Create a temporary HUD popup notification in Cocoa App

Im trying to have a window pop up to notify the user of an action, kind of like how the Flutter app shows a play/pause notification when a gesture is received.
This comes up for about two seconds and vanishes. The app itself is run from the menu bar so this pop up doesn't take control from whatever application is currently in focus but rather just shows on top of it and cannot be interacted with. I basically want to recreate exactly this. Say in the body of
- (void)popup:(id)sender {}
This is essentially very similar to a toast on android.
Edit: I looked closer at the flutter app and all it does is display two images (.png) one for the huge pause and another to indicate what application its in (in this case iTunes). Question now is how do I display an image like this.
You should take a look at this:
https://github.com/Foxnolds/MBProgressHUD-OSX
or Growl:
http://growl.info/documentation/developer/
To replicate this I added a 'HUD Window" from the interface builder which is just an NSPanel. I then added a image view to the panel and set the panel's colour to clear. I was then able to position the panel where I wanted and overlay the appropriate image.
I'm using this code:
static void showHud(NSString* text, int width, int height, CGFloat duration, int fontSize) {
NSWindow* window = [NSWindow new];
[window setFrame:NSMakeRect(0,0,width,height) display:NO];
[window center];
window.titleVisibility = NSWindowTitleHidden;
window.styleMask = NSWindowStyleMaskBorderless;
window.alphaValue = 0.9;
window.movableByWindowBackground = YES;
[window setLevel: NSStatusWindowLevel];
[window setBackgroundColor: [NSColor clearColor]];
[window setOpaque:NO];
//[window setHasShadow:NO];
[window makeKeyAndOrderFront:NSApp];
NSVisualEffectView *view = [[NSVisualEffectView new] initWithFrame:window.contentView.bounds];;
view.translatesAutoresizingMaskIntoConstraints = NO;
[view setBlendingMode:NSVisualEffectBlendingModeBehindWindow];
[view setMaterial:NSVisualEffectMaterialDark];
[view setState:NSVisualEffectStateActive];
//[view setAppearance:[NSAppearance appearanceNamed:NSAppearanceNameVibrantDark]];
//[view setWantsLayer:NO];
[view setWantsLayer:YES];
view.layer.cornerRadius = 16.;
//view.layer.shouldRasterize = YES;
//view.layer.rasterizationScale = 0.45;
view.layer.shadowOpacity = 0.1; //0.01;
view.layer.edgeAntialiasingMask = kCALayerTopEdge | kCALayerBottomEdge | kCALayerRightEdge | kCALayerLeftEdge;
[window.contentView addSubview:view];
//window.contentView = view;
NSTextField *label = [[NSTextField alloc] initWithFrame:view.bounds];
[label setBezeled:NO];
[label setDrawsBackground:NO];
[label setEditable:NO];
[label setSelectable:NO];
label.stringValue = text;
label.alignment = NSTextAlignmentCenter;
label.textColor = [NSColor whiteColor];
label.backgroundColor = [NSColor clearColor];
label.font = [NSFont fontWithName:#"Arial-BoldMT" size:fontSize];
//NSRect titleRect = [label titleRectForBounds:view.bounds];
NSSize cellSize = [label.cell cellSizeForBounds:view.bounds];
NSSize strSize = cellSize; // [label.attributedStringValue size];
NSRect frame = view.frame;
frame.origin.y = frame.size.height / 2 - strSize.height / 2;
frame.size.height = strSize.height;
label.frame = frame;
[view addSubview:label];
[NSAnimationContext runAnimationGroup:^(NSAnimationContext *context) {
context.duration = duration;
window.animator.alphaValue = 0.;
} completionHandler:^{
[window close];
}];
}

Strange behaviour when laying out subviews

I am creating a UI that the user can page through multiple views by using a UIScrollView. I create the UIView objects in code and add them to UIScrollView. I use the code below to create the views.
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
UIView *brandView = [[UIView alloc] initWithFrame:frame];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 50.0, frame.size.width, 30)];
label.text = [brand objectForKey:#"name"];
label.textAlignment = UITextAlignmentCenter;
label.backgroundColor = [UIColor redColor];
[brandView addSubview:label];
UIImageView *logoImageView = [[UIImageView alloc] init];
logoImageView.frame = CGRectMake(20.0, 100.0, 280.0, 300.0);
[brandView addSubview:logoImageView];
logoImageView.file = [brand objectForKey:#"logo"];
[logoImageView loadInBackground];
Although I give 50.0 for the y coordinate for the label, I can't see the label at all when I run the app. And logoImageView has 100.0 for its top but it appears just below the status bar, as if it had 0.0 for it's y coordinate.
What is the reason for this strange behaviour, am I doing something wrong?
Note: Auto layout for the UIScrollView is disabled on IB.
Below code will work for you !
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * 10;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
UIView *brandView = [[UIView alloc] init]; // CHANGE THAT HAS TO BE DONE
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 50.0, self.scrollView.frame.size.width, 30)];
label.text = #"Test";
label.textAlignment = UITextAlignmentCenter; // DEPRECATED IN iOS 6.0
label.backgroundColor = [UIColor redColor];
[brandView addSubview:label];
[self.scrollView addSubview:brandView];

Two lines of text on UIButton with different fonts each

I found a way to add two lines of text on a UIButton,
but what I want is that each of these lines of texts
have different font (for instance one is bold, other not).
How is it possible to do this?
Thanks.
You should add 2 UILabel to the UIButton as subviews.
You can do it like:
UIButton *testButton = [UIButton buttonWithType:UIButtonTypeCustom];
testButton.frame = CGRectMake(0, 0, 200, 40);
[self.view addSubview:testButton];
UILabel *firstLineTestButton = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 20)];
firstLineTestButton.text = #"First line";
firstLineTestButton.font = [UIFont systemFontOfSize:12];
[testButton addSubview:firstLineTestButton];
UILabel *secondLineTestButton = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, 200, 20)];
secondLineTestButton.text = #"Second line";
secondLineTestButton.font = [UIFont boldSystemFontOfSize:12];
[testButton addSubview:secondLineTestButton];
To also make highlighting possible for the UILabels, you need to make the highlighting of the button custom.
So add the actions to the button and then check the button subviews for the labels and change their colors.
[testButton addTarget:self action:#selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[testButton addTarget:self action:#selector(changeColor:) forControlEvents:UIControlEventTouchDown];
[testButton addTarget:self action:#selector(touchCancel:) forControlEvents:UIControlEventTouchDragExit];
-(void)buttonAction:(UIButton*)sender
{
[self touchCancel:sender];
/* DO SOME MORE ACTIONS */
}
-(void)changeColor:(UIButton*)sender
{
sender.backgroundColor = [UIColor grayColor];
for( UIView *subview in sender.subviews ){
if( [subview isKindOfClass:[UILabel class]] ){
UILabel *subViewLabel = (UILabel*)subview;
subViewLabel.textColor = [UIColor blueColor];
}
}
}
-(void)touchCancel:(UIButton*)sender
{
sender.backgroundColor = [UIColor clearColor];
for( UIView *subview in sender.subviews ){
if( [subview isKindOfClass:[UILabel class]] ){
UILabel *subViewLabel = (UILabel*)subview;
subViewLabel.textColor = [UIColor blackColor];
}
}
}
The solution of Roland is good, another way to do this would be to use a NSAttributedString. The downside is, that it only works in iOS 6 and above.
If this is not a problem, here is the code
- (void)viewDidLoad
{
[super viewDidLoad];
// We want 2 lines for our buttons' title label
[[self.button titleLabel] setNumberOfLines:2];
// Setup the string
NSMutableAttributedString *titleText = [[NSMutableAttributedString alloc] initWithString:#"This should be bold,\n and this should not."];
// Set the font to bold from the beginning of the string to the ","
[titleText addAttributes:[NSDictionary dictionaryWithObject:[UIFont boldSystemFontOfSize:14] forKey:NSFontAttributeName] range:NSMakeRange(0, 20)];
// Normal font for the rest of the text
[titleText addAttributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:14] forKey:NSFontAttributeName] range:NSMakeRange(20, 22)];
// Set the attributed string as the buttons' title text
[self.button setAttributedTitle:titleText forState:UIControlStateNormal];
}
You can add two UILabels as subview of the button and then can set the text of the labels as
[lbl1 setFont:[UIFont fontWithName:#"Arial-Bold" size:18]];
[lbl2 setFont:[UIFont fontWithName:#"Arial" size:16]];
For proper animation, you should subclass UIButton and redraw it when its state changes.
(void)drawRect:(CGRect)rect
{
if (!self.firstLineTestButton) {
self.firstLineTestButton = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 20)];
self.firstLineTestButton.text = #"First line";
self.firstLineTestButton.font = [UIFont systemFontOfSize:12];
[self addSubview:self.firstLineTestButton];
}
if (!self.secondLineTestButton) {
self.secondLineTestButton = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, 200, 20)];
self.secondLineTestButton.text = #"Second line";
self.secondLineTestButton.font = [UIFont boldSystemFontOfSize:12];
[self addSubview:self.secondLineTestButton];
}
if (!self.highlighted) {
// Do custom drawing such as changing text color
} else {
// Do custom drawing such as changing text color
}
}
(void)setHighlighted:(BOOL)highlighted {
[super setHighlighted:highlighted];
// Force redraw of button
[self setNeedsDisplay];
}

UITextView Shadow Not Working

I am trying to get a UITextView's layer's shadow to work in a UITableView header. I have a UIView that I am formatting everything in, and then setting the headerview equal to it.
UIView * view = [[UIView alloc] initWithFrame: CGRectMake(0, 0, self.tableView.frame.size.width, 450);
UIColor * baseColor = [[UIColor alloc] initWithRed: 45/255.0
green: 100/255.0
blue: 150/255.0
alpha: 1.0];
view.backgroundColor = baseColor;
...
UITextView * newCommentField = [[UITextView alloc] initWithFrame:CGRectMake(20, 230, 270, 120)];
newCommentField.text = #"New Comment";
newCommentField.tag = 3;
newCommentField.layer.shadowOffset = CGSizeMake(0, 3);
newCommentField.layer.shadowRadius = 5.0;
newCommentField.layer.shadowColor = [UIColor blackColor].CGColor;
newCommentField.layer.shadowOpacity = 0.8;
[view addSubview:newCommentField];
...
self.tableView.tableHeaderView = view;
Everything shows up properly in the view. However, the shadow is not appearing. I don't know what is going wrong here. I have even tried modifying the layer's frame and making it the size of the comment field, bigger and the same size.
You're not setting background color for newCommentField.
Try:
newCommentField.backgroundColor = [UIColor whiteColor];
You can do it this way also... Create a view (eg. bgView) of same size as your textView and add that textView as a subView of bgView, which will be added as subView of you header view. Apply the shadow effect to the bgView instead.
UIView *bgView=[[UIView alloc]initWithFrame:CGRectMake(20, 230, 270, 120)];
bgView.backgroundColor=[UIColor whiteColor];
bgView.layer.shadowOffset = CGSizeMake(0, 3);
bgView.layer.shadowRadius = 5.0;
bgView.layer.shadowColor = [UIColor blackColor].CGColor;
bgView.layer.shadowOpacity = 0.8;
[bgView addSubview:newCommentField];
[view addSubview:bgView];