]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - sensortag-ios-source-code-example/sensortag-ios-source-code-example.git/blob - SensorTag_iOS/SensorTag2-Example/displayTile.m
V1 Release
[sensortag-ios-source-code-example/sensortag-ios-source-code-example.git] / SensorTag_iOS / SensorTag2-Example / displayTile.m
1 /*!
2  *  \author         Ole A. Torvmark <o.a.torvmark@ti.com , torvmark@stalliance.no>
3  *  \brief          Basic graphics element SensorTag2-Example iOS application
4  *  \copyright      Copyright (c) 2015 Texas Instruments Incorporated
5  *  \file           displayTile.m
6  */
8 /*
9  * Copyright (c) 2015 Texas Instruments Incorporated
10  *
11  * All rights reserved not granted herein.
12  * Limited License.
13  *
14  * Texas Instruments Incorporated grants a world-wide, royalty-free,
15  * non-exclusive license under copyrights and patents it now or hereafter
16  * owns or controls to make, have made, use, import, offer to sell and sell ("Utilize")
17  * this software subject to the terms herein.  With respect to the foregoing patent
18  *license, such license is granted  solely to the extent that any such patent is necessary
19  * to Utilize the software alone.  The patent license shall not apply to any combinations which
20  * include this software, other than combinations with devices manufactured by or for TI (“TI Devices”).
21  * No hardware patent is licensed hereunder.
22  *
23  * Redistributions must preserve existing copyright notices and reproduce this license (including the
24  * above copyright notice and the disclaimer and (if applicable) source code license limitations below)
25  * in the documentation and/or other materials provided with the distribution
26  *
27  * Redistribution and use in binary form, without modification, are permitted provided that the following
28  * conditions are met:
29  *
30  *   * No reverse engineering, decompilation, or disassembly of this software is permitted with respect to any
31  *     software provided in binary form.
32  *   * any redistribution and use are licensed by TI for use only with TI Devices.
33  *   * Nothing shall obligate TI to provide you with source code for the software licensed and provided to you in object code.
34  *
35  * If software source code is provided to you, modification and redistribution of the source code are permitted
36  * provided that the following conditions are met:
37  *
38  *   * any redistribution and use of the source code, including any resulting derivative works, are licensed by
39  *     TI for use only with TI Devices.
40  *   * any redistribution and use of any object code compiled from the source code and any resulting derivative
41  *     works, are licensed by TI for use only with TI Devices.
42  *
43  * Neither the name of Texas Instruments Incorporated nor the names of its suppliers may be used to endorse or
44  * promote products derived from this software without specific prior written permission.
45  *
46  * DISCLAIMER.
47  *
48  * THIS SOFTWARE IS PROVIDED BY TI AND TI’S LICENSORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
49  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
50  * IN NO EVENT SHALL TI AND TI’S LICENSORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
51  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
52  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
53  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
54  * POSSIBILITY OF SUCH DAMAGE.
55  */
57 #import "displayTile.h"
59 @implementation autoSizeLabel
61 -(void) setText:(NSString *)text {
62     self.font = [UIFont fontWithName:@"Menlo" size:[autoSizeLabel getSizeOfFontFromFrame:self.frame andString:text]];
63     [super setText:text];
64 }
66 +(CGFloat) getSizeOfFontFromFrame:(CGRect) frame andString:(NSString *) string {
67     CGFloat maxFontSize = 40;
68     UILabel *fakeLabel = [[UILabel alloc] init];
69     fakeLabel.text = string;
70     //Width
71     while ([fakeLabel.text sizeWithAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"Menlo" size:maxFontSize]}].width > frame.size.width)
72     {
73         maxFontSize -= 5;
74     }
75     //Height
76     while ([fakeLabel.text sizeWithAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"Menlo" size:maxFontSize]}].height > frame.size.height)
77     {
78         maxFontSize -= 5;
79     }
80     return maxFontSize;
81 }
83 @end
86 @implementation displayTile
88 -(instancetype) initWithOrigin:(CGPoint) origin andSize:(CGSize)size {
89     self = [super init];
90     if (self) {
91         self.origin = origin;
92         self.size = size;
93         self.layer.cornerRadius = 10.0f;
94         self.layer.masksToBounds = YES;
95         self.title = [[autoSizeLabel alloc] init];
96         self.title.text = @"Title";
97         self.title.backgroundColor = [UIColor clearColor];
98         self.title.textColor = [UIColor whiteColor];
99         [self addSubview:self.title];
100         [self setFrame:CGRectMake(0, 0, 0, 0)];
101     }
102     return self;
105 -(void) setFrame:(CGRect)frame {
106     self.tileSize = (frame.size.width) / 8;
107     if ((self.tileSize * 13) > frame.size.height) self.tileSize = (frame.size.height - 40) / 13;
108     self.tileXOffset = (frame.size.width - (self.tileSize * 8)) / 2;
109     NSLog(@"TileSize :%0.1f",self.tileSize);
110     [super setFrame:CGRectMake((self.origin.x * self.tileSize + 3.0f) + self.tileXOffset, self.origin.y * self.tileSize + 3.0f, self.size.width * self.tileSize - 6.0f, self.size.height * self.tileSize - 6.0f)];
111     self.title.frame = CGRectMake(8, 2, (self.size.width * self.tileSize) - 6.0 - 8, 30 - 4);
114 -(void) drawRect:(CGRect)rect {
115     CGContextRef context = UIGraphicsGetCurrentContext();
116     
117     CGFloat titleHeight = 30.0f / (self.size.height * self.tileSize);
118     
119     CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
120     CGFloat locations[] = { 0.0,titleHeight,titleHeight, 1.0 };
121     
122     NSArray *colors = @[(__bridge id) [UIColor blackColor].CGColor, (__bridge id) [UIColor blackColor].CGColor, (__bridge id) [UIColor grayColor].CGColor , (__bridge id) [UIColor grayColor].CGColor];
123     
124     CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef) colors, locations);
125     CGPoint startPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMinY(rect));
126     CGPoint endPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMaxY(rect));
127     
128     CGContextSaveGState(context);
129     CGContextAddRect(context, rect);
130     CGContextClip(context);
131     CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0);
132     CGContextRestoreGState(context);
133     
134     CGGradientRelease(gradient);
135     CGColorSpaceRelease(colorSpace);
136     
140 /*
141 // Only override drawRect: if you perform custom drawing.
142 // An empty implementation adversely affects performance during animation.
143 - (void)drawRect:(CGRect)rect {
144     // Drawing code
146 */
150 @end