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]];
时间: 2024-08-06 16:04:33

UILabel+Create的相关文章

cocos2dx-ui的分类与使用

容器层的使用 GUI 控件我们大致可以分为两类,**普通控件** 和 容器控件,普通控件指的是一些常用的控件,如 UIButton,UILabel,UISlider 和 UITextField 等控件,而容器控件如 UILayout,UIScrollView,UIListView,UIPageView 等,这些容器控件都有一个特点,它可以作为容器,包含其它控件,虽然所有的控件都能够包含其它控件,但有些控件的职责非常单一,如按钮标签等,并不经常向其添加其它控件.以下详细介绍容器控件的使用方法. U

NGUI学习笔记(一)UILabel介绍

来个前言: 作为一个U3D程序员,自然要写一写U3D相关的内容了.想来想去还是从UI开始搞起,可能这也是最直观同时也最重要的部分之一了.U3D自带的UI系统,也许略坑,也没有太多介绍的价值,那么从今天开始就记录一下主流的UI插件-NGUI吧. NGUI版本: v3.6.8 学习笔记一 假定大家都已经将ngui导入到了项目中,这里需要注意,插件(.package)的存放路径不能有中文,否则会导致解压失败. 导入之后可以看到几个文件夹咯,Editor,Examples,Resources,Scrip

UILabel创建(React Native)

UILabel创建(React Native) by 伍雪颖 'use strict'; var React = require('react-native'); var { AppRegistry, StyleSheet, View, Text, } = React; class UILabel extends React.Component { render() { return ( <View style={styles.bgview}> <Text style = {styles

IOS 为UILabel添加长按复制功能

IOS 为UILabel添加长按复制功能 在iOS中下面三个控件,自身就有复制-粘贴的功能: 1.UITextView 2.UITextField 3.UIWebView UIKit framework提供了几个类和协议方便我们在自己的应用程序中实现剪贴板的功能. 1.UIPasteboard:我们可以向其中写入数据,也可以读取数据 2.UIMenuController:显示一个快捷菜单,用来展示复制.剪贴.粘贴等选择的项. 3.UIResponder中的 canPerformAction:wi

UITextField和一个UILabel绑定 浅析

转载自:http://fengdeng.github.io/blog/2016/01/22/rxswift-dao-di-%5B%3F%5D-ge-uitextfieldshi-ru-he-he-%5B%3F%5D-ge-uilabelbang-ding-de/ 多看多揣摩吧! 只要一行代码 writeTextField.rx_text.bindTo(displayLabel.rx_text) 看下效果 那么这到底是如何实现的呢 整体分析 代码层: writeTextField的rx_text通

[转]iOS为UILabel添加长按复制功能

在iOS中下面三个控件,自身就有复制-粘贴的功能: 1.UITextView 2.UITextField 3.UIWebView UIKit framework提供了几个类和协议方便我们在自己的应用程序中实现剪贴板的功能. 1.UIPasteboard:我们可以向其中写入数据,也可以读取数据 2.UIMenuController:显示一个快捷菜单,用来展示复制.剪贴.粘贴等选择的项. 3.UIResponder中的 canPerformAction:withSender:用于控制哪些命令显示在快

UIView及其子类 &#160;UILabel

UI概述 UI(User Interface):?用户界?面,?用户能看到的各种各样的?页?面元素. iOS App = 各种各样的UI控件 + 业务逻辑和算法. 什么是window? window是窗?口,每个app都需要借助window将内容展现给?用户看. 在iOS中,使?用UIWindow类来表?示窗?口,通常?一个应?用程序只创建 ?一个UIWindow对象. window的主要作用是呈现内容给用户,我们不会对window做太多操作. 如何创建window? 通常window的大小(f

Auto Layout和UILabel

前段时间千牛iOS版本也从iOS 6.0开始支持,所以可以正式引入Auto Layout来进行界面布局. 这里记录下在UILabel上应用Auto Layout进行布局的过程. 一.业务场景 用三个UILabel展示一件商品的基本信息:标题,价格,销量: 标题排在最上面,左右两边至少留出20的边距,可以换行: 价格排在标题下面,左边与标题对齐,顶部和标题留出10的边距: 销量排在价格右边,字体略小,底部和价格对齐,左边留出10的边距: 类似如下布局: 二.准备工作 首先,新建一个界面如下,添加三

[TypeScript] Create random integers in a given range

Learn how to create random integers using JavaScript / TypeScript. /** * Returns a random int between * @param start inclusive * @param before exclusive */ export function randomInt(start: number, before: number) { return start + Math.floor(Math.rand