zoukankan      html  css  js  c++  java
  • UILabel+Create

    #import <UIKit/UIKit.h>
    
    @interface UILabel (Create)
    
    /**
     *  创建普通Label
     *
     *  @param frame          frame
     *  @param text           text
     *  @param font           font
     *  @param textColor      textColor
     *  @param backgroudColor backgroudColor
     *  @param textAlignment  textAlignment
     *
     *  @return UILabel
     */
    + (UILabel *)createLabelWithFrame:(CGRect)frame text:(NSString *)text font:(UIFont *)font textColor:(UIColor *)textColor backgroudColor:(UIColor *)backgroudColor  textAlignment:(NSTextAlignment)textAlignment;
    
    /**
     *  创建自增高Label
     *
     *  @param frame          frame
     *  @param text           text
     *  @param font           font
     *  @param textColor      textColor
     *  @param backgroudColor backgroudColor
     *  @param textAlignment  textAlignment
     *
     *  @return UILabel
     */
    + (UILabel *)createAdjustsLabelWithFrame:(CGRect)frame text:(NSString *)text font:(UIFont *)font textColor:(UIColor *)textColor backgroudColor:(UIColor *)backgroudColor  textAlignment:(NSTextAlignment)textAlignment;
    
    @end
    
    #import "UILabel+Create.h"
    
    @implementation UILabel (Create)
    
    + (UILabel *)createLabelWithFrame:(CGRect)frame text:(NSString *)text font:(UIFont *)font textColor:(UIColor *)textColor backgroudColor:(UIColor *)backgroudColor  textAlignment:(NSTextAlignment)textAlignment
    {
        UILabel *label = [[UILabel alloc]initWithFrame:frame];
        label.text = text;
        [label setFont:font];
        [label setTextColor:textColor];
        if (backgroudColor == nil) {
            [label setBackgroundColor:[UIColor clearColor]];
        }
        else{
            [label setBackgroundColor:backgroudColor];
        }
        [label setTextAlignment:textAlignment];
        label.numberOfLines = 0;
        
        return  label;
    }
    
    + (UILabel *)createAdjustsLabelWithFrame:(CGRect)frame text:(NSString *)text font:(UIFont *)font textColor:(UIColor *)textColor backgroudColor:(UIColor *)backgroudColor  textAlignment:(NSTextAlignment)textAlignment
    {
        UILabel *label = [[UILabel alloc]initWithFrame:frame];
        label.text = text;
        [label setFont:font];
        [label setTextColor:textColor];
        if (backgroudColor == nil) {
            [label setBackgroundColor:[UIColor clearColor]];
        }
        else{
            [label setBackgroundColor:backgroudColor];
        }
        [label setTextAlignment:textAlignment];
        label.numberOfLines = 0;
        
        CGSize maxNameLabelSize = CGSizeMake(frame.size.width,2000);
        CGSize labelSize;
        labelSize = [text boundingRectWithSize:maxNameLabelSize
                                       options:NSStringDrawingUsesLineFragmentOrigin
                                    attributes:@{NSFontAttributeName:font}
                                       context:nil].size;
        [label setFrame:CGRectMake(frame.origin.x, frame.origin.y, labelSize.width, labelSize.height)];
        
        return  label;
    }
    
    // 使用
    [self.view addSubview:[UILabel createLabelWithFrame:CGRectMake(10, 100, self.view.frame.size.width-20, 30) text:_str font:[UIFont systemFontOfSize:14.f] textColor:[UIColor redColor] backgroudColor:[UIColor clearColor] textAlignment:NSTextAlignmentLeft]];
  • 相关阅读:
    php文件里直接写上<?xml version="1.0" encoding="utf8"?>出错?
    Cannot modify header information headers already sent by错误解决办法
    转:静态类和单例的区别
    转:Spring TransactionDefinition中事务传播的类型
    转:注解+动态代理例子
    转:UML几种类间关系
    转:AOP 的利器:ASM 3.0 介绍
    转:java内部类
    Android 模拟器安装及使用教程
    转:java读取指定package下的class
  • 原文地址:https://www.cnblogs.com/joesen/p/4079380.html
Copyright © 2011-2022 走看看