• 创建UILabel


    UILabelCreate.h
    #import <UIKit/UIKit.h>
    
    @interface UILabelCreate : UILabel
    
    /**
     *  创建UILabel 初始化不包含text
     *
     *  @param frame          frame
     *  @param font           font
     *  @param textColor      textColor
     *  @param backgroudColor backgroudColor
     *  @param textAlignment  textAlignment
     */
    - (void)createNormalLabel:(CGRect)frame font:(UIFont *)font textColor:(UIColor *)textColor backgroudColor:(UIColor *)backgroudColor  textAlignment:(NSTextAlignment)textAlignment;
    
    /**
     *  创建UILabel 初始化包含text
     *
     *  @param frame          frame
     *  @param text           text
     *  @param font           font
     *  @param textColor      textColor
     *  @param backgroudColor backgroudColor
     *  @param textAlignment  textAlignment
     */
    - (void)createDefaultLabel:(CGRect)frame text:(NSString *)text font:(UIFont *)font textColor:(UIColor *)textColor backgroudColor:(UIColor *)backgroudColor  textAlignment:(NSTextAlignment)textAlignment;
    
    /**
     *  创建UILabel 初始化包含text(自增高)
     *
     *  @param frame          frame
     *  @param text           text
     *  @param font           font
     *  @param textColor      textColor
     *  @param backgroudColor backgroudColor
     *  @param textAlignment  textAlignment
     */
    - (void)createLabelAdjustsFontSizeOfHigh:(CGRect)frame text:(NSString *)text font:(UIFont *)font textColor:(UIColor *)textColor backgroudColor:(UIColor *)backgroudColor  textAlignment:(NSTextAlignment)textAlignment;
    
    ≈
     *  设置Text并返回Label的高
     *
     *  @param text           text
     *  @param lableSizeWidth Label's Width
     *
     *  @return Label's higt
     */
    - (CGFloat)setTextWithLabelSizeHigh:(NSString *)text lableSizeWidth:(CGFloat)lableSizeWidth;
    
    ---------------------------------------------------------------------------------------------------- UILabelCreate.m
    #define IOS7_BEFORE ([[[UIDevice currentDevice] systemVersion] floatValue] <7.0 ? YES : NO) #import "UILabelCreate.h" @implementation UILabelCreate - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // Initialization code } return self; } /* // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { // Drawing code } */ - (void)createNormalLabel:(CGRect)frame font:(UIFont *)font textColor:(UIColor *)textColor backgroudColor:(UIColor *)backgroudColor textAlignment:(NSTextAlignment)textAlignment { [self setFrame:frame]; [self setFont:font]; [self setTextColor:textColor]; if (backgroudColor == nil) { [self setBackgroundColor:[UIColor clearColor]]; } else{ [self setBackgroundColor:backgroudColor]; } [self setTextAlignment:textAlignment]; self.numberOfLines = 0; } - (void)createDefaultLabel:(CGRect)frame text:(NSString *)text font:(UIFont *)font textColor:(UIColor *)textColor backgroudColor:(UIColor *)backgroudColor textAlignment:(NSTextAlignment)textAlignment { [self setFrame:frame]; [self setText:text]; [self setFont:font]; [self setTextColor:textColor]; if (backgroudColor == nil) { [self setBackgroundColor:[UIColor clearColor]]; } else{ [self setBackgroundColor:backgroudColor]; } [self setTextAlignment:textAlignment]; self.numberOfLines = 0; } - (void)createLabelAdjustsFontSizeOfHigh:(CGRect)frame text:(NSString *)text font:(UIFont *)font textColor:(UIColor *)textColor backgroudColor:(UIColor *)backgroudColor textAlignment:(NSTextAlignment)textAlignment { [self setText:text]; [self setFont:font]; [self setTextColor:textColor]; if (backgroudColor == nil) { [self setBackgroundColor:[UIColor clearColor]]; } else{ [self setBackgroundColor:backgroudColor]; } [self setTextAlignment:textAlignment]; self.numberOfLines = 0; CGSize maxNameLabelSize = CGSizeMake(frame.size.width,2000); CGSize labelSize; if (IOS7_BEFORE) { labelSize = [text sizeWithFont:font constrainedToSize:maxNameLabelSize lineBreakMode:NSLineBreakByWordWrapping]; } else{ labelSize = [text boundingRectWithSize:maxNameLabelSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font} context:nil].size; } [self setFrame:CGRectMake(frame.origin.x, frame.origin.y, labelSize.width, labelSize.height)]; } - (CGFloat)setTextWithLabelSizeHigh:(NSString *)text lableSizeWidth:(CGFloat)lableSizeWidth { [self setText:text]; CGSize maxNameLabelSize = CGSizeMake(lableSizeWidth,2000); CGSize labelSize; if (IOS7_BEFORE) { labelSize = [text sizeWithFont:self.font constrainedToSize:maxNameLabelSize lineBreakMode:NSLineBreakByWordWrapping]; } else{ NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys: self.font,NSFontAttributeName, nil]; labelSize = [text boundingRectWithSize:maxNameLabelSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size; } return labelSize.height; }
  • 相关阅读:
    uniapp 基于 flyio 的 http 请求封装
    微信小程序实现连续扫码功能(uniapp)
    定时器+echarts运行时间太长导致内存溢出页面崩溃
    vue2.0 + element ui 实现表格穿梭框
    js 不同时间格式介绍以及相互间的转换
    vue2.0+Element UI 表格前端分页和后端分页
    vue2.0 + Element UI + axios实现表格分页
    kafka能做什么?kafka集群配置 (卡夫卡 大数据)
    Java List和Map遍历的方法,forEach()的使用
    Flink 集群搭建,Standalone,集群部署,HA高可用部署
  • 原文地址:https://www.cnblogs.com/joesen/p/3779887.html
Copyright © 2020-2023  润新知