zoukankan      html  css  js  c++  java
  • 创建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; }
  • 相关阅读:
    ABP记录被删除调用Repository.Get报错
    C# 中在对象后面跟“?” 以及在类型后面跟问号
    list转table
    ling groupby多字段分组统计
    linq一堆多再对多
    ABP下mvc的libs还原
    EF数据迁移
    ABP密码规则设置
    ABP中table时间格式化
    ABP读取appseting.json
  • 原文地址:https://www.cnblogs.com/joesen/p/3779887.html
Copyright © 2011-2022 走看看