创建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 = ;
} - (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 = ;
} - (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 = ; CGSize maxNameLabelSize = CGSizeMake(frame.size.width,);
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,); 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;
}
创建UILabel的更多相关文章
- NGUI学习笔记(一)UILabel介绍
来个前言: 作为一个U3D程序员,自然要写一写U3D相关的内容了.想来想去还是从UI开始搞起,可能这也是最直观同时也最重要的部分之一了.U3D自带的UI系统,也许略坑,也没有太多介绍的价值,那么从今天 ...
- UILabel UISwitch UISegmentedControl UIAlertView
基础小控件 /***************************************UIlabel*************************************/ UILabel ...
- iOS学习21之UILabel, UITextField, UIButton, UIImageView
1.UILabel 1> 概述 UILabel (标签): 是显示文本的控件.在App中 UILabel 是出现频率最高的控件 UILabel 是 UIView 子类,作为子类一般是为了扩充父类 ...
- 【iOS开展-50】使用它来创建一个新的类的实现代码包,因此,不自觉地练习简单MVC实验,附带动画
接下来说说代码封装最后一个个案. 最后一种情况看:[iOS开展-48]九宫格案例:自己主动布局.字典转模型运用.id和instancetype差别.xib反复视图运用及与nib关系 (1)代码封装的原 ...
- iOS UIlabel怎么加载html字符串 富文本的用法
要加载html字符串,用人说,直接用webView啊!但是,有时候我们只需要显示2行文字,如此少的内容却要在复杂的UI排版中加入一个占用资源较多的webview,得不偿失.这里要说的是,我们其实可以用 ...
- ios开发UI篇--UILabel
概述 UILabel类实现了一个只读文本视图.你可以使用这个类来画一个或多个静态文本.UILabel类支持既简单又复杂的样式标签文本,还可以控制外观,比如标签是否使用了一个影子或者处于高亮状态等等. ...
- 【Swift】UILabel的简单操作方法
@IBAction func buttonClick_LabelCtrl(sender: AnyObject) { //定义CGRect来初始化UILable var frame: CGRect = ...
- iOS开发基础控件--UILabel
UILabel 的常见属性和方法: //创建UIlabel对象 UILabel* label = [[UILabel alloc] initWithFrame:self.view.bounds]; / ...
- IOS开发学习笔记(1)-----UILabel 详解
1. [代码][C/C++]代码 //创建uilabelUILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(20, 40, ...
随机推荐
- JavaScript 精髓整理篇之一(对象篇)postby:http://zhutty.cnblogs.com
废话篇头: 由于工作关系,所以写博文的时间有那么点~~,其实是输入法太懒了,都是输入法的错~~ 这一系列的博客将总结所有关于JavaScript语言的精髓,适合0基础到大师级别人物阅读. <Ja ...
- DB2查询当前时间与指定时间的时间差(相隔的秒数)
DB2查询当前时间与指定时间的时间差(相隔的秒数). 例子:“拍品表 auct_item”中有个“结束时间 end_date”的字段,求结束时间与当前时间的间隔秒数. select (DAYS(a. ...
- C# 超级简单的Telnet (TcpClient)客户端
基于Sockets 没什么好说的,代码说明了所有 using System; using System.Collections.Generic; using System.Linq; using Sy ...
- 让DIV垂直居中的几种办法
1.使用CSS3 的伸缩盒布局 <!doctype html> <html> <head> <meta charset="utf-8"&g ...
- 无法连接到ASP.NET Development Server 解决办法
右击项目名称 -> 属性 -> Web -> 选特定端口 -> 输入一个端口值.
- (转)Div+CSS布局入门
在网页制作中,有许多的术语,例如:CSS.HTML.DHTML.XHTML等等.在下面的文章中我们将会用到一些有关于HTML的基本知识,而在你学习这篇入门教程之前,请确定你已经具有了一定的HTML基础 ...
- mvc actionresult 判断是否回发?
if(Request.HttpMethod.Equals("POST", StringComparison.OrdinalIgnoreCase)){POST回发的代码}
- Jquery去除从数据库中查询到的内容含有的p标签
$("#topic_content").html($("#topic_content").text()); 如果这个数据是通过循环遍历出的数据,就需要下面这个代 ...
- (原创) ubuntu 12.04 install nvidia by the deb
先安装 驱动 1. sudo dpkg -i XXX.deb 2. sudo apt-get update 3. sudo apt-get install cuda 4. gedit ~/.bash ...
- <Win32_5>深入浅出Win32的计时器
说起时间,对于我们搞IT的人来说,那是要多重要有多重要.我觉得有价值的时间是给有抱负和有才能的人准备的,因为他们会充分利用,不会让时间失望…… 呵呵,有点儿说远了,还是回归主题吧 Win32的计时器其 ...