美文网首页
ios - label文本对齐设置内边距

ios - label文本对齐设置内边距

作者: 那一处风景ljz | 来源:发表于2017-07-26 11:15 被阅读399次

    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;
    }
    

    相关文章

      网友评论

          本文标题:ios - label文本对齐设置内边距

          本文链接:https://www.haomeiwen.com/subject/fwmnkxtx.html