label 的经常使用方法

UILabel 多行文字自动换行 (自动折行)
1.UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(10, 100, 300, 180)];   
2.    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 100, 300, 150)];   
3.    label.text = @"where are you? where are you? where are you? where are you? where are you? where are you? where are you? where are you? where are you? where are you?";   
4.    //清空背景颜色   
5.    label.backgroundColor = [UIColor clearColor];   
6.    //设置字体颜色为白色   
7.    label.textColor = [UIColor whiteColor];   
8.    //文字居中显示   
9.    label.textAlignment = UITextAlignmentCenter;   
10.    //自动折行设置   
11.    label.lineBreakMode = UILineBreakModeWordWrap;   
12.    label.numberOfLines = 0;
iOS的UILabel设置居上对齐,居中对齐,居下对齐
在iOS中默认的UILabel中的文字在竖直方向上只能居中对齐,博主参考国外网站,从UILabel继承了一个新类,实现了居上对齐,居中对齐,居下对齐。具体以下:
//  
//  myUILabel.h  
//    
//  
//  Created by yexiaozi_007 on 3/4/13.  
//  Copyright (c) 2013 yexiaozi_007. All rights reserved.  
//  
  
#import <UIKit/UIKit.h>  
typedef enum  
{  
    VerticalAlignmentTop = 0, // default  
    VerticalAlignmentMiddle,  
    VerticalAlignmentBottom,  
} VerticalAlignment;  
@interface myUILabel : UILabel  
{  
@private  
VerticalAlignment _verticalAlignment;  
}  
  
@property (nonatomic) VerticalAlignment verticalAlignment;  
  
@end  
 
//  
//  myUILabel.m  
//    
//  
//  Created by yexiaozi_007 on 3/4/13.  
//  Copyright (c) 2013 yexiaozi_007. All rights reserved.  
//  
  
#import "myUILabel.h"  
  
@implementation myUILabel  
@synthesize verticalAlignment = verticalAlignment_;  
  
- (id)initWithFrame:(CGRect)frame {  
    if (self = [super initWithFrame:frame]) {  
        self.verticalAlignment = VerticalAlignmentMiddle;  
    }  
    return self;  
}  
  
- (void)setVerticalAlignment:(VerticalAlignment)verticalAlignment {  
    verticalAlignment_ = verticalAlignment;  
    [self setNeedsDisplay];  
}  
  
- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines {  
    CGRect textRect = [super textRectForBounds:bounds limitedToNumberOfLines:numberOfLines];  
    switch (self.verticalAlignment) {  
        case VerticalAlignmentTop:  
            textRect.origin.y = bounds.origin.y;  
            break;  
        case VerticalAlignmentBottom:  
            textRect.origin.y = bounds.origin.y + bounds.size.height - textRect.size.height;  
            break;  
        case VerticalAlignmentMiddle:  
            // Fall through.  
        default:  
            textRect.origin.y = bounds.origin.y + (bounds.size.height - textRect.size.height) / 2.0;  
    }  
    return textRect;  
}  
  
-(void)drawTextInRect:(CGRect)requestedRect {  
    CGRect actualRect = [self textRectForBounds:requestedRect limitedToNumberOfLines:self.numberOfLines];  
    [super drawTextInRect:actualRect];  
}  
  
  
@end  
在使用时:
lbl_mylabel = [[myUILabel alloc] initWithFrame:CGRectMake(20, 50, 150, 600)];  
UIColor *color = [UIColor colorWithPatternImage:[UIImage imageNamed:@"halfTransparent.png"]];//使用半透明图片做为label的背景色  
lbl_mylabel.backgroundColor = color;  
lbl_mylabel.textAlignment = UITextAlignmentLeft;  
lbl_mylabel.textColor = UIColor.whiteColor;  
lbl_mylabel.lineBreakMode = UILineBreakModeWordWrap;  
lbl_mylabel.numberOfLines = 0;  
[lbl_mylabel setVerticalAlignment:VerticalAlignmentTop];  
[self addSubview:lbl_mylabel]; 
ios UILabel 变量名不能为title
-[UILabel copyWithZone:]: unrecognized selector sent to instance
 
遇到了这样一个错误,找了半天没找到是什么错误,因而,Google搜索,打开第一个连接http://stackoverflow.com /questions/10784207/uilabel-copywithzone-unrecognized-selector-sent-to-instance
 
UILabel 设置过长文本中间为省略号
 label.lineBreakMode = NSLineBreakByTruncatingMiddle;
参考:iOS组件之UILabel
 
iOS UILabel详解
·UILable是iPhone界面最基本的控件,主要用来显示文本信息。
·经常使用属性和方法有
:
一、建立
    CGRect rect = CGRectMake(100, 200, 50, 50);
    UILabel *label = [[UILabel alloc] initWithFrame:rect];
二、text //设置和读取文本内容,默认为
   nillabel.text = @”文本信息”; //设置内容
   NSLog(@”%@”, label.text); //读取内容
三、textColor
 //设置文字颜色,默认为黑色
  lable.textColor = [UIColor redColor];
四、font //设置字体大小,默认
  label.font = [UIFont systemFontOfSize:20];
 //⼀通常方法
  label.font = [UIFont boldSystemFontOfSize:20]; 
 //加粗方法
  label.font = [UIFont fontWithName:@"Arial" size:16];
  //指定字体的方法//还有⼀一种从外部导入字体的方法。
