【问题标题】:UILabel TextAlignement Vertical TopUILabel TextAlignement 垂直顶部
【发布时间】:2011-04-09 12:19:07
【问题描述】:

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

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

谁能帮助我如何使用它?

【问题讨论】:

    标签: cocoa-touch ios uilabel


    【解决方案1】:

    我知道现在回答可能会晚,但我是这样做的:
    我为 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
    

    【讨论】:

    • 以防万一有人使用它并发现它不起作用:UILabel 的文本必须在设置对齐之前设置,否则将在没有文本的情况下进行计算。只是说:-)
    • 谢谢,蒙肯。那行得通:P 我认为今晚的编码已经足够了!
    • 这简直太完美了!很好的解决方案。
    • 您的解决方案对我也很有效。而且我想补充一点,我使用的类别越多,我就越喜欢它们!
    • 不调用新方法,不能对所有标签都自动完成吗?
    【解决方案2】:

    你可以这样做......

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

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

    现在无论您在标签上设置什么,只需在其上附加几个 @"\n" ..... 例如-

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

    【讨论】:

    • 这个问题几乎把我逼疯了,直到找到你的解决方案!谢谢!无论如何,Apple 应该实现一个功能来对齐标签......
    猜你喜欢
    • 2014-05-20
    • 2010-11-06
    • 2015-10-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多