如何使用未隐藏的填充文本绘制UILabel?

时间:2014-07-15 09:43:19

标签: ios

我将用代码

绘制UILabel
CustomLabel *label = [[CustomLabel alloc] initWithFrame:CGRectMake(0, 0, 260, 100)];
label.layer.borderColor = [[UIColor redColor] CGColor];
label.layer.borderWidth = 3.0;
[label setFont:[UIFont fontWithName:@"Helvetica" size:16]];
CALayer * l1 = [label layer];
[l1 setMasksToBounds:YES];
[l1 setCornerRadius:10.0];

label.backgroundColor = [UIColor clearColor];
label.textAlignment = NSTextAlignmentLeft; // UITextAlignmentCenter, UITextAlignmentLeft
label.textColor=[UIColor whiteColor];
label.backgroundColor = [UIColor redColor];
label.numberOfLines=4;
label.lineBreakMode=NSLineBreakByWordWrapping;
label.text = [NSString stringWithFormat:@"%@", message];
label.tag = index;


[label sizeToFit];

当然,我会从stackoverflow找到一个用于标签创建填充的解决方案 代码

- (id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.edgeInsets = UIEdgeInsetsMake(5,5,5,5); //customize padding here
    }
    return self;
}

- (void)drawTextInRect:(CGRect)rect {
    [super drawTextInRect:UIEdgeInsetsInsetRect(rect, self.edgeInsets)];
}

一切正常,但我的标签中的文字显示不好,这是我的标签截图。

enter image description here

我想要这样。

enter image description here

请帮助并感谢您的时间。

1 个答案:

答案 0 :(得分:1)

你想要看看这个库..它真棒,完全可配置...... https://github.com/AlexBarinov/UIBubbleTableView