(如果需要的不是使用的属性值如换行形式,可以把对应的属性在程序中书写然后按"command"+鼠标左键点击就可以查看所有属性值)

一label基本设置

self.view.backgroundColor = [UIColor redColor];

//创建第一个标签控件

UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(50, 50, 200, 30)];

//对位置设置

//对控件的中心点进行设置

label.center = self.view.center;

label.frame = CGRectMake(20, 20, 30, 30);

加粗;

[UILabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:20]];

加粗并且倾斜

[UILabel setFont:[UIFont fontWithName:@"Helvetica-BoldOblique" size:20]];

//显示文字

label.text = @"我是美女";

//设置字体大小

label.font = [UIFont systemFontOfSize:30];

//自适应大小的方法   标签的大小由字体的大小长度决定

[label sizeToFit];

//字体的颜色  alpha 透明度 0 - 1   0- 1

label.textColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:195/255.0 alpha:1];

//Red Green Blue 0 - 255   255  255  255

//  0 - 1

//字体对齐格式  右侧是枚举类型

label.textAlignment = NSTextAlignmentCenter;

//加背景颜色

label.backgroundColor = [UIColor greenColor];

//显示出来 将标签 放到视图上 进行显示

[self.view addSubview:label];

//addSubview 添加子视图

//不是程序崩溃前提下 问题:

//第一点  frame是否设置了

//第二点  是不是加到了父视图中

//第三点  背景色和 控件颜色 一样

二.文字自适应

//创建label

UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 50, 200, 999)];

label.backgroundColor = [UIColor greenColor];

label.text = @"To be or not to be, that is a question。To be or not to be, that is a question。To be or not to be, that is a question。To be or not to be, that is a question。To be or not to be, that is a question。";

label.font = [UIFont systemFontOfSize:18];

label.textColor = [UIColor redColor];

//设置 label的换行模式

label.lineBreakMode = NSLineBreakByWordWrapping; //根据单词进行换行

//设置label显示几行  可以有无限行

label.numberOfLines = 0;

[label sizeToFit];

[self.view addSubview:label];

text  property

font  property

textColor  property

textAlignment  property

lineBreakMode  property

enabled  property

Sizing the Label’s Text

adjustsFontSizeToFitWidth  property

baselineAdjustment  property

minimumFontSize  property   无例

numberOfLines  property

Managing Highlight Values

highlightedTextColor  property

highlighted  property

Drawing a Shadow

shadowColor  property

shadowOffset  property

Drawing and Positioning Overrides

– textRectForBounds:limitedToNumberOfLines: 无例

– drawTextInRect:  无例

Setting and Getting Attributes

userInteractionEnabled  property

UILabel垂直居上对齐[label sizeToFit];

//设置文字过长时的显示格式

label.lineBreakMode = UILineBreakModeWordWrap;

typedefenum {

UILineBreakModeWordWrap =0,           // Wrap at word boundaries

UILineBreakModeCharacterWrap,          // Wrap at character boundaries

UILineBreakModeClip,           //截去多余部分 Simply clip when it hits the end of the rect截去多余部分

UILineBreakModeHeadTruncation, //截去头部Truncate at head of line: "...wxyz". Will truncate multiline text on first line

UILineBreakModeTailTruncation,//截去尾部 Truncate at tail of line: "abcd...". Will truncate multiline text on last line

UILineBreakModeMiddleTruncation,//截去中间 Truncate middle of line:  "ab...yz". Will truncate multiline text in the middle

} UILineBreakMode;

//设置label的行数,这个可以根据上节的UITextView自适应高度

label.numberOfLines = 2;

label.lineBreakMode = UILineBreakModeWordWrap;

label.textAlignment =  UITextAlignmentCenter;//设置文字对齐位置,居左,居中,居右

label.text = @ "123" ;//设置显示文字 

//设置文字颜色,可以有多种颜色可以选择

label.textColor = [UIColor whiteColor];

label.backgroundColor = [UIColor blackColor];

//设置字体:粗体,正常的是 SystemFontOfSize,调用系统的字体配置 

label.font = [UIFont boldSystemFontOfSize:20];

