Blue Highlight around Custom NSButton - objective-c

I want to make a custom NSButton with a solid color for its background but there is this blue highlight around the button that I can't figure out how to get rid of. Does anyone know how to get rid of it?
Thanks!
EDIT:
Here's the code for the button:
#implementation ZDButton
- (id)init {
if (self = [super init]) {
self.title = #"";
self.isSelected = false;
[self setBordered:NO];
[self setBackgroundColor:[NSColor whiteColor]];
}
return self;
}
- (void)drawRect:(NSRect)dirtyRect {
[super drawRect:dirtyRect];
[[NSColor colorWithCalibratedRed:arc4random_uniform(100) / 100.0
green:arc4random_uniform(100) / 100.0
blue:arc4random_uniform(100) / 100.0
alpha:1.0] set];
NSRectFill(dirtyRect);
NSDictionary *att = nil;
NSMutableParagraphStyle *style =
[[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[style setLineBreakMode:NSLineBreakByWordWrapping];
[style setAlignment:NSLeftTextAlignment];
att = [[NSDictionary alloc] initWithObjectsAndKeys:
style, NSParagraphStyleAttributeName,
[NSColor whiteColor],
NSForegroundColorAttributeName, nil];
int width = [self.title sizeWithAttributes:att].width;
int height = [self.title sizeWithAttributes:att].height;
int x = (dirtyRect.size.width - width) / 2;
int y = (dirtyRect.size.height - height) / 2;
NSRect centeredText = NSRectFromCGRect(CGRectMake(x, y, width, height));
[self.title drawInRect:centeredText withAttributes:att];
}
#end

Related

Adding non editable NSTextField as a label into another NSTextField

I am trying to implement a custom NSTextField that a) changes color when active and b) has a label in the top left hand corner.
I have the following implementation:
In TestTextField.h
#interface TestTextField : NSTextField
- (id)initFullWidthWithLabel:(NSString *)label andPreset:(NSString *)preset;
...
#end
In TestTextField.m
#interface TestTextField() {
BOOL _thisFieldIsActive;
}
#property (nonatomic, strong) NSTextField *label;
#end
#implementation TestTextField
- (id)initFullWidthWithLabel:(NSString *)label andPreset:(NSString *)preset {
self = [super initWithFrame:NSZeroRect];
if (self) {
_thisFieldIsActive = NO;
[self setFocusRingType:NSFocusRingTypeNone];
// small label top left
_label = [[NSTextField alloc] initWithFrame:NSZeroRect];
_label.stringValue = label;
if (preset) {
self.stringValue = preset;
}
else {
self.stringValue = #"0";
}
[self layoutUI];
}
return self;
}
- (void)turnActiveOff {
[self toggleActive:NO];
}
- (void)toggleActive:(BOOL)active {
_thisFieldIsActive = active;
if (_thisFieldIsActive) {
self.backgroundColor = [NSColor blueColor];
self.textColor = [NSColor whiteColor];
_label.textColor = [NSColor whiteColor];
}
else {
self.backgroundColor = [NSColor clearColor];
self.textColor = [NSColor blackColor];
_label.textColor = [NSColor grayColor];
}
}
- (BOOL)becomeFirstResponder {
NSLog(#"BecomeFirstResponder");
[self selectText:self];
[self toggleActive:YES];
return [super becomeFirstResponder];
}
- (void)textDidEndEditing:(NSNotification *)notification {
NSLog(#"DidEndEditing");
[self toggleActive:NO];
[super textDidEndEditing:notification];
}
- (void)layoutUI {
self.alignment = NSRightTextAlignment;
self.font = [NSFont fontWithName:#"HelveticaNeue-Light" size:32.0f];
self.layer.borderColor = [NSColor whiteColor].CGColor;
self.layer.borderWidth = 1.0f;
[self.layer setCornerRadius:4.0f];
// small label top left
_label.font = [NSFont fontWithName:#"HelveticaNeue-Light" size:12.0f];
_label.alignment = NSLeftTextAlignment;
_label.textColor = [NSColor grayColor];
_label.stringValue = [_label.stringValue uppercaseString];
_label.selectable = NO;
_label.editable = NO;
_label.drawsBackground = NO;
_label.bezeled = NO;
[self addSubview:_label];
NSDictionary *metrics = #{#"borderPadding": #5};
_label.translatesAutoresizingMaskIntoConstraints = NO;
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|-(borderPadding)-[_label(100)]" options:0 metrics:metrics views:#{ #"_label" : _label }]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|-(borderPadding)-[_label(30)]" options:0 metrics:metrics views:#{ #"_label" : _label }]];
}
In my ViewController I implement these TestTextFields by simply calling the custom initFullWidthWithLabel:andPreset: method and adding them as a subview to the VCs view
I can see that the label gets positioned correctly, however as soon as the the field becomes active and the backgroundColor is set, it seems to cover up the label. How can I make sure the label stays on top?
Even when the backgorund coloring is turned off, the label remains hidden.
Thanks
The core of the solution was to use a custom subclassed NSTextFieldCell with one method:
- (NSRect)drawingRectForBounds:(NSRect)rect {
NSRect rectInset = NSMakeRect(rect.origin.x + 100.0f, rect.origin.y, rect.size.width - 100.0f, rect.size.height);
return [super drawingRectForBounds:rectInset];
}

Drawing leaves trails when resizing window

I am trying to draw a box in custom view. I have the points for the corners of the box and they are scaled. If I put a small rectangle at each vertex on the box and the resize the window it works perfectly. If I use [path stroke] to draw the lines between the vertices and then resize the window, I end up with a mess as the box is redrawn as the window gets resized, so instead of a single box in a resized window I get all the boxes that were redrawn. I have not found a way to clear the custom view of all the intermediary drawings. Any help would be appreciated. Also, I am really new at this.
#import <Cocoa/Cocoa.h>
#interface PointDisplay : NSView
{
NSBezierPath *pathForLine;
NSMutableArray *pointList;
float originalWidth;
float originalHeight;
}
#end
import "PointDisplay.h"
#implementation PointDisplay
- (id)initWithFrame:(NSRect)frameRect
{
self = [super initWithFrame:frameRect];
if (self)
{
int opts = (NSTrackingActiveAlways | NSTrackingInVisibleRect | NSTrackingMouseEnteredAndExited | NSTrackingMouseMoved);
NSTrackingArea *area = [[NSTrackingArea alloc] initWithRect:[self bounds]
options:opts
owner:self
userInfo:nil];
[self addTrackingArea:area];
pathForLine = [[NSBezierPath alloc] init];
pointList = [[NSMutableArray alloc] init];
NSRect originalRect = [self bounds];
originalWidth = originalRect.size.width;
originalHeight = originalRect.size.height;
}
return self;
}
- (void)drawRect:(NSRect)dirtyRect
{
[super drawRect:dirtyRect];
NSRect myRect = [self bounds];
float widthNow = myRect.size.width;
float heightNow = myRect.size.height;
[[NSColor greenColor] set];
[NSBezierPath fillRect:myRect];
int rectWidth = 10, rectHeight = 10;
float lineWidth = 3.0;
if (pointList.count != 0)
{
NSPoint myPoint = [pointList[0] locationInWindow];
myPoint = [self convertPoint:myPoint fromView:nil];
myPoint.x = (myPoint.x - rectWidth / 2) * widthNow / originalWidth;
myPoint.y = (myPoint.y - rectHeight / 2) * heightNow / originalHeight;
[pathForLine moveToPoint:myPoint];
NSRect anotherRect = NSMakeRect(myPoint.x, myPoint.y, rectWidth, rectHeight);
[[NSColor redColor] set];
[pathForLine setLineWidth:lineWidth + .5 * ((int)lineWidth % 2)];
//[[NSBezierPath bezierPathWithRect:anotherRect] stroke];
for (int i = 1; i < pointList.count; i++)
{
myPoint = [pointList[i] locationInWindow];
myPoint = [self convertPoint:myPoint fromView:nil];
myPoint.x = (myPoint.x - 5) * widthNow / originalWidth;
myPoint.y = (myPoint.y - 5) * heightNow / originalHeight;
anotherRect = NSMakeRect(myPoint.x, myPoint.y, 10, 10);
[pathForLine lineToPoint:myPoint];
[pathForLine stroke];
//[[NSBezierPath bezierPathWithRect:anotherRect] fill];
}
}
}
#pragma mark Mouse events
- (void)mouseUp:(NSEvent *)theEvent
{
[pointList addObject:theEvent];
[self setNeedsDisplay:YES];
}
- (void)mouseExited:(NSEvent *)theEvent
{
[pathForLine closePath];
[pathForLine stroke];
[self setNeedsDisplay:YES];
}
#end

UIScrollView and UIPageControl, what am I doing wrong?

I have a class which is predefining some labels and binding their values in a UIScrollView.
I've managed to show those labels, but now I'm stuck at putting a label at the 2nd part of the ScrollView.
I've pushed my project to gitHub.
I can change the label's place on the already visible part, but I must be overlooking something.
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.title = _detail.name;
UIColor *bgColor = [UIColor blackColor];
UIColor *txtColor = [UIColor grayColor];
CGRect frame;
frame.origin.x = 0;
frame.origin.y = 0;
frame.size.width = _scrollView.frame.size.width *2;
NSString *phoneNr = (_detail.phoneNr == nil) ? #"Not specified" : _detail.phoneNr;
_telLabel = [self prepareLabel:phoneNr textColor:txtColor bgColor:bgColor page:0 y:telNrYAxis];
_webLabel = [self prepareLabel:#"Visit website" textColor:txtColor bgColor:bgColor page:0 y:websiteYAxis];
_detail.address = [_detail.address stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#"\n\t "]];
NSArray *addressArrComponents = [_detail.address componentsSeparatedByString:#","] ;
_addressLabel = [self prepareLabel:[addressArrComponents componentsJoinedByString:#"\n"] textColor:txtColor bgColor:bgColor page:0 y:addressYAxis];
UILabel *lbl = [self prepareLabel:#"Derp" textColor:txtColor bgColor:bgColor page:1 y:0];
_detailView = [[UIView alloc] initWithFrame:frame];
_detailView.backgroundColor = [UIColor blackColor];
[_detailView addSubview:_webLabel];
[_detailView addSubview:_addressLabel];
[_detailView addSubview:_telLabel];
[_detailView addSubview:lbl];
[_scrollView addSubview:_detailView];
NSLog(#"%f",self.view.frame.size.height - (_scrollView.frame.origin.y + _scrollView.frame.size.height) );
_pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2, self.view.frame.size.height - 250 , self.view.frame.size.width/4, 120)];
_pageControl.numberOfPages=2;
_pageControl.currentPage=0;
[_pageControl addTarget:self action:#selector(pageChange:) forControlEvents:UIControlEventTouchDown];
_scrollView.contentSize = CGSizeMake(800,800);
_scrollView.delegate=self;
_scrollView.backgroundColor = [UIColor blackColor];
_scrollView.pagingEnabled=YES;
_scrollView.showsHorizontalScrollIndicator = NO;
_scrollView.showsVerticalScrollIndicator = NO;
_scrollView.scrollsToTop = NO;
[self pageChange:0];
[self.view addSubview:_pageControl];
// Do any additional setup after loading the view.
}
-(UILabel*)prepareLabel:(NSString*) text textColor:(UIColor*)textColor bgColor:(UIColor*)backgroundColor page:(int)page y:(int) yPos{
int lines = [[text componentsSeparatedByString:#"\n"] count];
CGRect labelFrame = CGRectMake(_detailView.frame.size.width * page +20,yPos,self.view.frame.size.width, [UIFont systemFontSize]*lines);
UILabel *returnLabel = [[UILabel alloc] initWithFrame:labelFrame];
returnLabel.text = text;
returnLabel.backgroundColor = backgroundColor;
returnLabel.textColor = textColor;
[returnLabel setNumberOfLines:lines];
[returnLabel sizeToFit];
return returnLabel;
}
- (void)loadScrollViewWithPage:(int)page {
NSLog(#"Derped");
}
-(IBAction)pageChange:(id)sender{
int page=_pageControl.currentPage;
CGRect frame = _scrollView.frame;
frame.origin.x = _scrollView.frame.size.width * page;
frame.origin.y = 0;
//CGRect frame= (page == 0) ? _detailFrame : _reviewFrame;
NSLog(#"%f",frame.origin.x);
[_scrollView scrollRectToVisible:frame animated:YES];
}
The delegate -(IBAction)pageChange:(id)sender gets fired, but I must be doing something wrong with the frames somewhere :s
Please take a look!
Try to implement this method may help you :
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGFloat pageWidth = self.scrollView.frame.size.width;
float fractionalPage = self.scrollView.contentOffset.x / pageWidth;
NSInteger page = lround(fractionalPage);
self.pageControl.currentPage = page;
}

Resizeable Custom NSPanel

OK, here's my situation :
I'm trying a HUD-like custom-controls collection, SNRHUDKit.
I'm specifically using SNRHUDWindow as my main window class
No matter what, although it actually works, I can't get the NSWindow (or NSPanel - doesn't make much difference) to resize, when the user drags its lower-right corner.
The code for SNRHUDWindow is :
//
// SNRHUDWindow.m
// SNRHUDKit
//
// Created by Indragie Karunaratne on 12-01-22.
// Copyright (c) 2012 indragie.com. All rights reserved.
//
#import "SNRHUDWindow.h"
#import "NSBezierPath+MCAdditions.h"
#define SNRWindowTitlebarHeight 22.f
#define SNRWindowBorderColor [NSColor blackColor]
#define SNRWindowTopColor [NSColor colorWithDeviceWhite:0.240 alpha:0.960]
#define SNRWindowBottomColor [NSColor colorWithDeviceWhite:0.150 alpha:0.960]
#define SNRWindowHighlightColor [NSColor colorWithDeviceWhite:1.000 alpha:0.200]
#define SNRWindowCornerRadius 5.f
#define SNRWindowTitleFont [NSFont systemFontOfSize:11.f]
#define SNRWindowTitleColor [NSColor colorWithDeviceWhite:0.700 alpha:1.000]
#define SNRWindowTitleShadowOffset NSMakeSize(0.f, 1.f)
#define SNRWindowTitleShadowBlurRadius 1.f
#define SNRWindowTitleShadowColor [NSColor blackColor]
#define SNRWindowButtonSize NSMakeSize(18.f, 18.f)
#define SNRWindowButtonEdgeMargin 5.f
#define SNRWindowButtonBorderColor [NSColor colorWithDeviceWhite:0.040 alpha:1.000]
#define SNRWindowButtonGradientBottomColor [NSColor colorWithDeviceWhite:0.070 alpha:1.000]
#define SNRWindowButtonGradientTopColor [NSColor colorWithDeviceWhite:0.220 alpha:1.000]
#define SNRWindowButtonDropShadowColor [NSColor colorWithDeviceWhite:1.000 alpha:0.100]
#define SNRWindowButtonCrossColor [NSColor colorWithDeviceWhite:0.450 alpha:1.000]
#define SNRWindowButtonCrossInset 1.f
#define SNRWindowButtonHighlightOverlayColor [NSColor colorWithDeviceWhite:0.000 alpha:0.300]
#define SNRWindowButtonInnerShadowColor [NSColor colorWithDeviceWhite:1.000 alpha:0.100]
#define SNRWindowButtonInnerShadowOffset NSMakeSize(0.f, 0.f)
#define SNRWindowButtonInnerShadowBlurRadius 1.f
#interface SNRHUDWindowButtonCell : NSButtonCell
#end
#interface SNRHUDWindowFrameView : NSView
- (void)snr_drawTitleInRect:(NSRect)rect;
#end
#implementation SNRHUDWindow {
NSView *__customContentView;
}
- (id)initWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)windowStyle backing:(NSBackingStoreType)bufferingType defer:(BOOL)deferCreation
{
if ((self = [super initWithContentRect:contentRect styleMask:NSBorderlessWindowMask backing:bufferingType defer:deferCreation])) {
[self setOpaque:NO];
[self setBackgroundColor:[NSColor clearColor]];
[self setMovableByWindowBackground:YES];
[self setLevel:NSFloatingWindowLevel];
}
return self;
}
- (NSRect)contentRectForFrameRect:(NSRect)windowFrame
{
windowFrame.origin = NSZeroPoint;
windowFrame.size.height -= SNRWindowTitlebarHeight;
return windowFrame;
}
+ (NSRect)frameRectForContentRect:(NSRect)windowContentRect
styleMask:(NSUInteger)windowStyle
{
windowContentRect.size.height += SNRWindowTitlebarHeight;
return windowContentRect;
}
- (NSRect)frameRectForContentRect:(NSRect)windowContent
{
windowContent.size.height += SNRWindowTitlebarHeight;
return windowContent;
}
- (void)setContentView:(NSView *)aView
{
if ([__customContentView isEqualTo:aView]) {
return;
}
NSRect bounds = [self frame];
bounds.origin = NSZeroPoint;
SNRHUDWindowFrameView *frameView = [super contentView];
if (!frameView) {
frameView = [[SNRHUDWindowFrameView alloc] initWithFrame:bounds];
NSSize buttonSize = SNRWindowButtonSize;
NSRect buttonRect = NSMakeRect(SNRWindowButtonEdgeMargin, NSMaxY(frameView.bounds) -(SNRWindowButtonEdgeMargin + buttonSize.height), buttonSize.width, buttonSize.height);
NSButton *closeButton = [[NSButton alloc] initWithFrame:buttonRect];
[closeButton setCell:[[SNRHUDWindowButtonCell alloc] init]];
[closeButton setButtonType:NSMomentaryChangeButton];
[closeButton setTarget:self];
[closeButton setAction:#selector(close)];
[closeButton setAutoresizingMask:NSViewMaxXMargin | NSViewMinYMargin];
[frameView addSubview:closeButton];
[super setContentView:frameView];
}
if (__customContentView) {
[__customContentView removeFromSuperview];
}
__customContentView = aView;
[__customContentView setFrame:[self contentRectForFrameRect:bounds]];
[__customContentView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
[frameView addSubview:__customContentView];
}
- (NSView *)contentView
{
return __customContentView;
}
- (void)setTitle:(NSString *)aString
{
[super setTitle:aString];
[[super contentView] setNeedsDisplay:YES];
}
- (BOOL)canBecomeKeyWindow
{
return YES;
}
#end
#implementation SNRHUDWindowFrameView
- (void)drawRect:(NSRect)dirtyRect
{
NSRect drawingRect = NSInsetRect(self.bounds, 0.5f, 0.5f);
NSBezierPath *path = [NSBezierPath bezierPathWithRoundedRect:drawingRect xRadius:SNRWindowCornerRadius yRadius:SNRWindowCornerRadius];
[NSGraphicsContext saveGraphicsState];
[path addClip];
// Fill in the title bar with a gradient background
NSRect titleBarRect = NSMakeRect(0.f, NSMaxY(self.bounds) - SNRWindowTitlebarHeight, self.bounds.size.width, SNRWindowTitlebarHeight);
NSGradient *titlebarGradient = [[NSGradient alloc] initWithStartingColor:SNRWindowBottomColor endingColor:SNRWindowTopColor];
[titlebarGradient drawInRect:titleBarRect angle:90.f];
// Draw the window title
[self snr_drawTitleInRect:titleBarRect];
// Rest of the window has a solid fill
NSRect bottomRect = NSMakeRect(0.f, 0.f, self.bounds.size.width, self.bounds.size.height - SNRWindowTitlebarHeight);
[SNRWindowBottomColor set];
[NSBezierPath fillRect:bottomRect];
// Draw the highlight line around the top edge of the window
// Outset the width of the rectangle by 0.5px so that the highlight "bleeds" around the rounded corners
// Outset the height by 1px so that the line is drawn right below the border
NSRect highlightRect = NSInsetRect(drawingRect, 0.f, 0.5f);
// Make the height of the highlight rect something bigger than the bounds so that it won't show up on the bottom
highlightRect.size.height += 50.f;
highlightRect.origin.y -= 50.f;
NSBezierPath *highlightPath = [NSBezierPath bezierPathWithRoundedRect:highlightRect xRadius:SNRWindowCornerRadius yRadius:SNRWindowCornerRadius];
[SNRWindowHighlightColor set];
[highlightPath stroke];
[NSGraphicsContext restoreGraphicsState];
[SNRWindowBorderColor set];
[path stroke];
}
- (void)snr_drawTitleInRect:(NSRect)titleBarRect
{
NSString *title = [[self window] title];
if (!title) { return; }
NSShadow *shadow = [NSShadow new];
[shadow setShadowColor:SNRWindowTitleShadowColor];
[shadow setShadowOffset:SNRWindowTitleShadowOffset];
[shadow setShadowBlurRadius:SNRWindowTitleShadowBlurRadius];
NSMutableParagraphStyle *style = [NSMutableParagraphStyle new];
[style setAlignment:NSCenterTextAlignment];
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:SNRWindowTitleColor, NSForegroundColorAttributeName, SNRWindowTitleFont, NSFontAttributeName, shadow, NSShadowAttributeName, style, NSParagraphStyleAttributeName, nil];
NSAttributedString *attrTitle = [[NSAttributedString alloc] initWithString:title attributes:attributes];
NSSize titleSize = attrTitle.size;
NSRect titleRect = NSMakeRect(0.f, NSMidY(titleBarRect) - (titleSize.height / 2.f), titleBarRect.size.width, titleSize.height);
[attrTitle drawInRect:NSIntegralRect(titleRect)];
}
#end
#implementation SNRHUDWindowButtonCell
- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
NSRect drawingRect = NSInsetRect(cellFrame, 1.5f, 1.5f);
drawingRect.origin.y = 0.5f;
NSRect dropShadowRect = drawingRect;
dropShadowRect.origin.y += 1.f;
// Draw the drop shadow so that the bottom edge peeks through
NSBezierPath *dropShadow = [NSBezierPath bezierPathWithOvalInRect:dropShadowRect];
[SNRWindowButtonDropShadowColor set];
[dropShadow stroke];
// Draw the main circle w/ gradient & border on top of it
NSBezierPath *circle = [NSBezierPath bezierPathWithOvalInRect:drawingRect];
NSGradient *gradient = [[NSGradient alloc] initWithStartingColor:SNRWindowButtonGradientBottomColor endingColor:SNRWindowButtonGradientTopColor];
[gradient drawInBezierPath:circle angle:270.f];
[SNRWindowButtonBorderColor set];
[circle stroke];
// Draw the cross
NSBezierPath *cross = [NSBezierPath bezierPath];
CGFloat boxDimension = floor(drawingRect.size.width * cos(45.f)) - SNRWindowButtonCrossInset;
CGFloat origin = round((drawingRect.size.width - boxDimension) / 2.f);
NSRect boxRect = NSMakeRect(1.f + origin, origin, boxDimension, boxDimension);
NSPoint bottomLeft = NSMakePoint(boxRect.origin.x, NSMaxY(boxRect));
NSPoint topRight = NSMakePoint(NSMaxX(boxRect), boxRect.origin.y);
NSPoint bottomRight = NSMakePoint(topRight.x, bottomLeft.y);
NSPoint topLeft = NSMakePoint(bottomLeft.x, topRight.y);
[cross moveToPoint:bottomLeft];
[cross lineToPoint:topRight];
[cross moveToPoint:bottomRight];
[cross lineToPoint:topLeft];
[SNRWindowButtonCrossColor set];
[cross setLineWidth:2.f];
[cross stroke];
// Draw the inner shadow
NSShadow *shadow = [[NSShadow alloc] init];
[shadow setShadowColor:SNRWindowButtonInnerShadowColor];
[shadow setShadowBlurRadius:SNRWindowButtonInnerShadowBlurRadius];
[shadow setShadowOffset:SNRWindowButtonInnerShadowOffset];
NSRect shadowRect = drawingRect;
shadowRect.size.height = origin;
[NSGraphicsContext saveGraphicsState];
[NSBezierPath clipRect:shadowRect];
[circle fillWithInnerShadow:shadow];
[NSGraphicsContext restoreGraphicsState];
if ([self isHighlighted]) {
[SNRWindowButtonHighlightOverlayColor set];
[circle fill];
}
}
#end
Any ideas what could be responsible for the NSPanel losing its resizing ability?
I'm using this framework as well, and the reason that resizing doesn't work by default is this line in the initWithContentRect method:
if ((self = [super initWithContentRect:contentRect styleMask:NSBorderlessWindowMask backing:bufferingType defer:deferCreation])) {
As you can see, instead of passing the windowStyle bitmask provided to super's init method, it passes through just NSBorderlessWindowMask. A bit of sniffing around shows that for resizing to be possible at all, the styleMask must have NSResizableWindowMask included in the bitmask.
So, changing the line to
if ((self = [super initWithContentRect:contentRect styleMask:NSBorderlessWindowMask|NSResizableWindowMask backing:bufferingType defer:deferCreation])) {
should solve your problem.

UITabbarItem BadgeValue Text Color

I have a problem in my App. I set a badge value at one of the tabs in the UITabBar. The Badge value is correctly red and the circle around the badge value is correctly in white. The problem is, that the color of the text is gray (160, 160, 160). It is the same color like the normal state tabbaritem text is, but I set this color nowhere in the app code and I do not know where this color come from.
I searched for that issue in the whole net since weeks but I cannot find any solution. The only answer I found everywhere is, that it is not possible to change the color of the text of the badge value. But if it is not possible, why is it changed in my app?
I hope, that somebody can help me with that issue...
http://www.luventas-webdesign.de/stackoverflow/screenshot_badgevalue.png
Like the color is in my app
http://www.luventas-webdesign.de/stackoverflow/screenshot_like_it_should.png
Like the color should normally be...
Edit 02.11.2012 - Code
Creation of TabBarController:
#import "ExtendedTabBarController.h"
#import "configuration.h"
#implementation ExtendedTabBarController
- (void)viewDidLoad {
[super viewDidLoad];
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIColor colorWithRed:207.0/255.0 green:70.0/255.0 blue:61.0/255.0 alpha:1], UITextAttributeTextColor, [UIFont fontWithName:#"KievitPro-Regular" size:10.0], UITextAttributeFont, nil] forState:UIControlStateSelected];
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1], UITextAttributeTextColor, [UIFont fontWithName:#"KievitPro-Regular" size:10.0], UITextAttributeFont, nil] forState:UIControlStateNormal];
[self.tabBar sizeToFit];
UIView *tabbarBackgroundColorView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0, self.view.bounds.size.width, 49)];
[tabbarBackgroundColorView setBackgroundColor:[UIColor colorWithRed:233.0/255.0 green:233.0/255.0 blue:233.0/255.0 alpha:1]];
[self.tabBar insertSubview:tabbarBackgroundColorView atIndex:0];
}
- (void)viewDidUnload {
[super viewDidUnload];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return UIInterfaceOrientationIsPortrait(interfaceOrientation); // only portrait orientation
}
/**
* orientation for iOS6
**/
-(NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskPortrait;
}
#end
Call in AppDelegate:
ExtendedTabBarController *tabBarController = [[ExtendedTabBarController alloc] init];
[self setTabBarController:tabBarController];
[[UITabBar appearance] setBackgroundImage:[UIImage imageNamed:#"menu_bg"]];
// code for initialize View- and NavigationControllers...
self.tabBarController.viewControllers = #[highlightsNavigationController, categoryNavigationController, searchNavigationController, favoritesNavigationController, imprintNavigationController];
self.window.rootViewController = self.tabBarController;
[[UITabBar appearance] setSelectionIndicatorImage:[[UIImage alloc] init]];
Set the badge value:
int viewCount = 0;
NSUserDefaults * defs = [NSUserDefaults standardUserDefaults];
NSDictionary * dict = [defs dictionaryRepresentation];
for (id key in dict) {
if([key rangeOfString:#"_highlighted"].location != NSNotFound && [[[dict objectForKey:key] objectAtIndex:0] isEqualToString:#"YES"]) {
viewCount++;
}
}
UITabBarItem *tbi = (UITabBarItem *)[self.tabBarController.tabBar.items objectAtIndex:3];
if(viewCount <= 0) {
tbi.badgeValue = nil;
} else {
tbi.badgeValue = nil;
tbi.badgeValue = [NSString stringWithFormat:#"%d", viewCount];
}
Code for overwritten UILabel:
// -- file: UILabel+VerticalAlign.h
#pragma mark VerticalAlign
#interface UILabel (VerticalAlign)
- (void)alignTop;
- (void)alignBottom;
- (void)awakeFromNib;
-(id)initWithFrame:(CGRect)frame;
#end
#import "UILabel+VerticalAlign.h"
// -- file: UILabel+VerticalAlign.m
#implementation UILabel (VerticalAlign)
- (void)alignTop {
CGSize fontSize = [self.text sizeWithFont:self.font];
double finalHeight = fontSize.height * self.numberOfLines;
double finalWidth = self.frame.size.width; //expected width of label
CGSize theStringSize = [self.text sizeWithFont:self.font constrainedToSize:CGSizeMake(finalWidth, finalHeight) lineBreakMode:self.lineBreakMode];
int newLinesToPad = (finalHeight - theStringSize.height) / fontSize.height;
for(int i=0; i<newLinesToPad; i++)
self.text = [self.text stringByAppendingString:#"\n "];
}
- (void)alignBottom {
CGSize fontSize = [self.text sizeWithFont:self.font];
double finalHeight = fontSize.height * self.numberOfLines;
double finalWidth = self.frame.size.width; //expected width of label
CGSize theStringSize = [self.text sizeWithFont:self.font constrainedToSize:CGSizeMake(finalWidth, finalHeight) lineBreakMode:self.lineBreakMode];
int newLinesToPad = (finalHeight - theStringSize.height) / fontSize.height;
for(int i=0; i<newLinesToPad; i++)
self.text = [NSString stringWithFormat:#" \n%#",self.text];
}
- (void)awakeFromNib
{
[super awakeFromNib];
[self setFont:[UIFont fontWithName:#"KievitPro-Regular" size:12.0]];
}
-(id)initWithFrame:(CGRect)frame
{
id result = [super initWithFrame:frame];
if (result) {
[self setFont:[UIFont fontWithName:#"KievitPro-Regular" size:12.0]];
}
return result;
}
#end
I found a solution for my problem on my own:
I must remove the following lines from the overwritten UILabel:
- (void)awakeFromNib
{
[super awakeFromNib];
[self setFont:[UIFont fontWithName:#"KievitPro-Regular" size:12.0]];
}
-(id)initWithFrame:(CGRect)frame
{
id result = [super initWithFrame:frame];
if (result) {
[self setFont:[UIFont fontWithName:#"KievitPro-Regular" size:12.0]];
}
return result;
}
Maybe someone can explain me, why this lines change the text color of the badge value, before we can close this post?
Instead of setting the default UILabel font using a category, use the UILabel's appearance method to set the font:
[[UILabel appearance] setFont:[UIFont fontWithName:#"KievitPro-Regular" size:12.0]];
When I tested this the text for the badge appeared as the normal white color.