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

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;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,665评论 19 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 179,238评论 25 708
  • 感情这回事 真的太脆弱 一给再给的心 如何不造作 自私与自爱 是谁在折磨 施舍一颗糖果 就可以被蛊惑 戏里的欢乐和...
    洛家仁人阅读 289评论 0 0
  • 那天在书店,看到精装版的《双城记》,封面和年少买这本书时候的已经大相径庭,扉页上却依然印着那一段话说“这是最幸运的...
    爱吹故事的张多多阅读 2,411评论 5 36

友情链接更多精彩内容