2014-01-08 37 views
1

我有一个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来解决,我希望有人跑进入这个问题之前!

+0

添加链接到图像。 – Wain

+0

为什么要添加一个子视图并使用'drawRect'?你不应该自己调用'drawRect' ... – Wain

+0

我需要一个视图,因为我想让我的UILabel在它周围有间距。这UIView是这个周围的间距应该是圆角的形式。我创建了一个名为BubbleView的UIView的子类,并覆盖了drawRect类。当我没有把它放进去的时候,我甚至没有得到任何灰色方块。 – guest

回答

0

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

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

+0

我曾尝试设置框架,但由于我有自动布局,它不起作用:(只是在这篇文章中添加了图片 – guest

+0

我确实有行在setText中设置视图的背景颜色hostView.backgroundColor = [UIColor whiteColor ]; – guest

+0

我确实有行setText中设置视图的背景颜色hostView.backgroundColor = [UIColor whiteColor];我也为BubbleView BubbleView的initWithFrame * hostView = [[BubbleView alloc] initWithFrame:CGRectMake(20.0f,160.0f, textRect.size.width + 20,textRect.size.height + 20)]; - >我认为这是正确的吗?不知道发生了什么。 – guest

0

如果你想保存编码方法你强烈需要在您的drawRect:方法

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

    YOUR CODE 
} 

添加[super drawRect:rect]在这种情况下,你会看到在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

+0

刚刚添加,但结果是一样的:( – guest

+0

我刚在ios7模拟器中自己实现了这个代码。一切都很好,只要你想:没有方角和可视文本在BubbleView中。 – malex

+0

这非常有趣。我仍然没有得到这个。看起来文本和按钮之间的边距不是10px。之前我没有在下面看到视图时,我能够让文本显示在圆形的UILabel上(与您的类似)。当我在视图的顶部添加一个视图,并在视图顶部添加UILabel时,我打开了BubbleView覆盖文本的这个deadend ...你可以把你在github上的东西放在哪里?我想看看你做了什么!非常感谢您的帮助! – guest

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

+0

我将不得不明天尝试这个,并会让你知道。谢谢。现在是2.45AM .. – guest