iOS开发之三:常用控件--UILabel的使用

UILabel 一般用来显示文本内容。

常用的属性如下:

    @property(nonatomic,copy)   NSString           *text;           // 文本的内容,默认为 nil
    @property(nonatomic,retain) UIFont             *font;            // 文本的字体,默认为nil (system font 17 plain)
    @property(nonatomic,retain) UIColor            *textColor;       // 文本的颜色,默认为 nil (text draws black)
    @property(nonatomic,retain) UIColor            *shadowColor;     // 文本的阴影,默认为 nil (没有阴影),如果要设置阴影,则需要设置偏移量
    @property(nonatomic)        CGSize             shadowOffset;    // 设置偏移量
    @property(nonatomic)        NSTextAlignment    textAlignment;   // 文本对齐方式,默认左对齐
    @property(nonatomic)        NSLineBreakMode    lineBreakMode;   // 文本超出frame时的截取方式
    @property(nonatomic,retain)               UIColor *highlightedTextColor; // 文本选中时的高亮颜色
    @property(nonatomic,getter=isHighlighted) BOOL     highlighted;          // 是否存在高亮,默认为NO
    
    @property(nonatomic,getter=isUserInteractionEnabled) BOOL userInteractionEnabled;  // 用户交互是否打开,默认为NO。
UILabel的用法,实例代码:

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(90, 100, 140, 40)];
    //背景为红色
    label.backgroundColor = [UIColor redColor];
    //文本内容
    label.text = @"测试文本";
    //文本字体
    label.font = [UIFont systemFontOfSize:14];
    //文本行数
    label.numberOfLines = 0; // no limit
    //Label宽度不够时,对文本进行打断的方式
    label.lineBreakMode = NSLineBreakByTruncatingHead;
    //文本的阴影 颜色
    label.shadowColor = [UIColor yellowColor];
    //文本的阴影偏移量
    label.shadowOffset = CGSizeMake(-2, 2);
    //文本对齐方式,参数是枚举类型,有左中右三种对齐方式
    label.textAlignment = NSTextAlignmentCenter;
    //文本的字体颜色
    label.textColor = [UIColor blueColor];
    //根据文本自动调整label的宽度和高度
    [label sizeToFit];
一些好看的UILabel效果:http://code4app.com/category/label

我们用的比较多的UILabel的第三方开源库RTLabel,下载地址:https://github.com/honcheng/RTLabel

至于RTLabel的用法,我就不介绍了,百度一下,还挺多的。code4app上也有demo。

原文地址:https://www.cnblogs.com/wanghang/p/6298904.html