在文本之前和之后在UILabel中绘制线条

时间:2014-12-04 10:04:25

标签: objective-c uilabel drawrect nsattributedstring core-text

我想在UILabel中的字符串前后绘制一行,与下图完全相同:

PS:文字不是静态的,所以我不能说出确切的文字宽度,否则我会在文字前后放两条静态线。

PS:此外,我不能将标签放在一条线上,并将标签背景颜色与视图背景相同,因为我在下方视图中有渐变色。

UILabel with line around text

2 个答案:

答案 0 :(得分:4)

我希望下面是你要找的......

#import "ViewController.h"

@interface ViewController () {
    UILabel *textLabel;
    UILabel *fakeTextLabel;
    UILabel *lineLabel1;
    UILabel *lineLabel2;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor lightGrayColor];
    NSString *labelText = @"  MOBILE INTERNET  ";
    fakeTextLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];
    fakeTextLabel.backgroundColor = [UIColor blackColor];
    fakeTextLabel.textColor = [UIColor whiteColor];
    fakeTextLabel.text = labelText;
    [fakeTextLabel sizeToFit]; // this is very important...
    fakeTextLabel.hidden = YES;
    [self.view addSubview:fakeTextLabel];

    int lineWidth = (320-fakeTextLabel.frame.size.width);
    lineWidth = lineWidth / 2.00;
    textLabel = [[UILabel alloc] initWithFrame:CGRectMake(320-lineWidth-fakeTextLabel.frame.size.width, 200, fakeTextLabel.frame.size.width, 100)];
    textLabel.text = labelText;
    textLabel.textColor = [UIColor greenColor];
    [self.view addSubview:textLabel];


    lineLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 249, lineWidth, 2)];
    lineLabel1.text = @"";
    lineLabel1.backgroundColor = [UIColor redColor];

    lineLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(lineWidth+fakeTextLabel.frame.size.width, 249, lineWidth, 2)];
    lineLabel2.text = @"";
    lineLabel2.backgroundColor = [UIColor redColor];

    [self.view addSubview:lineLabel1];
    [self.view addSubview:lineLabel2];
}

答案 1 :(得分:2)

您可以通过多种方式实现这一目标。

方式1。有意见。

UIView *containerView = [[UIView alloc] initWithFrame:...];// The view that holds background, your label and lines
containerView.backgroundColor = [UIColor clearColor];
[containerView addSubView:bgImageView];
[containerView addSubView:label];
[label sizeToFit];
label.center = CGPointMake(containerView.frame.size.width/2, someY);

CGFloar margin = 5;
UIView *leftLineView = [UIView new];
leftLineView.backgroundColor = [UIColor whiteColor];
leftLineView.frame = CGRectMake(0, 0, containerView.frame.size.width - label.frame.size.width - margin, 0.5f); 
leftLineView.center = CGPointMake(leftLineView.center.x, label.center.y);

UIView *rightLineView = [UIView new];
rightLineView.backgroundColor = [UIColor whiteColor];
rightLineView.frame = CGRectMake(label.frame.origin.x + label.frame.size.width + margin, 0, containerView.frame.size.width - label.frame.size.width - margin, 0.5f); 
rightLineView.center = CGPointMake(rightLineView.center.x, label.center.y);

[containerView addSubiew:leftLineView];
[containerView addSubiew:rightLineView];

方式2。使用CoreGraphics。

制作自定义UIView并在-drawRect:方法中绘制线条。

- (void)drawRect:(CGRect)rect
{
    CGContextRef currentContext = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(currentContext, 0.5f);
    CGContextMoveToPoint(currentContext, 0.0f, self.label.center.y);
    CGContextAddLineToPoint(currentContext, self.label.origin.x - margin , self.label.center.y);
    CGContextMoveToPoint(currentContext, self.label.origin.x + self.label.frame.size.width + margin, self.label.center.y);    
    CGContextAddLineToPoint(currentContext, self.frame.size.width, self.label.center.y);    
}

像这样:)

请注意,我直接在SO编辑器中而不是在Xcode中编写此代码,因此可能会出现一些语法错误。