转载自:http://dong-zsh.github.io/2015/10/14/%E8%AE%BE%E7%BD%AElable%E5%86%85%E5%AE%B9%E4%B8%8D%E4%B8%8A%E4%B8%8B%E5%B1%85%E4%B8%AD/
#import <UIKit/UIKit.h> typedef enum { VerticalAlignmentTop = 0, |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| #import "mylable.h"
@implementation mylable
@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: |
###用法
1 2 3 4 5 6
| mylable *lable = [[mylable alloc] initWithFrame:CGRectMake(10, 400, 100, 200)]; lable.text = @"dajdiojajdajdjajdadakdajdiaodaiojdioajdioajidjaijdajdadadna"; lable.backgroundColor = [UIColor grayColor]; lable.numberOfLines = 0; [lable setVerticalAlignment:VerticalAlignmentTop]; [self.view addSubview:lable];
|