Skip to content

Commit 7dd12c7

Browse files
committed
Added the ability to set a custom view instead of the text (for example an UIImageView)
1 parent fe928c4 commit 7dd12c7

File tree

4 files changed

+59
-9
lines changed

4 files changed

+59
-9
lines changed

src/NSBubbleData.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@ typedef enum _NSBubbleType
2424
@property (readonly, nonatomic, strong) NSDate *date;
2525
@property (readonly, nonatomic) NSBubbleType type;
2626
@property (readonly, nonatomic, strong) NSString *text;
27+
@property (readonly, nonatomic, strong) UIView *view;
2728

2829
- (id)initWithText:(NSString *)text andDate:(NSDate *)date andType:(NSBubbleType)type;
2930
+ (id)dataWithText:(NSString *)text andDate:(NSDate *)date andType:(NSBubbleType)type;
31+
- (id)initWithView:(UIView *)view andDate:(NSDate *)date andType:(NSBubbleType)type;
32+
+ (id)dataWithView:(UIView *)view andDate:(NSDate *)date andType:(NSBubbleType)type;
3033

3134
@end

src/NSBubbleData.m

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ @implementation NSBubbleData
1818
@synthesize date = _date;
1919
@synthesize type = _type;
2020
@synthesize text = _text;
21+
@synthesize view = _view;
2122