label.font = [UIFont fontWithName:@ "Arial Rounded MT Bold"  size:(36.0)];

//[UIFont fontWithName:@ "Arial" size:14.0]]; //非加粗

//设置文本是否高亮和高亮时的颜色

scoreLabel.highlighted = YES; 

scoreLabel.highlightedTextColor = [UIColor orangeColor]; 

//设置阴影的颜色和阴影的偏移位置 

scoreLabel.shadowColor = [UIColor redColor]; 

scoreLabel.shadowOffset = CGSizeMake(1.0,1.0); 

//设置是否能与用户进行交互 

scoreLabel.userInteractionEnabled = YES;  

//设置label中的文字是否可变,默认值是YES  

scoreLabel.enabled = NO;

//设置字体大小是否适应label宽度 

label.adjustsFontSizeToFitWidth = YES; 

//如果adjustsFontSizeToFitWidth属性设置为YES,这个属性就来控制文本基线的行为

coreLabel.baselineAdjustment = UIBaselineAdjustmentNone

typedefenum {

UIBaselineAdjustmentAlignBaselines =0,// default. used when shrinking text to position based on the original baseline

UIBaselineAdjustmentAlignCenters,

UIBaselineAdjustmentNone,

} UIBaselineAdjustment;

//最小文字号数

minimumFontSize

设置背景色为透明

scoreLabel.backgroudColor=[UIColor clearColor];

自定义的颜色:

scoreLabel.backgroudColor=[UIColor clearColor];

UIColor *color = [UIColor colorWithRed:1.0f green:50.0f blue:0.0f alpha:1.0f];

scoreLabel.textColor = [UIColor color]

//UIColor 里的 RGB 值是CGFloat类型的在0~1范围内,对应0~255的颜色值范围。

 

- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines;

//改变绘文字属性.重写时调用super可以按默认图形属性绘制,若自己完全重写绘制函数,就不用调用super了.

- (void)drawTextInRect:(CGRect)rect;

eg:

UILabel *label = [[UILabelalloc] initWithFrame:CGRectMake(0, 0, 75, 40)];   //声明UIlbel并指定其位置和长宽
label.backgroundColor = [UIColorclearColor]; //设置label的背景色,这里设置为透明色。
label.font = [UIFont fontWithName:@"Helvetica-Bold" size:13]; //设置label的字体和字体大小。
//lable的旋转
label.transform = CGAffineTransformMakeRotation(0.1); //设置label的旋转角度
label.text = @“helloworld”; //设置label所显示的文本
label.textColor = [UIColorwhiteColor]; //设置文本的颜色
label.shadowColor = [UIColorcolorWithWhite:0.1falpha:0.8f]; //设置文本的阴影色彩和透明度。
label.shadowOffset = CGSizeMake(2.0f, 2.0f); //设置阴影的倾斜角度。
label.textAlignment = UITextAlignmentCenter; //设置文本在label中显示的位置,这里为居中。
//换行技巧:如下换行可实现多行显示,但要求label有足够的宽度。
label.lineBreakMode = UILineBreakModeWordWrap; //指定换行模式
label.numberOfLines = 2; // 指定label的行数

 

 

 

 

让label自适应里面的文字,自动调整宽度和高度的

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,0,0)];这个frame是初设的,没关系,后面还会重新设置其size。
[label setNumberOfLines:0];
NSString *s = @"string......";
UIFont *font = [UIFont fontWithName:@"Arial" size:12];
CGSize size = CGSizeMake(320,2000);
CGSize labelsize = [s sizeWithFont:font constrainedToSize:size lineBreakMode:UILineBreakModeWordWrap];
[label setFrame:CGRectMake:(0,0, labelsize.width, labelsize.height)];
[self.view addSubView:label];
这样就可以对s赋值让其自动调整其大小了。

UILabel跑马灯效果

//http://www.cocoachina.com/bbs/read.php?tid=74540

#import <UIKit/UIKit.h>

