UILabel
来源:互联网 发布:暴雪 泰坦计划 知乎 编辑:程序博客网 时间:2024/05/29 17:14
//设置字体:粗体,正常的是 SystemFontOfSize
label1.font = [UIFont boldSystemFontOfSize:20];
//设置文字颜色
label1.textColor = [UIColor orangeColor];
label2.textColor = [UIColor purpleColor];
//设置文字位置
label1.textAlignment = UITextAlignmentRight;
label2.textAlignment = UITextAlignmentCenter;
//设置字体大小适应label宽度
label4.adjustsFontSizeToFitWidth = YES;
//设置label的行数
UIlabel.backgroudColor=[UIColor clearColor]; //可以去掉背景色
//设置高亮
label6.highlighted = YES;
label6.highlightedTextColor = [UIColor orangeColor];
//设置阴影
label7.shadowColor = [UIColor redColor];
label7.shadowOffset = CGSizeMake(1.0,1.0);
//设置是否能与用户进行交互
label7.userInteractionEnabled = YES;
//设置label中的文字是否可变,默认值是YES
label3.enabled = NO;
//设置文字过长时的显示格式
label3.lineBreakMode = UILineBreakModeMiddleTruncation;//截去中间
. //自动折行设置 . label.lineBreakMode = UILineBreakModeWordWrap; . label.numberOfLines = 0;
//如果adjustsFontSizeToFitWidth属性设置为YES,这个属性就来控制文本基线的行为
label4.baselineAdjustment = UIBaselineAdjustmentNone;
一.默认状态
单行,不可获取用户点击事件,UIViewContentModeRedraw模式,改变就重绘.
二.相关属性与函数
1.文本属性
text // label 显示的文字
font // text 的字体,值不可以是nil,否则异常
textColor // text 的颜色
textAlignment // text 的对其方式
lineBreakMode // 当文字超出label显示区域时的截取方式
typedef enum {
UILineBreakModeWordWrap = 0, // 以空格为界,保留整个单词
UILineBreakModeCharacterWrap, // 保留整个字符
UILineBreakModeClip, // 画到边界为止
UILineBreakModeHeadTruncation, // 省略开始,...代替
UILineBreakModeTailTruncation, // 省略尾部,...代替
UILineBreakModeMiddleTruncation, // 省略中间,...代替.这3个多行时作用于最后一行,多行时这三个以单词分行
} UILineBreakMode;
enabled // 文字是否可变,布尔值
2.文本区域
adjustsFontSizeToFitWidth
布尔值,@property(nonatomic) BOOL adjustsFontSizeToFitWidth,设置文本字体是否要减小来适应label的区域.只有当label的行数事1时有效.字体一直减小到你设置的最小字体值.
baselineAdjustment
当adjustsFontSizeToFitWidth值为YES的时候,如果文本的font需要缩小,这个值控制基线位置.只有为本行数为1时有效
@property(nonatomic) UIBaselineAdjustment baselineAdjustment
typedef enum {
UIBaselineAdjustmentAlignBaselines = 0, // 默认值文本最上端与label中间线对齐
UIBaselineAdjustmentAlignCenters,// 文本中间与label中间线对齐
UIBaselineAdjustmentNone,// 文本最底端与label中间线对齐
} UIBaselineAdjustment;
minimumFontSize // 最小字体,行数为1时有效,默认0.0
numberOfLines // 文本行数,为0时没有最大行数限制
3.高亮设置
highlightedTextColor // 文本高亮时的颜色
highlighted // 文本是否高亮
4.绘制阴影
shadowColor // 阴影颜色
shadowOffset // 阴影偏移
5.重载绘画
– textRectForBounds:limitedToNumberOfLines: // 不可直接调用,需要子类重载,第二参数为0时没有行数限制
– drawTextInRect: // 不可直接调用,需要子类重载
6.用户交互
userInteractionEnabled // 是否可与用户交互,基本上也都在子类中重载使用
三.常用范例
1.取得label大小正好适合文字
NSString* text = @"this is a simple uilabel sample";
UILabel* label = [[UILabel alloc] initWithFrame:CGRectZero];
label.backgroundColor = [UIColor redColor];
CGFloat width = 30.0;
UIFont* font = [UIFont systemFontOfSize:14.0];
label.font = font;
label.numberOfLines = 0;
label.lineBreakMode = UILineBreakModeWordWrap;
label.text = text;
// 方法一,用文字来确定
CGSize size = [text sizeWithFont:font constrainedToSize:CGSizeMake(width, 2000.0) lineBreakMode:UILineBreakModeWordWrap];
// 方法二,自适应
//CGSize size = [label sizeThatFits:CGSizeMake(width, 2000.0)];
label.frame = CGRectMake(0, 0, size.width, size.height);
- UILabel
- UILabel
- UILabel
- UILabel
- UILabel
- UILabel
- UILabel
- UILabel
- UILabel
- UILabel
- UILabel
- UILabel
- UILabel
- UIlabel
- UILabel
- UILabel
- UILabel
- UILabel
- 我的学习之旅(36) sched.c
- SqlServer Try Catch
- uva 1335 Beijing Guards(二分)
- 4.jquery的使用
- Android画布使用Region.Op.XOR没有效果的原因
- UILabel
- Ubuntu查看crontab运行日志
- decode_stacktrace: make stack dump output useful again
- Eclipse启动时tomcat超时设置
- CSS Sprite
- cocos2d-x 3.x win配置
- 坚持刷题
- 2.5习题与注解
- ActionResult