The label that can be used to add an icon.

Source: Internet
Author: User

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.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.