@interface TextFlowView : UIView {

//显示文本的标签

UILabel *_firstLabel;

UILabel *_secondLabel;

//定时器

NSTimer *_timer;

//显示的文本

NSString *_text;

//是否需要滚动

BOOL _needFlow;

//控件的框架大小

CGRect _frame;

//文本的字体

UIFont *_font;

//当前第一个控件的索引

NSInteger _startIndex;

//定时器每次执行偏移后,累计的偏移量之和

CGFloat _XOffset;

//文本显示一行,需要的框架大小

CGSize _textSize;

}

- (id)initWithFrame:(CGRect)frame Text:(NSString *)text;

- (void)setFont:(UIFont *)font;

- (void)setText:(NSString *)text;

@end

//////////////////////////////////////////////////////////////////////////////////

#import "TextFlowView.h"

@implementation TextFlowView

#pragma mark -

#pragma mark 内部调用

#define SPACE_WIDTH 50

#define LABEL_NUM 2

//改变一个TRect的起始点位置,但是其终止店点的位置不变,因此会导致整个框架大小的变化

- (CGRect)moveNewPoint:(CGPoint)point rect:(CGRect)rect

{

CGSize tmpSize;

tmpSize.height = rect.size.height + (rect.origin.y - point.y);

tmpSize.width = rect.size.width + (rect.origin.x - point.x);

returnCGRectMake(point.x, point.y, tmpSize.width, tmpSize.height);

}

//开启定时器

- (void)startRun

{

_timer = [NSTimerscheduledTimerWithTimeInterval:0.02target:selfselector:@selector(timerAction) userInfo:nilrepeats:YES];

}

//关闭定时器

- (void)cancelRun

{

if (_timer)

{

[_timerinvalidate];

}

}

//定时器执行的操作

- (void)timerAction

{

staticCGFloat offsetOnce = -1;

_XOffset += offsetOnce;

if (_XOffset +  _textSize.width <= 0)

{

_XOffset += _textSize.width;

_XOffset += SPACE_WIDTH;

}

[selfsetNeedsDisplay];

}

//计算在给定字体下,文本仅显示一行需要的框架大小

- (CGSize)computeTextSize:(NSString *)text

{

if (text == nil)

{

returnCGSizeMake(0, 0);

}

CGSize boundSize = CGSizeMake(10000, 100);

CGSize stringSize = [_textsizeWithFont:_fontconstrainedToSize:boundSize lineBreakMode:UILineBreakModeWordWrap];

return stringSize;

}

- (id)initWithFrame:(CGRect)frame Text:(NSString *)text

{

self = [superinitWithFrame:frame];

if (self)

{

_text = [text retain];

_frame = frame;

//默认的字体大小

_font = [UIFontsystemFontOfSize:16.0F];

self.backgroundColor = [UIColorredColor];

//初始化标签

//判断是否需要滚动效果

_textSize = [selfcomputeTextSize:text];

//需要滚动效果

if (_textSize.width > frame.size.width)

{

_needFlow = YES;

[selfstartRun];

}

}

returnself;

}

// Only override drawRect: if you perform custom drawing.

// An empty implementation adversely affects performance during animation.

- (void)drawRect:(CGRect)rect

{

CGContextRef context= UIGraphicsGetCurrentContext();

CGContextSetFillColorWithColor(context, [UIColorwhiteColor].CGColor);

// Drawing code

CGFloat startYOffset = (rect.size.height - _textSize.height)/2;

CGPoint origin = rect.origin;

if (_needFlow == YES)

{

//        NSLog(@"OFFSETX:%f", _XOffset);

//        NSLog(@"textwidth:%f",_textSize.width);

rect = [selfmoveNewPoint:CGPointMake(_XOffset, startYOffset) rect:rect];

//        NSLog(@"rect X:%f  Y:%f",rect.origin.x, rect.origin.y);

//        NSLog(@"rect W:%f  H:%f", rect.size.width, rect.size.height);

while (rect.origin.x <= rect.size.width+rect.origin.x)

{

[_textdrawInRect:rect withFont:_font];

rect = [selfmoveNewPoint:CGPointMake(rect.origin.x+_textSize.width+SPACE_WIDTH, rect.origin.y) rect:rect];

//            NSLog(@"inner->rect X:%f  Y:%f",rect.origin.x, rect.origin.y);

//            NSLog(@"inner->rect W:%f  H:%f", rect.size.width, rect.size.height);

}

}

else

{

//在控件的中间绘制文本

origin.x = (rect.size.width - _textSize.width)/2;

origin.y = (rect.size.height - _textSize.height)/2;

rect.origin = origin;

[_textdrawInRect:rect withFont:_font];

}

}

