类似饿了么的定位titleView

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

.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);
    }];
}

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,986评论 25 709
  • 《我的少女时代》这部电影还没上映之前就被里面那首主题曲《小幸运》所吸引了。每次听都会被歌词的情绪所感染。其实歌词...
    HANDKERCHIEF阅读 1,877评论 0 0

友情链接更多精彩内容