2223
+ (id)dataWithText:(NSString *)text andDate:(NSDate *)date andType:(NSBubbleType)type
2324
{
@@ -38,12 +39,30 @@ - (id)initWithText:(NSString *)initText andDate:(NSDate *)initDate andType:(NSBu
3839
return self;
3940
}
4041

42+
+ (id)dataWithView:(UIView *)view andDate:(NSDate *)date andType:(NSBubbleType)type
43+
{
44+
return [[[NSBubbleData alloc] initWithView:view andDate:date andType:type] autorelease];
45+
}
46+
47+
- (id)initWithView:(UIView *)view andDate:(NSDate *)date andType:(NSBubbleType)type
48+
{
49+
self = [super init];
50+
if (self) {
51+
_view = [view retain];
52+
_date = [date retain];
53+
_type = type;
54+
}
55+
return self;
56+
}
57+
4158
- (void)dealloc
4259
{
4360
[_date release];
4461
_date = nil;
4562
[_text release];
4663
_text = nil;
64+
[_view release];
65+
_view = nil;
4766
[super dealloc];
4867
}
4968

src/UIBubbleTableView.m

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,16 @@ - (void)reloadData
129129
dataInternal.type = NSBubbleDataTypeNormalBubble;
130130

131131
// Calculating cell height
132-
dataInternal.labelSize = [(dataInternal.data.text ? dataInternal.data.text : @"") sizeWithFont:[UIFont systemFontOfSize:[UIFont systemFontSize]] constrainedToSize:CGSizeMake(220, 9999) lineBreakMode:UILineBreakModeWordWrap];
132+
CGFloat dataHeight = 0.0;
133+
if (dataInternal.data.text)
134+
{
135+
dataInternal.labelSize = [(dataInternal.data.text ? dataInternal.data.text : @"") sizeWithFont:[UIFont systemFontOfSize:[UIFont systemFontSize]] constrainedToSize:CGSizeMake(220, 9999) lineBreakMode:UILineBreakModeWordWrap];
136+
dataHeight = dataInternal.labelSize.height;
137+
}
138+
else
139+
dataHeight = CGRectGetHeight(dataInternal.data.view.frame)+12.0 /*cf offsetY in cell. this not really cool implementation all these values*/;
133140

134-
dataInternal.height = dataInternal.labelSize.height + 5 + 11;
141+
dataInternal.height = dataHeight + 5 + 11;
135142

136143
dataInternal.header = nil;
137144

src/UIBubbleTableViewCell.m

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@
1717

1818
@interface UIBubbleTableViewCell ()
1919
- (void) setupInternalData;
20+
@property (nonatomic, retain) UIView *oldCustomView;
2021
@end
2122

2223
@implementation UIBubbleTableViewCell
2324

2425
@synthesize dataInternal = _dataInternal;
26+
@synthesize oldCustomView = _oldCustomView;
2527

2628
- (void)setFrame:(CGRect)frame
2729
{
@@ -33,6 +35,8 @@ - (void) dealloc
3335
{
3436
[_dataInternal release];
3537
_dataInternal = nil;
38+
[_oldCustomView release];
39+
_oldCustomView = nil;
3640
[super dealloc];
3741
}
3842

@@ -58,22 +62,39 @@ - (void) setupInternalData
5862
}
5963

6064
NSBubbleType type = self.dataInternal.data.type;
65+
CGFloat imageOffsetX = 5.0;
66+
CGFloat imageOffsetY = 6.0;
6167

62-
float x = (type == BubbleTypeSomeoneElse) ? 20 : self.frame.size.width - 20 - self.dataInternal.labelSize.width;
68+
CGFloat height = self.dataInternal.data.view ? (CGRectGetHeight(self.dataInternal.data.view.frame) + imageOffsetY*2.0) : self.dataInternal.labelSize.height;
69+
CGFloat width = self.dataInternal.data.view ? (CGRectGetWidth(self.dataInternal.data.view.frame) + imageOffsetX*2.0) : self.dataInternal.labelSize.width;
70+
71+
float x = (type == BubbleTypeSomeoneElse) ? 20 : self.frame.size.width - 20 - width;
6372
float y = 5 + (self.dataInternal.header ? 30 : 0);
6473

65-
contentLabel.font = [UIFont systemFontOfSize:[UIFont systemFontSize]];
66-
contentLabel.frame = CGRectMake(x, y, self.dataInternal.labelSize.width, self.dataInternal.labelSize.height);
67-
contentLabel.text = self.dataInternal.data.text;
68-
74+
[self.oldCustomView removeFromSuperview];
75+
self.oldCustomView = nil;
76+
77+
if (!self.dataInternal.data.view) {
78+
contentLabel.hidden = NO;
79+
contentLabel.font = [UIFont systemFontOfSize:[UIFont systemFontSize]];
80+
contentLabel.frame = CGRectMake(x, y, self.dataInternal.labelSize.width, self.dataInternal.labelSize.height);
81+
contentLabel.text = self.dataInternal.data.text;
82+
}
83+
else
84+
{
85+
contentLabel.hidden = YES;
86+
self.oldCustomView = self.dataInternal.data.view;
87+
[self.contentView addSubview:self.oldCustomView];
88+
self.oldCustomView.frame = CGRectMake(x+imageOffsetX-1.0, y+imageOffsetY+3.0, CGRectGetWidth(self.oldCustomView.frame), CGRectGetHeight(self.oldCustomView.frame));
89+
}
6990
if (type == BubbleTypeSomeoneElse)
7091
{
7192
bubbleImage.image = [[UIImage imageNamed:@"bubbleSomeone.png"] stretchableImageWithLeftCapWidth:21 topCapHeight:14];
72-
bubbleImage.frame = CGRectMake(x - 18, y - 4, self.dataInternal.labelSize.width + 30, self.dataInternal.labelSize.height + 15);
93+
bubbleImage.frame = CGRectMake(x - 18, y - 4, width + 30, height + 15);
7394
}
7495
else {
7596
bubbleImage.image = [[UIImage imageNamed:@"bubbleMine.png"] stretchableImageWithLeftCapWidth:15 topCapHeight:14];
76-
bubbleImage.frame = CGRectMake(x - 9, y - 4, self.dataInternal.labelSize.width + 26, self.dataInternal.labelSize.height + 15);
97+
bubbleImage.frame = CGRectMake(x - 9, y - 4, width + 26, height + 15);
7798
}
7899
}
79100

0 commit comments

Comments
 (0)