iOS开发

类似饿了么的定位titleView

2016-08-01  本文已影响183人  武小寺

就不写实现原理,太简单,给懒人的福利.

.h代码

#import <UIKit/UIKit.h>

@interface WLlocationTitleView : UIView


@property(nonatomic, copy)NSString *addressText;

@end

.m代码

#import "WLlocationTitleView.h"

@interface WLlocationTitleView ()

@property (nonatomic, strong)UIImageView *leftImageV;
@property (nonatomic, strong)UIImageView *rightImageV;
@property (nonatomic, strong)UILabel *textLabel;

@end


@implementation WLlocationTitleView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        _leftImageV = [[UIImageView alloc] init];
        _rightImageV = [[UIImageView alloc] init];
        _textLabel = [[UILabel alloc] init];
        
        
        [self addSubview:_leftImageV];
        [self addSubview:_rightImageV];
        [self addSubview:_textLabel];
        
        
        self.layer.masksToBounds = YES;
        self.layer.cornerRadius = frame.size.height / 2;
        
        [self setUI];
        
    }
    return self;
}


- (void)setUI
{
    _leftImageV.image = [UIImage imageNamed:@"position_icon"];
    [_leftImageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(8);
        make.centerY.mas_equalTo(self.mas_centerY);
        make.height.mas_equalTo(13);
        make.width.mas_equalTo(11);
    }];
    
    _textLabel.textColor = [UIColor whiteColor];
    _textLabel.font = [UIFont systemFontOfSize:14];
    _textLabel.text = @"定位中...";
    [_textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(5);
        make.height.mas_equalTo(20);
        make.left.mas_equalTo(_leftImageV.mas_right).mas_offset(5);
    }];
    
    _rightImageV.image = [UIImage imageNamed:@"xiala_icon"];
    [_rightImageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_textLabel.mas_right).mas_offset(5);
        make.centerY.mas_equalTo(self.mas_centerY);
        make.width.mas_equalTo(9);
        make.height.mas_equalTo(5);
    }];
    
}


-(void)setAddressText:(NSString *)addressText
{
    _addressText = addressText;
    _textLabel.text = _addressText;
    CGSize size = [WLTools textSizeWithContentSize:CGSizeMake(200, 20) font:[UIFont systemFontOfSize:14] str:_addressText];
    [_textLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(size.width + 5);
    }];
}

调用时,只需要传入文本就好,图标自己换一下就好.

上一篇 下一篇

猜你喜欢

热点阅读