UIView内部的UILabel产生带有“方角”的圆角

时间:2014-01-08 08:20:31

标签: objective-c uiview storyboard uilabel

我创建了一个UIView,并将背景颜色设置为白色。该视图包含UILabel,它是一个名为BubbleView的类。 (抱歉,我无法添加图片,因为您需要声望10+ :(

问题: 1.下面的代码生成一个带有圆角的灰色标签,带有灰色边框方角。这是因为UIView产生方形角尖。 UILabel圆润。请注意,我已经将UIView的背景设置为白色。 我的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类中添加以下代码(inside - (id)initWithFrame:(CGRect)frame)。这摆脱了方角! (我认为Wain是那个提出这个但我可能误解了他的人)......

[self setOpaque:NO];
[self setBackgroundColor:[UIColor clearColor]];

所以...仍然有问题的另一部分要解决,我希望有人之前遇到过这个问题!

3 个答案:

答案 0 :(得分:0)

设置视图的背景颜色并将标签添加为子视图。设置框架以获得所需的填充。不要实施drawRect

现在,视图将自动绘制背景颜色和标签,标签将绘制文本(带有背景颜色和边框设置)。

答案 1 :(得分:0)

如果您想保存编码方法,则需要在[super drawRect:rect]方法中添加drawRect:

- (void)drawRect:(CGRect)rect{
    [super drawRect:rect];

    YOUR CODE
}

在这种情况下,您会在UILabel

中看到您的文字

此外,您不应直接致电drawRect:。它将在运行时自动调用:

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;

enter image description here

答案 2 :(得分:0)

我知道当我创建自定义按钮时,我需要setMasksToBounds

    + (void) addBorderToButtons:(UIButton *) btn
    {
        // Round button corners
        CALayer *btnLayer = [btn layer];
        [btnLayer setMasksToBounds:YES];
        [btnLayer setCornerRadius:15.0f];

        // Apply a 1 pixel, black border around Buy Button
        [btnLayer setBorderWidth:1.5f];
        [btnLayer setBorderColor:[[UIColor blackColor] CGColor]];
    }

设置此更改

enter image description here

到此

enter image description here

相关问题