UILabel笔记

这篇博客详细介绍了UILabel的属性和方法,包括NSLineBreakMode文本显示模式,交互开关的差异,adjustsFontSizeToFitWidth和minimumScaleFactor的用法,preferredMaxLayoutWidth在自动布局中的作用,以及如何设置和计算富文本attributedText的高度。文章还提醒注意特殊情况,如空内容的高度计算和特殊字符的影响。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、属性

1.NSLineBreakMode 文本过长时的显示方式

typedef NS_ENUM(NSInteger, NSLineBreakMode) {
    NSLineBreakByWordWrapping = 0,      // Wrap at word boundaries, default
    NSLineBreakByCharWrapping,      // Wrap at character boundaries
    NSLineBreakByClipping,      // Simply clip
    NSLineBreakByTruncatingHead,    // Truncate at head of line: "...wxyz"
    NSLineBreakByTruncatingTail,    // Truncate at tail of line: "abcd..."
    NSLineBreakByTruncatingMiddle   // Truncate middle of line:  "ab...yz"
} NS_ENUM_AVAILABLE(10_0, 6_0);

UILineBreakModeWordWrap = 0,
以单词为单位换行,以单位为单位截断。
UILineBreakModeCharacterWrap,
以字符为单位换行,以字符为单位截断。
UILineBreakModeClip,
以单词为单位换行。以字符为单位截断。
UILineBreakModeHeadTruncation,
以单词为单位换行。如果是单行,则开始部分有省略号。如果是多行,则中间有省略号,省略号后面有4个字符。
UILineBreakModeTailTruncation,
以单词为单位换行。无论是单行还是多行,都是末尾有省略号。
UILineBreakModeMiddleTruncation,
以单词为单位换行。无论是单行还是多行,都是中间有省略号,省略号后面只有2个字符。

2.交互开关

@property(nonatomic,getter=isUserInteractionEnabled) BOOL userInteractionEnabled;  // default is NO
@property(nonatomic,getter=isEnabled)                BOOL enabled;                 // default is YES. changes how the label is drawn

两个属性是有区别的,userInteractionEnabled只是交互能力,不会影响其他属性;但enabled会使label失效变暗等。

3.adjustsFontSizeToFitWidth 是否缩小字体使其适应label宽度;
minimumScaleFactor 最小的缩小比例,从0到1;

4.preferredMaxLayoutWidth 用于支持autoLayout的最大宽度

5.attributedText 设置富文本
具体可以参考NSAttributedString这个类。

   NSMutableAttributedString *textLabelStr =[[NSMutableAttributedString alloc] initWithString:@"QQ:123456"];
    [textLabelStr setAttributes:@{NSForegroundColorAttributeName : [UIColor redColor], NSFontAttributeName : [UIFont systemFontOfSize:13]} range:NSMakeRange(3,6)];

    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(50, 100, 100, 50)];
    label.attributedText = textLabelStr;
    [self.view addSubview:label];
//效果:QQ是默认的黑色17字体,123456是红色13字体



二、方法
关于Label的文字高度计算,使用boundingRectWithSize:

NSString *text = _datasource[indexPath.row];

    // 段落设置与实际显示的 Label 属性一致 采用 NSMutableParagraphStyle 设置Nib 中 Label 的相关属性传入到 NSAttributeString 中计算;
    NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
    style.lineBreakMode = NSLineBreakByWordWrapping;
    style.alignment = NSTextAlignmentLeft;

    NSAttributedString *string = [[NSAttributedString alloc]initWithString:text attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16], NSParagraphStyleAttributeName:style}];

    CGSize size =  [string boundingRectWithSize:CGSizeMake(200.f, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil].size;
    NSLog(@" size =  %@", NSStringFromCGSize(size));

    // 并不是高度计算不对,我估计是计算出来的数据是 小数,在应用到布局的时候稍微差一点点就不能保证按照计算时那样排列,所以为了确保布局按照我们计算的数据来,就在原来计算的基础上 取ceil值,再加1;
    CGFloat height = ceil(size.height) + 1;

唯一缺陷,就是文字为空时,高度=13.8;所以在封装工具类的时候,必须判断内容是否为空,如果内容为空,是要返回一定高度还是返回0,这个需要自己取决。
另外,有时候特许字符如“\r”可能会影响最终的高度,建议使用NSCharacterSet等方法去掉这些字符。

三、NSAttributedString字符属性列表

NSFontAttributeName :字体字号
value值:UIFont类型

NSParagraphStyleAttributeName : 段落样式
value值:NSParagraphStyle类型(其属性如下)
(0)lineBreakMode = NSLineBreakByWordWrapping, 说明:这里段落的文字过长显示格式,切勿选择“省略号”方式,否则无法计算高度!
(1)lineSpacing 行间距(具体用法可查看上面的设置行间距API)
(2)paragraphSpacing 段落间距
(3)alignment 对齐方式
(4)firstLineHeadIndent 指定段落开始的缩进像素
(5)headIndent 调整全部文字的缩进像素

NSForegroundColorAttributeName 字体颜色
value值:UIColor类型

NSBackgroundColorAttributeName 背景颜色
value值:UIColor类型

NSObliquenessAttributeName 字体粗倾斜
value值:NSNumber类型

NSExpansionAttributeName 字体加粗
value值:NSNumber类型(比例) 0就是不变 1增加一倍

NSKernAttributeName 字间距
value值:CGFloat类型

NSUnderlineStyleAttributeName 下划线
value值:10

NSUnderlineColorAttributeName 下划线颜色
value值:UIColor类型

NSStrikethroughStyleAttributeName 删除线
value值:10

NSStrikethroughColorAttributeName 删除线颜色
value值:UIColor类型

NSStrokeColorAttributeName 字体颜色
value值:UIColor类型

NSStrokeWidthAttributeName 字体描边
value值:CGFloat

NSLigatureAttributeName 连笔字
value值:10

NSShadowAttributeName 阴影
value值:NSShawdow类型(下面是其属性)
(1)shadowOffset 影子与字符串的偏移量
(2)shadowBlurRadius 影子的模糊程度
(3)shadowColor 影子的颜色

NSLinkAttributeName 链接
value值:NSURL (preferred) or NSString类型

NSBaselineOffsetAttributeName 基准线偏移
value值:NSNumber类型

NSWritingDirectionAttributeName 文字方向 分别代表不同的文字出现方向
value值:@[@(1),@(2)]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值