我有一个UIView,我创建并设置为白色的背景颜色。该视图包含UILabel,它是一个名为BubbleView的类。 (对不起,我不能添加图片,因为你需要信誉10+ :(UIView内的UILabel产生与“方角”的圆角
问题: 1.下面的代码产生一个灰色的标签与圆角与灰色边框的方角提示。这是因为UIView产生的方形请注意,我已将UIView的背景设置为白色 2.我的UILabel文本字符串隐藏在UIView后面,因此不显示。
我很想向你展示图片,但我是新的,并且我无法添加图片,直到我获得10个以上的声望。
http://i.stack.imgur.com/CdRjy.png http://i.stack.imgur.com/zCdCV.png
这是我的用于设置文本和视图代码:
BubbleView:
- (void)drawRect:(CGRect)rect
{
const CGFloat boxWidth = self.bubbleWidth;
const CGFloat boxHeight = self.bubbleHeight;
NSLog(@"text, width, height: %@, %f, %f", self.text, self.bubbleWidth, self.bubbleHeight);
CGRect boxRect = CGRectMake(
roundf(self.bounds.size.width - boxWidth)/2.0f,
roundf(self.bounds.size.height - boxHeight)/2.0f,
boxWidth,
boxHeight);
UIBezierPath *roundedRect = [UIBezierPath bezierPathWithRoundedRect:boxRect cornerRadius:14.0f];
[[UIColor colorWithWhite:0.3f alpha:0.8f] setFill];
[roundedRect fill];
NSDictionary *attributes = @{
NSFontAttributeName : [UIFont systemFontOfSize:16.0f],
NSForegroundColorAttributeName : [UIColor whiteColor]
};
CGPoint textPoint = CGPointMake(
self.center.x+boxWidth/2,
self.center.y+boxHeight/2);
NSLog(@"text point origin: %f, %f", textPoint.x, textPoint.y);
[self.text drawAtPoint:textPoint withAttributes:attributes];
}
主视图控制器:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor];
[self setText];
}
-(void) viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
[self setText];
}
- (void) setText
{
NSString *textR = @"I need this text to show up on autolayout so that i could continue working";
UIFont* font = [UIFont fontWithName:@"HelveticaNeue" size:14.0f];
CGSize constraint = CGSizeMake(250,9999);
CGRect textRect = [textR boundingRectWithSize:constraint
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{NSFontAttributeName:font}
context:nil];
BubbleView *hostView = [[BubbleView alloc] initWithFrame:CGRectMake(20.0f, 160.0f, textRect.size.width+20, textRect.size.height+20)];
hostView.bubbleWidth = textRect.size.width+20;
hostView.bubbleHeight = textRect.size.height+20;
hostView.text = textR;
hostView.layer.cornerRadius = 10.0f;
self.view.layer.masksToBounds = TRUE;
[hostView drawRect:textRect];
hostView.backgroundColor = [UIColor whiteColor];
self.detailsView = hostView;
//self.detailsView.backgroundColor = [UIColor whiteColor];
NSLog(@"size: %f, %f", textRect.size.width, textRect.size.height);
NSLog(@"origin: %f, %f - size: %f, %f, backgroundColor: @%@", self.detailsView.frame.origin.x, self.detailsView.frame.origin.y, self.detailsView.frame.size.width, self.detailsView.frame.size.height, self.detailsView.backgroundColor);
[self.view addSubview:self.detailsView];
self.hostSays.text = textR;
self.hostSays.textColor = [UIColor blueColor];
[self.view layoutSubviews];
}
解决方案(只1部分): 好,所以我设法解决了我的一半问题。我不得不在BubbleView类中添加下面的代码(在 - (id)initWithFrame:(CGRect)框架中)。这摆脱了方角! (我认为韦恩是谁提出这一个,但我可能误解了他)......
[self setOpaque:NO];
[self setBackgroundColor:[UIColor clearColor]];
。所以......还是有问题,另一部分2来解决,我希望有人跑进入这个问题之前!
添加链接到图像。 – Wain
为什么要添加一个子视图并使用'drawRect'?你不应该自己调用'drawRect' ... – Wain
我需要一个视图,因为我想让我的UILabel在它周围有间距。这UIView是这个周围的间距应该是圆角的形式。我创建了一个名为BubbleView的UIView的子类,并覆盖了drawRect类。当我没有把它放进去的时候,我甚至没有得到任何灰色方块。 – guest