文字前后在UILabel中画线 [英] Draw line in UILabel before and after text
本文介绍了文字前后在UILabel中画线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在 UILabel
中的字符串前后绘制一条线,就像下面的图片一样:
I want to draw a line before and after a string in UILabel
exactly like the following image:
PS:文本不是静态的,所以我不能说出确切的文本宽度是多少,否则我会在文本前后放置两行静态行。
PS: the text is not static, so i can't tell what is the exact text width, otherwise i would put two static lines before and after the text.
PS :另外,我不能将标签放在一行的上方,也不能给标签背景颜色提供与视图背景相同的颜色,因为我在下部视图中具有渐变颜色。
PS: Also i can't put the label above a line and give the label background color same to the view background because i have gradient color in the lower view.
推荐答案
我希望下面是您想要的...
I Hope below is what you was looking for...
#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];
}
这篇关于文字前后在UILabel中画线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文