- (void)dealloc

{

[_textrelease];

[superdealloc];

}

#pragma mark -

#pragma mark 外部调用

- (void)setFont:(UIFont *)font

{

_font = font;

}

- (void)setText:(NSString *)text

{

[_textrelease];

_text = [text retain];

}

@end

美化UILabel中的字体代码分享 
http://www.devdiv.com/iOS_iPhone-%E7%BE%8E%E5%8C%96UILabel%E4%B8%AD%E7%9A%84%E5%AD%97%E4%BD%93%E4%BB%A3%E7%A0%81%E5%88%86%E4%BA%AB-thread-122319-1-1.html
UILabel跑马灯效果
http://hi.baidu.com/suxinde2009/blog/item/5bcd0e60dd9bb77f0d33fac3.html
分享一个可垂直顶端对齐的UILabel
http://www.devdiv.com/%E5%88%86%E4%BA%AB%E4%B8%80%E4%B8%AA%E5%8F%AF%E5%9E%82%E7%9B%B4%E9%A1%B6%E7%AB%AF%E5%AF%B9%E9%BD%90%E7%9A%84UILabel-weblog-64796-7239.html

 

让UILabel具有链接功能,点击后调用safari打开网址

  1. //侬侬官网连接
  2. UILabel *labelGovUrl = [[UILabel alloc] initWithFrame:CGRectMake(73.0, 330.0, 180.0, 40.0)];
  3. labelGovUrl.autoresizingMask = (UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight);
  4. labelGovUrl.text = @"侬侬官网 >";
  5. labelGovUrl.backgroundColor = [UIColor clearColor];
  6. labelGovUrl.textColor = [UIColor whiteColor];
  7. labelGovUrl.font = [UIFont fontWithName:@"Helvetica-Bold" size:14];
  8. labelGovUrl.userInteractionEnabled = YES;
  9. labelGovUrl.tag = K_NNGOV_WEBSITE_LABEL_URL;
  10. UITapGestureRecognizer *tapGesture =
  11. [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(openURL:)] autorelease];
  12. [labelGovUrl addGestureRecognizer:tapGesture];
  13. [self.view addSubview:labelGovUrl];
  14. [labelGovUrl release];
  15. -(void)openURL:(UITapGestureRecognizer *)gesture{
  16. NSInteger tag = gesture.view.tag;
  17. NSString *url = nil;
  18. if (tag == K_NNWEIBO_LABEL_URL) {
  19. url = @"http://t.qq.com/yourgame/";
  20. }
  21. if(tag == K_NNGOV_WEBSITE_LABEL_URL){
  22. url = @"http://www.zjnn.cn/";
  23. }
  24. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
  25. }

