UILabel文本内容顶部对齐

本文介绍如何创建一个名为HJLabel的UILabel子类,实现文本内容顶部对齐,并提供获取文本实际占用尺寸的功能。通过示例代码展示具体实现方法。

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

这里我们创建一个继承自UILabel的子类 HJLabel


  • HJLabel.h
//
//  HJLabel.h
//  UILabelTest
//
//  Created by 黄健 on 16/5/25.
//  Copyright © 2016年 黄健. All rights reserved.
//

#import <UIKit/UIKit.h>

typedef enum {
    HJTextVerticalAlignmentTop,
    HJTextVerticalAlignmentMiddle,
    HJTextVerticalAlignmentBottom,
} HJTextVerticalAlignment;

@interface HJLabel : UILabel

/**
 *  @author 黄健, 2016-05-25 20:05:54
 *
 *  @brief 设置label的垂直对齐方式,包括顶部对齐、居中对齐和底部对齐
 */
@property (nonatomic, assign) HJTextVerticalAlignment textVerticalAlignment;

@end

  • HJLabel.m

//
//  HJLabel.m
//  UILabelTest
//
//  Created by 黄健 on 16/5/25.
//  Copyright © 2016年 黄健. All rights reserved.
//

#import "HJLabel.h"

@implementation HJLabel

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

- (void)setTextVerticalAlignment:(HJTextVerticalAlignment)textVerticalAlignment
{
    _textVerticalAlignment = textVerticalAlignment;
    [self setNeedsDisplay];
}

- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines
{
    CGRect textRect = [super textRectForBounds:bounds limitedToNumberOfLines:numberOfLines];

    switch (self.textVerticalAlignment)
    {
        case HJTextVerticalAlignmentTop:
            textRect.origin.y = bounds.origin.y;
            break;
        case HJTextVerticalAlignmentBottom:
            textRect.origin.y = bounds.origin.y + bounds.size.height - textRect.size.height;
            break;
        case HJTextVerticalAlignmentMiddle:
            // 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

  • 测试

#define content1 @"红尘之上,浮生若梦,一帘忧思,一缕情思,邂逅相逢转角遇见爱,奈何几时寻得有缘人。"

    HJLabel *myLabel = [[HJLabel alloc] init];
    myLabel.frame = CGRectMake(35, 450, 250, 80);
    myLabel.backgroundColor = [UIColor cyanColor];
    myLabel.text = content1;
    myLabel.numberOfLines = 0;
    myLabel.font = [UIFont systemFontOfSize:14];
    myLabel.textVerticalAlignment = HJTextVerticalAlignmentTop;
    [self.view addSubview:myLabel];

  • 运行

这里写图片描述


小技能:获取文本内容实际占用的尺寸

以上面的测试代码为例,追加以下代码


NSDictionary *attr = @{NSFontAttributeName : [UIFont systemFontOfSize:14]};

    /**
     *  @author 黄健, 2016-05-26 10:05:44
     *
     *  @brief 获取文字内容的实际宽高
     *
     *  @param 250      指定宽度
     *  @param MAXFLOAT 高度任意
     *
     *  @return 返回计算后得到的宽高
     */
    CGSize contentSize = [content1 boundingRectWithSize:CGSizeMake(250, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:attr context:nil].size;

    NSLog(@"实际宽高:%@", NSStringFromCGSize(contentSize)); // {238, 50.12109375}

  • 运行

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值