[ios] SearchView improvements

This commit is contained in:
Igor Khmurets 2014-05-20 23:17:12 +03:00 committed by Alex Zolotarev
parent f12e3a19f9
commit 3e7701a1da
3 changed files with 68 additions and 27 deletions

View file

@ -5,6 +5,7 @@
@interface SearchUniversalCell : SearchCell
@property (nonatomic, readonly) UILabel * titleLabel;
@property (nonatomic, readonly) UILabel * typeLabel;
@property (nonatomic, readonly) UILabel * subtitleLabel;
@property (nonatomic, readonly) UILabel * distanceLabel;
@property (nonatomic, readonly) UIImageView * iconImageView;
@ -12,6 +13,6 @@
- (void)setTitle:(NSString *)title selectedRange:(NSRange)selectedRange;
- (void)setSubtitle:(NSString *)subtitle selectedRange:(NSRange)selectedRange;
+ (CGFloat)cellHeightWithTitle:(NSString *)title subtitle:(NSString *)subtitle distance:(NSString *)distance viewWidth:(CGFloat)width;
+ (CGFloat)cellHeightWithTitle:(NSString *)title type:(NSString *)type subtitle:(NSString *)subtitle distance:(NSString *)distance viewWidth:(CGFloat)width;
@end

View file

@ -5,6 +5,7 @@
@interface SearchUniversalCell ()
@property (nonatomic) UILabel * titleLabel;
@property (nonatomic) UILabel * typeLabel;
@property (nonatomic) UILabel * subtitleLabel;
@property (nonatomic) UILabel * distanceLabel;
@property (nonatomic) UIImageView * iconImageView;
@ -18,6 +19,7 @@
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
[self.contentView addSubview:self.titleLabel];
[self.contentView addSubview:self.typeLabel];
[self.contentView addSubview:self.subtitleLabel];
[self.contentView addSubview:self.iconImageView];
[self.contentView addSubview:self.distanceLabel];
@ -25,11 +27,12 @@
return self;
}
#define DISTANCE_FONT [UIFont fontWithName:@"HelveticaNeue" size:10]
#define TITLE_FONT [UIFont fontWithName:@"HelveticaNeue" size:17]
#define SUBTITLE_FONT [UIFont fontWithName:@"HelveticaNeue" size:12.5]
#define DISTANCE_FONT [UIFont fontWithName:@"HelveticaNeue-Light" size:12.5]
#define TYPE_FONT [UIFont fontWithName:@"HelveticaNeue-Light" size:12.5]
#define TITLE_FONT [UIFont fontWithName:@"HelveticaNeue-Light" size:17]
#define SUBTITLE_FONT [UIFont fontWithName:@"HelveticaNeue-Light" size:12.5]
#define TITLE_LEFT_SHIFT 44
#define TITLE_RIGHT_SHIFT 35
#define TITLE_RIGHT_SHIFT 23
#define MAX_TITLE_HEIGHT 200
- (void)setTitle:(NSString *)title selectedRange:(NSRange)selectedRange
@ -110,39 +113,56 @@
{
[super layoutSubviews];
[self.distanceLabel sizeToFit];
self.distanceLabel.width += 4;
self.distanceLabel.maxX = self.width - 25;
self.distanceLabel.minY = 12;
[self.typeLabel sizeToFit];
self.typeLabel.maxX = self.width - TITLE_RIGHT_SHIFT;
self.typeLabel.minY = 10;
CGFloat distanceWidth = [self.distanceLabel.text sizeWithDrawSize:CGSizeMake(100, 20) font:DISTANCE_FONT].width;
self.titleLabel.width = self.width - distanceWidth - TITLE_LEFT_SHIFT - TITLE_RIGHT_SHIFT;
[self.distanceLabel sizeToFit];
self.distanceLabel.maxX = self.width - TITLE_RIGHT_SHIFT;
self.distanceLabel.maxY = self.height - 10;
CGFloat const space = 4;
self.titleLabel.width = self.width - self.typeLabel.width - TITLE_LEFT_SHIFT - TITLE_RIGHT_SHIFT - space;
self.titleLabel.minX = TITLE_LEFT_SHIFT;
[self.titleLabel sizeToFit];
if ([self.subtitleLabel.text length])
self.titleLabel.minY = 8;
self.titleLabel.minY = 6;
else
self.titleLabel.midY = self.height / 2 - 1.5;
self.subtitleLabel.origin = CGPointMake(self.titleLabel.minX, self.titleLabel.maxY - 2);
self.subtitleLabel.width = self.width - self.distanceLabel.width - TITLE_LEFT_SHIFT - TITLE_RIGHT_SHIFT - space;
self.subtitleLabel.origin = CGPointMake(TITLE_LEFT_SHIFT, self.titleLabel.maxY - 2);
}
+ (CGFloat)cellHeightWithTitle:(NSString *)title subtitle:(NSString *)subtitle distance:(NSString *)distance viewWidth:(CGFloat)width
+ (CGFloat)cellHeightWithTitle:(NSString *)title type:(NSString *)type subtitle:(NSString *)subtitle distance:(NSString *)distance viewWidth:(CGFloat)width
{
CGFloat distanceWidth = [distance sizeWithDrawSize:CGSizeMake(100, 20) font:DISTANCE_FONT].width;
CGFloat titleHeight = [title sizeWithDrawSize:CGSizeMake(width - distanceWidth - TITLE_LEFT_SHIFT - TITLE_RIGHT_SHIFT, MAX_TITLE_HEIGHT) font:TITLE_FONT].height;
CGFloat typeWidth = [type sizeWithDrawSize:CGSizeMake(100, 22) font:TYPE_FONT].width;
CGFloat titleHeight = [title sizeWithDrawSize:CGSizeMake(width - typeWidth - TITLE_LEFT_SHIFT - TITLE_RIGHT_SHIFT, MAX_TITLE_HEIGHT) font:TITLE_FONT].height;
return MAX(50, titleHeight + ([subtitle length] ? 27 : 15));
}
- (UILabel *)typeLabel
{
if (!_typeLabel)
{
_typeLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 70, 16)];
_typeLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin;
_typeLabel.font = TYPE_FONT;
_typeLabel.textColor = [UIColor colorWithColorCode:@"c9c9c9"];
_typeLabel.textAlignment = NSTextAlignmentRight;
}
return _typeLabel;
}
- (UILabel *)distanceLabel
{
if (!_distanceLabel)
{
_distanceLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 70, 16)];
_distanceLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin;
_distanceLabel.backgroundColor = [UIColor colorWithColorCode:@"16b78a"];
_distanceLabel.textColor = [UIColor colorWithColorCode:@"c9c9c9"];
_distanceLabel.font = DISTANCE_FONT;
_distanceLabel.textColor = [UIColor whiteColor];
_distanceLabel.textAlignment = NSTextAlignmentCenter;
_distanceLabel.textAlignment = NSTextAlignmentRight;
}
return _distanceLabel;
}
@ -174,7 +194,7 @@
{
if (!_iconImageView)
{
_iconImageView = [[UIImageView alloc] initWithFrame:CGRectMake(17, 16, 18, 18)];
_iconImageView = [[UIImageView alloc] initWithFrame:CGRectMake(17, 15.5, 18, 18)];
_iconImageView.contentMode = UIViewContentModeCenter;
}
return _iconImageView;

View file

@ -391,6 +391,7 @@ static void OnSearchResultCallback(search::Results const & results)
self.tableView.scrollIndicatorInsets = self.tableView.contentInset;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
SearchUniversalCell * cell = [tableView dequeueReusableCellWithIdentifier:[SearchUniversalCell className]];
@ -400,6 +401,7 @@ static void OnSearchResultCallback(search::Results const & results)
// initial categories
cell.iconImageView.image = [UIImage imageNamed:@"SearchCellSpotIcon"];
cell.distanceLabel.text = nil;
cell.typeLabel.text = nil;
[cell setTitle:NSLocalizedString(self.categoriesNames[indexPath.row], nil) selectedRange:NSMakeRange(0, 0)];
[cell setSubtitle:nil selectedRange:NSMakeRange(0, 0)];
}
@ -420,6 +422,15 @@ static void OnSearchResultCallback(search::Results const & results)
// suggest item
cell.iconImageView.image = [UIImage imageNamed:@"SearchCellSpotIcon"];
cell.distanceLabel.text = nil;
cell.typeLabel.text = nil;
[cell setSubtitle:nil selectedRange:NSMakeRange(0, 0)];
}
else if (result.GetResultType() == search::Result::RESULT_POI_SUGGEST)
{
// poi suggest item
cell.iconImageView.image = [UIImage imageNamed:@"SearchCellPinsIcon"];
cell.distanceLabel.text = nil;
cell.typeLabel.text = nil;
[cell setSubtitle:nil selectedRange:NSMakeRange(0, 0)];
}
else
@ -427,6 +438,7 @@ static void OnSearchResultCallback(search::Results const & results)
// final search result item
cell.iconImageView.image = [UIImage imageNamed:@"SearchCellPinIcon"];
cell.distanceLabel.text = wrapper.distances[@(position)];
cell.typeLabel.text = [NSString stringWithUTF8String:result.GetFeatureType()];
NSString * subtitle = [NSString stringWithUTF8String:result.GetRegionString()];
NSRange subtitleRange = [subtitle rangeOfString:self.searchBar.textField.text options:NSCaseInsensitiveSearch];
[cell setSubtitle:subtitle selectedRange:subtitleRange];
@ -437,11 +449,11 @@ static void OnSearchResultCallback(search::Results const & results)
// 'show on map' cell
cell.iconImageView.image = [UIImage imageNamed:@"SearchCellSpotIcon"];
cell.distanceLabel.text = nil;
cell.typeLabel.text = nil;
[cell setTitle:NSLocalizedString(@"search_show_on_map", nil) selectedRange:NSMakeRange(0, 0)];
[cell setSubtitle:nil selectedRange:NSMakeRange(0, 0)];
}
}
return cell;
}
@ -449,7 +461,7 @@ static void OnSearchResultCallback(search::Results const & results)
{
if ([self isShowingCategories])
{
return [SearchUniversalCell cellHeightWithTitle:self.categoriesNames[indexPath.row] subtitle:nil distance:nil viewWidth:tableView.width];
return [SearchUniversalCell cellHeightWithTitle:self.categoriesNames[indexPath.row] type:nil subtitle:nil distance:nil viewWidth:tableView.width];
}
else
{
@ -460,11 +472,12 @@ static void OnSearchResultCallback(search::Results const & results)
search::Result const & result = [wrapper resultWithPosition:position];
NSString * title = [NSString stringWithUTF8String:result.GetString()];
NSString * subtitle = [NSString stringWithUTF8String:result.GetRegionString()];
return [SearchUniversalCell cellHeightWithTitle:title subtitle:subtitle distance:wrapper.distances[@(position)] viewWidth:tableView.width];
NSString * type = [NSString stringWithUTF8String:result.GetFeatureType()];
return [SearchUniversalCell cellHeightWithTitle:title type:type subtitle:subtitle distance:wrapper.distances[@(position)] viewWidth:tableView.width];
}
else
{
return [SearchUniversalCell cellHeightWithTitle:NSLocalizedString(@"search_show_on_map", nil) subtitle:nil distance:nil viewWidth:tableView.width];
return [SearchUniversalCell cellHeightWithTitle:NSLocalizedString(@"search_show_on_map", nil) type:nil subtitle:nil distance:nil viewWidth:tableView.width];
}
}
}
@ -493,7 +506,11 @@ static void OnSearchResultCallback(search::Results const & results)
{
self.searchBar.textField.text = [NSString stringWithUTF8String:result.GetSuggestionString()];
[self search];
return;
}
else if (result.GetResultType() == search::Result::RESULT_POI_SUGGEST)
{
self.searchBar.textField.text = [NSString stringWithUTF8String:result.GetSuggestionString()];
[self search];
}
else
{
@ -518,12 +535,12 @@ static void OnSearchResultCallback(search::Results const & results)
- (BOOL)indexPathIsForSearchResultItem:(NSIndexPath *)indexPath
{
return indexPath.row || [self rowsCount] == 1;
return YES;//indexPath.row || [self rowsCount] == 1;
}
- (NSInteger)searchResultPositionForIndexPath:(NSIndexPath *)indexPath
{
return ([self rowsCount] == 1) ? 0 : indexPath.row - 1;
return indexPath.row;//([self rowsCount] == 1) ? 0 : indexPath.row - 1;
}
- (NSInteger)rowsCount
@ -535,6 +552,9 @@ static void OnSearchResultCallback(search::Results const & results)
resultsCount = (wrapperCount == 1) ? 1 : wrapperCount + 1;
else
resultsCount = 0;
resultsCount = wrapperCount;
return [self isShowingCategories] ? [self.categoriesNames count] : resultsCount;
}