五、textAlignment 
//设置标签文本对齐方式。
  label.textAlignment = NSTextAlignmentCenter;
 //还有NSTextAlignmentLeft、 NSTextAlignmentRight.
六、numberOfLines 
//标签最多显示行数,若是为0则表示多行。
  label.numberOfLines = 2;
七、enabled 
//只是决定了Label的绘制方式,将它设置为NO将会使文本变暗,表示它没有激活,这时向它设置颜色值是无效的。
   label.enable = NO;
八、highlighted
 //是否高亮显示label.highlighted = YES;
  label.highlightedTextColor = [UIColor orangeColor]; 
//高亮显示时的文本颜色
九、ShadowColor
 //设置阴影颜色 
  [label setShadowColor:[UIColor blackColor]];
十、ShadowOffset 
//设置阴影偏移量
[label setShadowOffset:CGSizeMake(-1, -1)];
十一、baselineAdjustment
 //若是adjustsFontSizeToFitWidth属性设置为YES,这个属性就来控制文本基线的行为。
    label.baselineAdjustment = UIBaselineAdjustmentNone;
    UIBaselineAdjustmentAlignBaselines = 0,默认,文本最上端与中线对齐。
    UIBaselineAdjustmentAlignCenters,  文本中线与label中线对齐。
    UIBaselineAdjustmentNone, 文本最低端与label中线对齐。
十二、Autoshrink
//是否自动收缩Fixed Font Size 默认,若是Label宽度小于文字长度时时,文字大小不自动缩放minimumScaleFactor 设置最小收缩比例,若是Label宽度小于文字长度时,文字进行收缩,收缩超过比例后,中止收缩。minimumFontSize 设置最小收缩字号,若是Label宽度小于文字长度时,文字字号减少,低于设定字号后,再也不减少。
//6.0之后再也不使用了。
     label.minimumScaleFactor = 0.5;
1三、adjustsLetterSpacingToFitWidth 
//改变字母之间的间距来适应Label大小
     myLabel.adjustsLetterSpacingToFitWidth = NO;
1四、 lineBreakMode //设置文字过长时的显示格式 
     label.lineBreakMode = NSLineBreakByCharWrapping;以字符为显示单位显示,后面部分省略不显示。
     label.lineBreakMode = NSLineBreakByClipping;剪切与文本宽度相同的内容长度,后半部分被删除。
     label.lineBreakMode = NSLineBreakByTruncatingHead;前面部分文字以……方式省略,显示尾部文字内容。
     label.lineBreakMode = NSLineBreakByTruncatingMiddle;中间的内容以……方式省略,显示头尾的文字内容。
     label.lineBreakMode = NSLineBreakByTruncatingTail;结尾部分的内容以……方式省略,显示头的文字内容。
     label.lineBreakMode = NSLineBreakByWordWrapping;以单词为显示单位显示,后面部分省略不显示。
1五、adjustsFontSizeToFitWidth
 //设置字体大小适应label宽度 
  label.adjustsFontSizeToFitWidth = YES;
1六、attributedText:设置标签属性文本。
    NSString *text = @"first";
    NSMutableAttributedString *textLabelStr =[[NSMutableAttributedString alloc]initWithString:text];
    [textLabelStr setAttributes:@{NSForegroundColorAttributeName :
    [UIColor lightGrayColor], NSFontAttributeName :
    [UIFont systemFontOfSize:17]} range:NSMakeRange(11,10)];
    label.attributedText = textLabelStr;
1七、竖排文字显示每一个文字加一个换行符,这是最方便和简单的实现方式。
    label.text = @"请\n竖\n直\n方\n向\n排\n列";
    label.numberOfLines = [label.text length];
1八、计算UIlabel 随字体多行后的高度
    CGRect bounds = CGRectMake(0, 0, 200, 300);
    heightLabel = [myLabel textRectForBounds:bounds limitedToNumberOfLines:20]; 
//计算20行后的Label的Frame
     NSLog(@"%f",heightLabel.size.height);
1九、UILabel根据字数多少自动实现适应高度
     UILabel *msgLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 45, 0, 0)];
     msgLabel.backgroundColor = [UIColor lightTextColor];
     [msgLabel setNumberOfLines:0];
     msgLabel.lineBreakMode = UILineBreakModeWordWrap;
     msgLabel.font = [UIFont fontWithName:@"Arial" size:12];
     CGSize size = CGSizeMake(290, 1000);
     msgLabel.text = @"获取到的deviceToken,咱们能够经过webservice服务提交给.net应用程序,这里我简单处理,直接打出来,拷贝到.net应用环境中使用。";
    CGSize msgSie = [msgLabel.text sizeWithFont:fonts constrainedToSize:size];
    [msgLabel setFrame:CGRectMake(15, 45, 290, msgSie.height)];
20、渐变字体Label
    UIColor *titleColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"btn.png"]];
    NSString *title = @"Setting";
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 80, 44)];
    titleLabel.textColor = titleColor;
    titleLabel.text = title;
    titleLabel.font = [UIFont boldSystemFontOfSize:20];
    titleLabel.backgroundColor = [UIColor clearColor];
    [self.view addSubview:titleLabel];
    [titleLabel release];
2一、Label添加边框
    titleLabel.layer.borderColor = [[UIColor grayColor] CGColor];
    titleLabel.layer.borderWidth = 2;
相关文章
相关标签/搜索