能添加表徵圖的label,添加表徵圖label

來源:互聯網
上載者:User

能添加表徵圖的label,添加表徵圖label

能添加表徵圖的label

 

效果

 

源碼

https://github.com/YouXianMing/UI-Component-Collection 中的 IconEdgeInsetsLabel

////  IconEdgeInsetsLabel.h//  EdgeInsetLabel////  Created by YouXianMing on 16/6/22.//  Copyright © 2016年 YouXianMing. All rights reserved.//#import <UIKit/UIKit.h>typedef enum : NSUInteger {        kIconAtLeft,    kIconAtRight,    } EIconEdgeDirection;@interface IconEdgeInsetsLabel : UILabel@property (nonatomic, strong) UIView             *iconView;@property (nonatomic)         UIEdgeInsets        edgeInsets;@property (nonatomic)         EIconEdgeDirection  direction;@property (nonatomic)         CGFloat             gap;- (void)sizeToFitWithText:(NSString *)text;@end
////  IconEdgeInsetsLabel.m//  EdgeInsetLabel////  Created by YouXianMing on 16/6/22.//  Copyright © 2016年 YouXianMing. All rights reserved.//#import "IconEdgeInsetsLabel.h"#import "UIView+SetRect.h"@interface IconEdgeInsetsLabel ()@property (nonatomic, weak) UIView  *oldIconView;@end@implementation IconEdgeInsetsLabel- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines {        UIEdgeInsets insets = self.edgeInsets;        CGRect rect = [super textRectForBounds:UIEdgeInsetsInsetRect(bounds, insets) limitedToNumberOfLines:numberOfLines];        rect.origin.x    -= insets.left;    rect.origin.y    -= insets.top;    rect.size.height += (insets.top + insets.bottom);    _iconView && [_iconView isKindOfClass:[UIView class]] ?    (rect.size.width += (insets.left + insets.right + _gap + _iconView.frame.size.width)) :    (rect.size.width += (insets.left + insets.right));    return rect;}- (void)drawTextInRect:(CGRect)rect {        UIEdgeInsets insets = self.edgeInsets;        if (self.iconView) {                if (self.direction == kIconAtLeft) {            _iconView.left    = insets.left;            _iconView.centerY = self.middleY;            insets = UIEdgeInsetsMake(insets.top, insets.left + _gap + _iconView.frame.size.width, insets.bottom, insets.right);                    } else if (self.direction == kIconAtRight) {                    _iconView.right   = self.width - insets.right;            _iconView.centerY = self.middleY;            insets = UIEdgeInsetsMake(insets.top, insets.left, insets.bottom, insets.right  + _gap + _iconView.frame.size.width);        }    }        [super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)];}- (void)sizeToFitWithText:(NSString *)text {    self.text = text;    [self sizeToFit];}#pragma mark - setter & getter.@synthesize iconView = _iconView;- (void)setIconView:(UIView *)iconView {    _oldIconView && [_oldIconView isKindOfClass:[UIView class]] ? ([_oldIconView removeFromSuperview]) : 0;        _iconView    = iconView;    _oldIconView = iconView;    iconView.x   = 0.f;    iconView.y   = 0.f;        [self addSubview:iconView];}- (UIView *)iconView {    return _iconView;}@end

 

細節

1. 繼承自UILabel

2. 重載了UILabel的兩個方法

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.