The label that can be used to add an icon.
Label that can add icons
Effect
Source code
IconEdgeInsetsLabel in https://github.com/YouXianMing/UI-Component-Collection
/// 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,} response; @ interface identifier: 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 topology () @ property (nonatomic, weak) UIView * oldIconView; @ end @ implementation topology-(CGRect) textRectForBounds :( CGRect) bounds limitedToNumberOfLines :( NSInteger) numberOfLines {UIEdgeInsets insets = self. edgeInsets; CGRect rect = [super textRectForBounds: UIEdgeInsets InsetRect (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
Details
1. inherited from UILabel
2. Two UILabel methods are reloaded.