UILabel TextAlignment 垂直顶部

发布于 2024-10-31 11:26:44 字数 1955 浏览 1 评论 0原文

我有一个很大的问题,我有一个以编程方式创建的 UILabel ,然后通过界面生成器进行连接,我已将其定位在我需要的位置,但我看到我在其中设置的文本打印在UILabelBox,我发现了很多问题,但我不知道我可以使用它,我发现了这个:

//
//  VerticallyAlignedLabel.h
//

#import <Foundation/Foundation.h>

typedef enum VerticalAlignment {
    VerticalAlignmentTop,
    VerticalAlignmentMiddle,
    VerticalAlignmentBottom,
} VerticalAlignment;

@interface VerticallyAlignedLabel : UILabel {
@private
    VerticalAlignment verticalAlignment_;
}

@property (nonatomic, assign) VerticalAlignment verticalAlignment;

@end

//
//  VerticallyAlignedLabel.m
//

#import "VerticallyAlignedLabel.h"


@implementation VerticallyAlignedLabel

@synthesize verticalAlignment = verticalAlignment_;

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

- (void)setVerticalAlignment:(VerticalAlignment)verticalAlignment {
    verticalAlignment_ = verticalAlignment;
    [self setNeedsDisplay];
}

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

任何人都可以帮助我如何使用它吗?

I have a great problem, I have a UILabel created programmatically and then connect via Interface builder, where i have positionated where I need, but i see that the text i set in it it's printed in the center of the UILabelBox, I have found a lot of question but i haven't know I can use it, I have found this:

//
//  VerticallyAlignedLabel.h
//

#import <Foundation/Foundation.h>

typedef enum VerticalAlignment {
    VerticalAlignmentTop,
    VerticalAlignmentMiddle,
    VerticalAlignmentBottom,
} VerticalAlignment;

@interface VerticallyAlignedLabel : UILabel {
@private
    VerticalAlignment verticalAlignment_;
}

@property (nonatomic, assign) VerticalAlignment verticalAlignment;

@end

//
//  VerticallyAlignedLabel.m
//

#import "VerticallyAlignedLabel.h"


@implementation VerticallyAlignedLabel

@synthesize verticalAlignment = verticalAlignment_;

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

- (void)setVerticalAlignment:(VerticalAlignment)verticalAlignment {
    verticalAlignment_ = verticalAlignment;
    [self setNeedsDisplay];
}

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

Anyone can help me how I can use it please?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

傲世九天 2024-11-07 11:26:44

我知道现在回答可能已经晚了,但我是这样做的:

我为 UILabel 创建了一个类别,并在需要时调用 -setVerticalAlignmentTop

// UILabel(VAlign).h
#import <Foundation/Foundation.h>
@interface UILabel (VAlign)
- (void) setVerticalAlignmentTop;
@end

// UILabel(VAlign).m
#import "UILabel(VAlign).h"
@implementation UILabel (VAlign)
- (void) setVerticalAlignmentTop
{
    CGSize textSize = [self.text sizeWithFont:self.font
                            constrainedToSize:self.frame.size
                                lineBreakMode:self.lineBreakMode];

    CGRect textRect = CGRectMake(self.frame.origin.x,
                                 self.frame.origin.y,
                                 self.frame.size.width,
                                 textSize.height);
    [self setFrame:textRect];
    [self setNeedsDisplay];
}

@end

I know it may be late to answer, but I did it this way:

I made a Category for UILabel and call -setVerticalAlignmentTop when it was needed.

// UILabel(VAlign).h
#import <Foundation/Foundation.h>
@interface UILabel (VAlign)
- (void) setVerticalAlignmentTop;
@end

// UILabel(VAlign).m
#import "UILabel(VAlign).h"
@implementation UILabel (VAlign)
- (void) setVerticalAlignmentTop
{
    CGSize textSize = [self.text sizeWithFont:self.font
                            constrainedToSize:self.frame.size
                                lineBreakMode:self.lineBreakMode];

    CGRect textRect = CGRectMake(self.frame.origin.x,
                                 self.frame.origin.y,
                                 self.frame.size.width,
                                 textSize.height);
    [self setFrame:textRect];
    [self setNeedsDisplay];
}

@end
用心笑 2024-11-07 11:26:44

您可以按如下方式进行操作......

  1. 从 IB 设置标签的 numberOfLines 属性 0

  2. 将标签的 lineBreakMode 设置为 UILineBreakModeWordWrap(非常非常重要)

现在无论您在标签上设置什么,只需在其上添加一些@“\n”......
前任。-

[yourTextLabel setText:@"myLabel\n\n\n\n\n"];

You can do it as follows.......

  1. Set your label's numberOfLines property 0 from IB

  2. Set your label's lineBreakMode as UILineBreakModeWordWrap (very very important)

now whatever you set on the label just append few @"\n" to it.....
ex.-

[yourTextLabel setText:@"myLabel\n\n\n\n\n"];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文