iosiOSlabel基本使用以及文字自适应的更多相关文章

  1. C# 使用 GDI+ 给图片添加文字,并使文字自适应矩形区域

    需求 需求是要做一个编辑文字的页面.用户在网页端写文字,文字区域是个矩形框,用户可以通过下方的拖动条调节文字大小. 如下图: 提交数据的时候前端传文字区域的左上角和右下角定位给后台.因为前端的字体大小 ...

  2. JAVA操作Excel时文字自适应单元格的宽度设置方法

    使用JAVA操作Excel通常都使用JXL,方法很简单网上也有很多的教程,然后往往一些细节性的问题却导致我们这些Programmer苦恼不已.这两天帮一个朋友做一个Excel表格自动生成的小软件,就遇 ...

  3. 根据展示文字自适应 cell 高度,实现点击cell的伸缩扩展

    1.要根据展示的文字计算cell的高度, 再此给NSString写的延展的方法, 以此获取展示文字的高度 2.在自定义的cell中 声明属性和定义方法 注:在cell上初始化子控件,最好用代码写, 不 ...

  4. iOS UILabel文字自适应高度自适应

    第一步:创建UILabel对象,并设置一些基本设置 UILabel *label = [[UILabel alloc] init]; label.text = @"8月29日,在雅加达亚运会 ...

  5. asp.net 页面,文字自适应手机屏幕

    (1)在<head>和</head>之间插入代码. <meta name="viewport" content="width=device- ...

  6. javascript实现限定高度下文字随不同设备自适应改变字体大小至字数完全展示

    function fontAutoMoreLine() { let textBox = document.getElementById("iconTxt"); let maxHei ...

  7. javascript实现一行文字随不同设备自适应改变字体大小至字数完全展示

    产品提了一个小需求,希望一行能展示用户输入的所有文字,因为最多限制为25字符,但是如果夹杂英文/韩文/日文等,即使字符数是一样的,但是展示的长度不一样,则有些title标题会被截断. 效果如图 前提是 ...

  8. iOS 设置UILabel的行间距并自适应高度

    NSString *contentStr = @"总以为,在最初的地方,有一个最原来的我,就也会有一个最原来的你"; UILabel *tempLabel = [[UILabel ...

  9. 14种网页图片和文字特效的jQuery插件代码

    1.网页图片3d旋转jQuery代码 演示和下载地址 2.存css3实现的tabl选项卡代码 演示和下载地址 3.jQuery标签旋转代码 演示和下载地址 4.鼠标悬浮的图片选项卡代码 演示和下载地址 ...

随机推荐

  1. 【ubuntu】开机启动

    背景 在ubuntu下做开发,虚拟机要经常开启和关闭,重要的进程需要随机自启,非重要的可以手工启动.比如nginx就需要自启,confluence就没那么重要了. 为了控制哪些程序要自启,哪些程序不要 ...

  2. 用sql实现汉字转拼音

    有时我们会需要将汉字转为拼音,例如需要将省市转为拼音后当做编码存储(尽管国家有统一的标识码,但有时候我们还是会用到),网络上也有工具提供汉字转拼音的功能,但各有优劣,一般转拼音后还会存在带声调的字母, ...

  3. asp xml对象转换为string

    'xml文件中没有属性的情况 Dim xmlStrxmlStr="<root><count>1</count><error>0</err ...

  4. Java并发编程:深入剖析ThreadLocal(转载)

    Java并发编程:深入剖析ThreadLocal(转载) 原文链接:Java并发编程:深入剖析ThreadLocal 想必很多朋友对ThreadLocal并不陌生,今天我们就来一起探讨下ThreadL ...

  5. iOS7之后的文本高度封装

    #import "NSString+Util.h" @implementation NSString (Util) +(CGFloat)changeStationWidth:(NS ...

  6. win10下安装Django

    Django的核心(1.4+)可以运行在从2.5到2.7之间的任何Python版本. 我的电脑是操作系统是window10 ,内存是4G. 1.下载django 官网地址:https://www.dj ...

  7. Java和R齐头并进才是根本

    Java和R齐头并进才是根本  数据分析师的成长之路  http://www.slideshare.net/SanderMak/data-science-with-r-for-java-d

  8. 在ASP.NET Web Forms中使用页面导出伪xls Excel表格

    将数据导出为Excel表格是比较常见的需求,也有很多组件支持导出真正的Excel表格.由于Excel能打开HTML文件,并支持其中的table元素以及p之类的文本元素的显示,所以把.html扩展名改为 ...

  9. jquery 里 $(this)的用法

    当遇到循环table时,查看其中的td.tr属性和值会有一点的麻烦.此时就必须使用$(this)来解决这一类的问题了. 1.直接使用 2.间接使用 <table> <?php for ...

  10. "ApplicationDbContext"(泛指之类的数据库上下文模型)上下文的模型已在数据库创建后发生更改。请考虑使用 Code First 迁移更新数据库。

    一,在我使用自动生成数据库的时候,当你改变了数据库就会出现下面问题 "ApplicationDbContext"(泛指之类的数据库上下文模型)上下文的模型已在数据库创建后发生更改. ...