UILabel 详解
//1. 创建 UIlabel
UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(20, 40, 280, 80)];
//2. 设置背景色 label1.backgroundColor = [UIColor grayColor];
//3. 设置 tag
label1.tag = 91;
//4. 设置标签文本 label1.text = @"Hello world!";
//5. 设置标签文本字体和字体大小 label1.font = [UIFont fontWithName:@"Arial" size:30];
//6. 设置文本对其方式
label1.textAlignment = UITextAlignmentCenter;
// 文本对齐方式有以下三种
//typedef enum {
// UITextAlignmentLeft = 0, 左对齐
// UITextAlignmentCenter, 居中对齐
// UITextAlignmentRight, 右对齐
//} UITextAlignment;
//7. 文本颜色 label1.textColor = [UIColor blueColor];
//8. 超出 label 边界文字的截取方式
label1.lineBreakMode = UILineBreakModeTailTruncation;
// 截取方式有以下 6 种
//typedef enum {
// UILineBreakModeWordWrap = 0, 以空格为边界 , 保留整个单词
// UILineBreakModeCharacterWrap, 保留整个字符
// UILineBreakModeClip, 到边界为止
// UILineBreakModeHeadTruncation, 省略开始 , 以 … 代替
// UILineBreakModeTailTruncation, 省略结尾 , 以 ...... 代替
// UILineBreakModeMiddleTruncation, 省略中间 , 以 ...... 代 替 , 多行时作用于最后⼀一行
//} UILineBreakMode;
//9. 文本文字自适应大小 label1.adjustsFontSizeToFitWidth = YES;
// 当 adjustsFontSizeToFitWidth=YES 时候 , 如果文本 font 要缩小时
//baselineAdjustment 这个值控制文本的基线位置 , 只有文本行数为 1 是有效label1.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
// 有三种方式
//typedef enum {
// UIBaselineAdjustmentAlignBaselines = 0, 默认值 文本最上端于 label 中线对齐
// UIBaselineAdjustmentAlignCenters,// 文本中线于 label 中线对齐
// UIBaselineAdjustmentNone,// 文本最低端与 label 中线 对齐
//} UIBaselineAdjustment;
//10. 文本最多行数 , 为 0 时没有最大行数限制
label1.numberOfLines = 2;
//11. 最小字体 , 行数为 1 时有效 , 默认为 0.0
label1.minimumFontSize = 10.0;
//12. 文本高亮
label1.highlighted = YES;
//13. 文本是否可变 label1.enabled = YES;
//14. 去掉 label 背景色 label1.backgroundColor = [UIColor clearColor];
//15. 文本阴影颜色 label1.shadowColor = [UIColor grayColor];
//16. 阴影大小 label1.shadowOffset = CGSizeMake(1.0, 1.0);
//17. 是否能与用户交互 label1.userInteractionEnabled = YES;
//添加视图
[self.view addSubview:label1];
[label1 release];
UILabel 的使用
摘要 : UILabel 的基本属性 , 简单说明 ,ios6 新属性
myLabel.minimumScaleFactor = 10.0;// 默认值为 0, 为当前字体大小
一、初始化
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake( 40 , 40 , 120 , 44)];
[ self .view addSubview:myLabel];
二、设置文字
1 、设置默认文本
NSString *text = @" 标签文本 " ;
myLabel.text = text;
2 、设置标签文本 ( 此属性是 iOS6 .0 之后才出现 , 如若不是必要 , 不建议使用此属性 )
NSString *text = @" 其实没什么 " ;
NSMutableAttributedString *attributeString =[[NSMutableAttributedString alloc] initWithString:text];
[attributeStringsetAttributes: @ { NSForegroundColorAttributeName : [UIColor redColor], NSFontAttributeName : [UIFont systemFontOfSize: 17 ] } range:NSMakeRange( 2 , 1 )];
myLabel.attributedText = attributeString;
关键字标红的效果
NSString *keyword = @" 开源 " ;
NSString *result = @" 开源中国社区 " ;
// 设置标签文字
NSMutableAttributedString *attrituteString = [[NSMutableAttributedString alloc] initWithString:result];
// 获取标红的位置和长度
NSRange range = [result rangeOfString:keyword];
// 设置标签文字的属性
[attrituteString setAttributes: @{ NSForegroundColorAttributeName : [UIColor redColor], NSFontAttributeName : [UIFont systemFontOfSize: 17 ] } range:range];
// 显示在 Label 上
label.attributedText = attrituteString;
3 、 设置字体 , 如果是使用 2 中的文本 , 那在设置 AttributeString 的属性时已经设置过Font 了和 textColor 了 , 直接使用 1 设置文本时设置文本时 , 设置字体方法
myLabel.font = [UIFont systemFontOfSize: 13 ];
4 、 设置颜色
myLabel.textColor = [UIColor blueColor];
5 、 设置对齐方式
myLabel.textAlignment = NSTextAlignmentCenter; // 居中
NSTextAlignmentLeft // 左对齐
NSTextAlignmentCenter // 居中
NSTextAlignmentRight // 右对齐
NSTextAlignmentJustified // 最后⼀一行自然对
NSTextAlignmentNatural // 默认对齐脚本
NSTextAlignmentJustified 和 NSTextAlignmentNatural 用 的时候会报错 , 程序崩溃 ,暂时不知道什么时候可以使用 , 希望知道的指教⼀一下 , 感激不尽。
6 、 文字剪裁方式
NSLineBreakByWordWrapping = 0 , // 以空格为边界 , 保留单词
// 保留整个字符 // 简单剪裁 , 到边界为止 // 按照 "...... 文字 " 显示 // 按照 " 文字 ......文字 " 显示 // 按照 " 文字 ......" 显示
NSLineBreakByCharWrapping,
NSLineBreakByClipping,
NSLineBreakByTruncatingHead ,
NSLineBreakByTruncatingTail,
NSLineBreakByTruncatingMiddle
myLabel. lineBreakMode = NSLineBreakByTruncatingHead;
7 、 设置 Label enabled 属性 如果设置为 No, 则文字颜色会变暗 , 表明其是不可用的 ,默认值为 YES 。
myLabel.enabled = NO ;
二、匹配 Label 上的文字
1 、是否根据文本宽度改变字体大小
myLabel .adjustsFontSizeToFitWidth = YES ;
// 假设文字内容为 @" 曾在月光之下望烟花 , 曾共看夕阳渐降下 ",Label 长
度为 200 , 则⼀一行显示不下 , 若设置此属性为 YES , 则会降低字体大小 , 以显示全部内容。
前后对比 :
2 、改变字母之间的间距来适应 label 大小 // 当这个属性是 YES, 标签可能改变标签文本的字母间距 , 以使该文本更
适合标签的边界内。此属性的字符串 , 而不管当前行的行的裁剪模式。该属性的默认值是 NO 。
myLabel.adjustsLetterSpacingToFitWidth = NO ;
// 个人使用了⼀一下 , 没发现有什么区别 , 不知道具体是什么时候发挥作用。
3 、设置对齐基线
myLabel.adjustsFontSizeToFitWidth = YES ; // 调整基线位置需将 此属性设置为 YES
myLabel.baselineAdjustment = UIBaselineAdjustmentAlignBaselines;
此属性有三个值可选
1 UIBaselineAdjustmentAlignBaselines // 文本最上端与 Label 中线 对齐 , 默认值
2 UIBaselineAdjustmentAlignCenters // 文本中线与 Label 中线对 齐
3 UIBaselineAdjustmentNone // 文本最下端与 Label 中线 对齐
4 、最小字体大小 , 当字体小于这个最小值时无效 , 显示此属性值
iOS6 .0 之前 :minimumFontSize iOS6 .0 之后 :minimumScaleFactor
1 myLabel.minimumScaleFactor = 10.0 ; // 默认值为 0, 为当前字体大 小
5 、行数
myLabel.numberOfLines = 2 ; //Label 行数 6 、高亮
myLabel.highlighted = YES ; // 是否高亮
myLabel.highlightedTextColor = [UIColor redColor]; // 高亮颜 色 ; 此属性在设置按钮的 titleLabel 时 , 无论 highlighted 是 YES 还是 NO, 在按钮按下时标题都显示此高亮颜色
7 、阴影
myLabel.shadowColor = [UIColor grayColor]; // 阴影颜色 , 默认 为 nil
myLabel.shadowOffset = CGSizeMake( 1 , 1 ); // 阴影的偏移点 三、 Label 位置
1 、计算 UIlabel 随字体多行后的高度
CGRect result,bounds;
bounds = CGRectMake( 0 , 0 , 200 , 300 );
heightLabel = [myLabel textRectForBounds:bounds
limitedToNumberOfLines: 20 ]; // 计算 20 行后的 Label 的 Frame 4 NSLog(@"%f",heightLabel.size.height);
2 、绘制 text 到指定区域
-‐ ( void )drawTextInRect:(CGRect)rect
// 需要重载此方法 , 然后由子类调用 , 重写时调用 super 可以按默认图形属性绘制 , 若自己完全重写绘制函数 , 就不用调用 super 了