1.自定义个label
#import <UIKit/UIKit.h>
@interface CustomLabel : UILabel
@property (nonatomic, assign) UIEdgeInsets textInsets; // 控制字体与控件边界的间隙
@end
#import "CustomLabel.h"
@implementation CustomLabel
- (instancetype)init {
if (self = [super init]) {
_textInsets = UIEdgeInsetsZero;
}
return self;
}
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
_textInsets = UIEdgeInsetsZero;
}
return self;
}
- (void)drawTextInRect:(CGRect)rect {
[super drawTextInRect:UIEdgeInsetsInsetRect(rect, _textInsets)];
}
@end
2.使用
#import "ViewController.h"
#import "CustomLabel.h"
@interface ViewController (){
CustomLabel *regulationLabel;//中心抽奖规则lab
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
regulationLabel = [[CustomLabel alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width*6/7, self.view.frame.size.height*5/6)];
NSMutableParagraphStyle *paragStyle = [[NSMutableParagraphStyle alloc]init];
paragStyle.alignment = NSTextAlignmentLeft;
paragStyle.headIndent = 1*8.0f+2.0f;//行首缩进
// paragStyle.firstLineHeadIndent = 1*8.0f;//首行缩进
// paragStyle.lineSpacing = 4.0f;
NSDictionary *labDic = @{NSParagraphStyleAttributeName:paragStyle,
NSFontAttributeName:[UIFont systemFontOfSize:12],
NSForegroundColorAttributeName:[UIColor blackColor]};
NSAttributedString *attrString = [[NSAttributedString alloc]initWithString:[self regulationString] attributes:labDic];
regulationLabel.backgroundColor = [UIColor redColor];
regulationLabel.attributedText = attrString;
regulationLabel.numberOfLines = 0;//
[regulationLabel sizeToFit]; //自适应大小的方法 标签的大小由字体的大小长度决定
regulationLabel.center = self.view.center;
regulationLabel.lineBreakMode = NSLineBreakByCharWrapping; //根据xx进行换行
regulationLabel.userInteractionEnabled = YES;
regulationLabel.textInsets = UIEdgeInsetsMake(0, 10, 10, 10);//设置内边距
[self.view addSubview:regulationLabel];
}
- (NSString *)regulationString{
NSString *ruleString = @"1.Do any additional setup after loading the view, typically from a nib.Do any additional setup after loading the view, typically from a nib.Do any additional setup after loading the view, typically from a nib.Do any additional setup after loading the view, typically from a nib.Do any additional setup after loading the view, typically from a nib.Do any additional setup after loading the view, typically from a nib.ffffff";
return ruleString;
}
网友评论