美文网首页
UILabel学习笔记

UILabel学习笔记

作者: 寻心_0a46 | 来源:发表于2019-08-07 21:14 被阅读0次

UILabel摘要

一种显示一行或多行只读文本的视图,通常与控件一起用于描述它们的预期用途。

UILabel常用属性

@property(nullable, nonatomic,copy) NSString *text;

属性描述 : 设置UILabe文本,默认为nil。

@property(nullable, nonatomic,copy)   NSString           *text; 

@property(null_resettable, nonatomic,strong) UIFont *font;

属性描述 : 设置UILabe文本字体,默认为nil(系统字体17为普通字体)。

@property(null_resettable, nonatomic,strong) UIFont      *font;     

@property(null_resettable, nonatomic,strong) UIColor *textColor;

属性描述 : 设置文本颜色,默认为nil(文本绘制为黑色)。

@property(null_resettable, nonatomic,strong) UIColor     *textColor;

@property(nullable, nonatomic,strong) UIColor *shadowColor;

属性描述 : 设置文本内容阴影颜色,默认为nil(无阴影)。

@property(nullable, nonatomic,strong) UIColor            *shadowColor;

@property(nonatomic) NSTextAlignment textAlignment;

属性描述 :设置文本对齐方式,默认为NSTextAlignmentNatural(在ios9之前,默认为NSTextAlignmentLeft)。

@property(nonatomic)        NSTextAlignment    textAlignment; 

@property(nonatomic) NSLineBreakMode lineBreakMode;

属性描述 : 设置文字过长时的显示格式,默认是NSLineBreakByTruncatingTail,用于单行和多行文本。

@property(nonatomic)        NSLineBreakMode    lineBreakMode; 

//NSLineBreakMode枚举值如下:
typedef NS_ENUM(NSInteger, NSLineBreakMode) {
    NSLineBreakByWordWrapping = 0,         // 以单词为显示单位显示,后面部分省略不显示。
    NSLineBreakByCharWrapping,        // 以字符为显示单位显示,后面部分省略不显示。
    NSLineBreakByClipping,        // 剪切与文本宽度相同的内容长度,后半部分被删除。
    NSLineBreakByTruncatingHead,    // 前面部分文字以……方式省略,显示尾部文字内容。
    NSLineBreakByTruncatingTail,    // 结尾部分的内容以……方式省略,显示头的文字内容。
    NSLineBreakByTruncatingMiddle    // 中间的内容以……方式省略,显示头尾的文字内容。
} NS_ENUM_AVAILABLE(10_0, 6_0);

@property(nullable, nonatomic,copy) NSAttributedString *attributedText NS_AVAILABLE_IOS(6_0);

属性描述 : 设置带属性的字符串,默认为nil

@property(nullable, nonatomic,copy)   NSAttributedString *attributedText NS_AVAILABLE_IOS(6_0); 

@property(nullable, nonatomic,strong) UIColor *highlightedTextColor;

属性描述 : 设置突出显示的文本颜色,默认为nil。

@property(nullable, nonatomic,strong)  UIColor *highlightedTextColor;

@property(nonatomic,getter=isHighlighted) BOOL highlighted;

属性描述 : 是否突出显示,默认为NO。

@property(nonatomic,getter=isHighlighted) BOOL     highlighted; 

@property(nonatomic) NSInteger numberOfLines;

属性描述 : 设置要绘制的行的数量以及调用sizeToFit时的操作。默认值为1(单行)。值0表示没有限制。如果文本的高度达到行数的#,或者视图的高度小于允许的行数的#,文本将使用换行模式截断。

@property(nonatomic) NSInteger numberOfLines;  

@property(nonatomic,getter=isEnabled) BOOL enabled;

属性描述 : 是否启用,默认为 YES

@property(nonatomic,getter=isEnabled)    BOOL enabled; 

将enabled设为NO,原本为红色文本的标签的样式,如果设置了shadowColor,shadowColor不会收到影响:


屏幕快照 2019-10-25 下午9.37.05.png

接下来的3个属性允许标签自动调整大小以适应一定的宽度,方法是将字体大小调整一个比例因子>=最小比例因子,并指定文本基线在需要缩小字体时的移动方式。

//调整字体大小以适应宽度,默认为NO
@property(nonatomic) BOOL adjustsFontSizeToFitWidth; 
//基线调整,默认为UIBaselineAdjustmentAlignBaselines
@property(nonatomic) UIBaselineAdjustment baselineAdjustment; 
//枚举值如下:
typedef NS_ENUM(NSInteger, UIBaselineAdjustment) {
    //根据原始基线将文本缩小到位置时使用
    UIBaselineAdjustmentAlignBaselines = 0, 
    //基线调整对准中心
    UIBaselineAdjustmentAlignCenters,
    //不进行基线调整
    UIBaselineAdjustmentNone,
};
//最低比例因子,默认为 0.0
@property(nonatomic) CGFloat minimumScaleFactor NS_AVAILABLE_IOS(6_0);

标签文本自动调整大小以适应宽度测试代码:

- (void)viewDidLoad {
    [super viewDidLoad];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectZero];
    label.text = @"测试标签";
    label.textColor = [UIColor redColor];
    label.font = [UIFont systemFontOfSize:18.0];
    label.adjustsFontSizeToFitWidth = YES;
    label.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
    label.minimumScaleFactor = 0.0;
    label.enabled = YES;
    [self.view addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.view);
        make.size.mas_offset(CGSizeMake(20, 50));
    }];  
}
屏幕快照 2019-10-25 下午9.35.10.png

@property(nonatomic) BOOL allowsDefaultTighteningForTruncation NS_AVAILABLE_IOS(9_0);

属性描述 : 如果断行模式是开始截断之前的截断模式之一,则会压缩字符间的间距,以使行宽度大于可用空间。执行的最大收紧量由系统根据上下文(如字体、行宽等)确定。默认为NO。

@property(nonatomic) BOOL allowsDefaultTighteningForTruncation NS_AVAILABLE_IOS(9_0);
实现文本居上与居下的标签
//  YSCCustomLabel.h
//  可以实现文本居上与居下的标签

#import <UIKit/UIKit.h>

typedef enum {
    //垂直对齐居上
    VerticalAlignmentTop = 0,
    //垂直对齐居中
    VerticalAlignmentMiddle,
    //垂直对齐居下
    VerticalAlignmentBottom
}VerticalAlignment;

@interface YSCCustomLabel : UILabel

@property (nonatomic) VerticalAlignment verticalAlignment;

@end

//
//  YSCCustomLabel.m

#import "YSCCustomLabel.h"

@implementation YSCCustomLabel

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.verticalAlignment = VerticalAlignmentMiddle;
    }
    return self;
}

- (void)setVerticalAlignment:(VerticalAlignment)verticalAlignment {
    _verticalAlignment = verticalAlignment;
    [self setNeedsLayout];
}


- (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:
        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

居上:


屏幕快照 2019-08-07 下午8.50.52.png

居中:


屏幕快照 2019-08-07 下午8.58.46.png
居下:
屏幕快照 2019-08-07 下午9.04.57.png
处理UILabel时的问题记录

在处理程序界面时遇到bug要求到图片位置时自动换行,bug如图:


屏幕快照 2019-07-02 下午5.05.22.png

自动换行么,简单,随即设置了UILabel的numberOfLines = 0;
发现效果是这样的,我去。

屏幕快照 2019-07-02 下午5.09.15.png

因为后台返回的数据格式类似这样:2019-06-12 00:00:00 ~ 2019-06-13 00:00:00,日期与时间中间是存在空格的。
后来修改UILabel的lineBreakMode属性为NSLineBreakByCharWrapping,

效果如下: 屏幕快照 2019-07-02 下午5.23.09.png
这只是一种方法的尝试,未必能解决其他情景下的UILabel换行问题。

相关文章

  • UILabel学习笔记

    UILabel摘要 一种显示一行或多行只读文本的视图,通常与控件一起用于描述它们的预期用途。 UILabel常用属...

  • IOS基础UI操作UILabel

    UILabel之小白学习纪录篇 代码创建UILabel 改变文字内容 改变文字颜色 改变字体大小 改变字体排列方式...

  • swift学习笔记-UI篇之UILabel

    1. UILabel的作用   UILabel用来在界面上显示一行或多行文本。 2. UILabel的创建 3. ...

  • UILabel常用属性

    //学习内容 /* 1.控件 UIView UILabel UITextField UITextView UIBu...

  • UILable字体

    //学习内容1.控件 UIView UILabel UITextField UITextView UIButton...

  • Swift UILabel学习

    let label = UILabel(frame: CGRect(x: 10, y: 240, width: 3...

  • iOS-UILable详细介绍

    //创建uilabel UILabel *label1 = [[UILabel alloc] initWithFr...

  • iOS UILabel geekband

    //创建uilabel UILabel *label1 = [[UILabel alloc] initWithFr...

  • UILabel带下划线实现

    创建uilabel对象 UILabel*Label2 = [[UILabel alloc]initWithFram...

  • 图文混排

    1.给View添加UILabel UILabel *label = [[UILabel alloc] init];...

网友评论

      本文标题:UILabel学习笔记

      本文链接:https://www.haomeiwen.com/subject/xfnadctx.html