你好,歡迎來到IOS教程網

 Ios教程網 >> IOS訊息 >> 關於IOS >> 可調整內邊距的UILabel

可調整內邊距的UILabel

編輯:關於IOS

繼承UILabel,重載drawTextInRect方法。

//1.header file
#import <UIKit/UIKit.h>

@interface InsetsLabel : UILabel
@property(nonatomic) UIEdgeInsets insets;
-(id) initWithFrame:(CGRect)frame andInsets: (UIEdgeInsets) insets;
-(id) initWithInsets: (UIEdgeInsets) insets;
@end

//2. implementation file
@implementation InsetsLabel
@synthesize insets=_insets;
-(id) initWithFrame:(CGRect)frame andInsets:(UIEdgeInsets)insets {
self = [super initWithFrame:frame];
if(self){
self.insets = insets;
}
return self;
}

-(id) initWithInsets:(UIEdgeInsets)insets {
self = [super init];
if(self){
self.insets = insets;
}
return self;
}

-(void) drawTextInRect:(CGRect)rect {
return [super drawTextInRect:UIEdgeInsetsInsetRect(rect, self.insets)];
}
@end

 

調用部分

InsetsLabel * lblTitle=[[InsetsLabel alloc] initWithFrame:CGRectMake(0, 35+25*i, 185, 22)];
[lblTitle setInsets:UIEdgeInsetsMake(0, 5, 0, 5)];

 

 

 

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved