ios基础篇(四)——UILabel的经常使用属性及方法

UILabel的经常使用属性及方法:
一、
text //设置和读取文本内容,默认为nil label.text = @”文本信息”; //设置内容 NSLog(@”%@”, label.text); //读取内容
二、textColor //设置文字颜色,默认为黑色 lable.textColor = [UIColor redColor];
三、font //设置字体大小,默认17 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(-5, -5)];
十、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;
十二、
adjustsFontSizeToFitWidth //改变字母之间的间距来适应Label大小
myLabel.adjustsFontSizeToFitWidth = 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 *str = @"text"; NSMutableAttributedString *textLabelStr = [[NSMutableAttributedString alloc] initWithString:str]; [textLabelStrsetAttributes:@{NSForegroundColorAttributeName:[UIColor lightGrayColor],
                 NSFontAttributeName :[UIFont systemFontOfSize:17]}
                 range:NSMakeRange(11,10)]; labe.attributedText = textLabelStr;
1六、计算UIlabel 随字体多行后的高度 CGRect bounds = CGRectMake(0, 0, 100, 100); heightLabel = [myLabel textRectForBounds:boundslimitedToNumberOfLines:20]; //计算20行后的Label的Frame,经常使用语UITableViewCell的可变高度 NSLog(@"%f",heightLabel.size.height);//输出
1七、UILabel根据字数多少自动实现适应高度 UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 30, 0, 0)];
label.backgroundColor = [UIColor redColor];
[label setNumberOfLines:0];
label.lineBreakMode = UILineBreakModeWordWrap;
label.font = [UIFont fontWithName:@"Arial" size:15];
CGSize size = CGSizeMake(200, 1000);
label.text = @“labelText”;
CGSize size = [label.text sizeWithFont:fonts constrainedToSize:size];
[label setFrame:CGRectMake(10, 10, 200, size.height)];
1八、渐变字体Label
UIColor *titleColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"pic.png"]];
NSString *title = @"titleText";
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
titleLabel.textColor = titleColor;
titleLabel.text = title;
titleLabel.font = [UIFont boldSystemFontOfSize:20];
titleLabel.backgroundColor = [UIColor clearColor];
[self.view addSubview:titleLabel];
1九、Label添加边框 titleLabel.layer.borderColor = [[UIColor grayColor] CGColor]; titleLabel.layer.borderWidth = 2;

 

    //初始化
    myLabel = [[UILabel alloc] initWithFrame:(CGRect){20,50,300,50}];
    [self.view addSubview:myLabel];
    //背景颜色
    myLabel.backgroundColor = [UIColor purpleColor];
    //文本
    myLabel.text = @"锄禾日当午,汗滴禾下土.";
    //文本颜色
    myLabel.textColor = [UIColor grayColor];
    //字体大小
    myLabel.font = [UIFont boldSystemFontOfSize:20];
    //对齐方式
    myLabel.textAlignment = NSTextAlignmentCenter;
    //行数
    myLabel.numberOfLines = 2;
    //圆角
    myLabel.layer.cornerRadius = 0;
    myLabel.layer.masksToBounds = YES;
    //高亮
    myLabel.highlighted = YES;
    myLabel.highlightedTextColor = [UIColor greenColor];
    //自动折行设置
    myLabel.lineBreakMode = NSLineBreakByCharWrapping;
    //阴影
    [myLabel setShadowOffset:(CGSize){4,4}];
    [myLabel setShadowColor:[UIColor blueColor]];
    //文本基线
    myLabel.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
    //是否自动收缩
    myLabel.minimumScaleFactor = 0.4;
    //宽度自适应
    myLabel.adjustsFontSizeToFitWidth = NO;
    
相关文章
相关标签/搜索