root/apps/iphone/superbook/trunk/HABadgeViewController/HABadgeViewController.m
| Revision 885, 5.1 kB (checked in by henri, 22 months ago) |
|---|
| Line | |
|---|---|
| 1 | // |
| 2 | // HABadgeViewController.m |
| 3 | // LocateThem |
| 4 | // |
| 5 | // Created by Henri Asseily on 6/4/10. |
| 6 | /* |
| 7 | Copyright (c) 2008-2010, Telnic Ltd. All rights reserved. |
| 8 | |
| 9 | Redistribution and use in source and binary forms, with or without modification, |
| 10 | are permitted provided that the following conditions are met: |
| 11 | |
| 12 | Redistributions of source code must retain the above copyright notice, this list of conditions |
| 13 | and the following disclaimer. Redistributions in binary form must reproduce the above copyright |
| 14 | notice, this list of conditions and the following disclaimer in the documentation and/or other |
| 15 | materials provided with the distribution. |
| 16 | Neither the name of the Telnic Ltd. nor the names of its contributors may be used to endorse or |
| 17 | promote products derived from this software without specific prior written permission. |
| 18 | THIS DOCUMENTATION IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS |
| 19 | OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY |
| 20 | AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 21 | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 22 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER |
| 24 | IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 25 | OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | // |
| 28 | |
| 29 | #import "HABadgeViewController.h" |
| 30 | |
| 31 | #define HABWIDTH 18.0 // Badge default width, which is the same as the height |
| 32 | #define HABVALUETAG 31318 |
| 33 | |
| 34 | @implementation HABadgeViewController |
| 35 | |
| 36 | @synthesize parentView; |
| 37 | @synthesize badgeLabel; |
| 38 | |
| 39 | + (void)setBadgeValue:(NSUInteger)aValue forView:(UIView *)aView { |
| 40 | UIView *badgeView = [aView viewWithTag:HABADGEVIEWTAG]; |
| 41 | if (! badgeView) { |
| 42 | HABadgeViewController *bVC = [[[HABadgeViewController alloc] init] autorelease]; |
| 43 | badgeView = bVC.view; |
| 44 | [aView addSubview:badgeView]; |
| 45 | } |
| 46 | // Resize the badge horizontally to fit the new text |
| 47 | CGFloat bW = HABWIDTH + 10*(aValue/10); |
| 48 | badgeView.frame = CGRectMake(aView.frame.size.width-bW, 0, bW, HABWIDTH); |
| 49 | |
| 50 | // Change the badge text |
| 51 | UILabel *badgeValue = (UILabel *)[badgeView viewWithTag:HABVALUETAG]; |
| 52 | badgeValue.text = [NSString stringWithFormat:@"%d", aValue]; |
| 53 | } |
| 54 | |
| 55 | + (void)removeBadgeFromView:(UIView *)aView { |
| 56 | UIView *badgeView = [aView viewWithTag:HABADGEVIEWTAG]; |
| 57 | if (badgeView) { |
| 58 | [badgeView removeFromSuperview]; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | - (void)loadView { |
| 63 | UILabel *badgeValue = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, HABWIDTH, HABWIDTH)]; |
| 64 | badgeValue.adjustsFontSizeToFitWidth = NO; |
| 65 | badgeValue.alpha = 1.000; |
| 66 | badgeValue.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin; |
| 67 | badgeValue.baselineAdjustment = UIBaselineAdjustmentAlignCenters; |
| 68 | badgeValue.clearsContextBeforeDrawing = YES; |
| 69 | badgeValue.clipsToBounds = YES; |
| 70 | badgeValue.contentMode = UIViewContentModeCenter; |
| 71 | badgeValue.enabled = YES; |
| 72 | badgeValue.font = [UIFont fontWithName:@"Helvetica-Bold" size:10.000]; |
| 73 | badgeValue.hidden = NO; |
| 74 | badgeValue.lineBreakMode = UILineBreakModeTailTruncation; |
| 75 | badgeValue.minimumFontSize = 10.000; |
| 76 | badgeValue.multipleTouchEnabled = NO; |
| 77 | badgeValue.numberOfLines = 1; |
| 78 | badgeValue.opaque = NO; |
| 79 | badgeValue.shadowOffset = CGSizeMake(0.0, -1.0); |
| 80 | badgeValue.tag = 0; |
| 81 | badgeValue.textAlignment = UITextAlignmentCenter; |
| 82 | badgeValue.textColor = [UIColor colorWithWhite:1.000 alpha:1.000]; |
| 83 | badgeValue.userInteractionEnabled = NO; |
| 84 | badgeValue.text = @""; |
| 85 | |
| 86 | UIImageView *badgeImageView = [[[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, HABWIDTH, HABWIDTH)] autorelease]; |
| 87 | badgeImageView.frame = CGRectMake(0.0, 0.0, HABWIDTH, HABWIDTH); |
| 88 | badgeImageView.alpha = 1.000; |
| 89 | badgeImageView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin; |
| 90 | badgeImageView.backgroundColor = [UIColor colorWithWhite:1.000 alpha:1.000]; |
| 91 | badgeImageView.clearsContextBeforeDrawing = YES; |
| 92 | badgeImageView.clipsToBounds = NO; |
| 93 | badgeImageView.contentMode = UIViewContentModeScaleToFill; |
| 94 | badgeImageView.hidden = NO; |
| 95 | badgeImageView.image = nil; |
| 96 | badgeImageView.multipleTouchEnabled = NO; |
| 97 | badgeImageView.opaque = NO; |
| 98 | badgeImageView.tag = 0; |
| 99 | badgeImageView.userInteractionEnabled = NO; |
| 100 | badgeImageView.contentStretch = CGRectMake(0.5, 0.5, 0.05, 0.05); |
| 101 | badgeImageView.tag = HABADGEVIEWTAG; |
| 102 | |
| 103 | [badgeImageView addSubview:badgeValue]; |
| 104 | self.view = badgeImageView; |
| 105 | badgeLabel = badgeValue; |
| 106 | } |
| 107 | |
| 108 | |
| 109 | - (void)didReceiveMemoryWarning { |
| 110 | // Releases the view if it doesn't have a superview. |
| 111 | [super didReceiveMemoryWarning]; |
| 112 | |
| 113 | // Release any cached data, images, etc that aren't in use. |
| 114 | } |
| 115 | |
| 116 | - (void)viewDidUnload { |
| 117 | if (badgeLabel) { |
| 118 | [badgeLabel release]; |
| 119 | } |
| 120 | [super viewDidUnload]; |
| 121 | } |
| 122 | |
| 123 | |
| 124 | - (void)dealloc { |
| 125 | if (badgeLabel) { |
| 126 | [badgeLabel release]; |
| 127 | } |
| 128 | if (parentView) { |
| 129 | [parentView release]; |
| 130 | } |
| 131 | [super dealloc]; |
| 132 | } |
| 133 | |
| 134 | |
| 135 | @end |
Note: See TracBrowser
for help on using the browser.








