Iphone 将标签放置在圆圈中

Iphone 将标签放置在圆圈中,iphone,ios,uilabel,drawrect,Iphone,Ios,Uilabel,Drawrect,我想将UILabel放置在圆的中心,但我似乎无法影响标签的位置。我似乎只能通过更改CGRect帧的高度来影响标签的位置。更改其他值根本不会影响位置 这是我的圆圈。m代码 - (id)initWithFrame:(CGRect)frame radius:(CGFloat)aRadius color:(UIColor*) aColor { self = [super initWithFrame:frame]; if (self) { self.opaque = NO;

我想将UILabel放置在圆的中心,但我似乎无法影响标签的位置。我似乎只能通过更改CGRect帧的高度来影响标签的位置。更改其他值根本不会影响位置

这是我的圆圈。m代码

- (id)initWithFrame:(CGRect)frame radius:(CGFloat)aRadius color:(UIColor*) aColor {
    self = [super initWithFrame:frame];
    if (self) {
        self.opaque = NO;

        [self setRadius:aRadius];
        [self setColor:aColor];
    }
    return self;
}


- (void)drawRect:(CGRect)rect
{

    NSString *string = @"1";
    UIFont* font = [UIFont systemFontOfSize:80];
    UILabel *label = [[UILabel alloc] init];
    label.text = string;
    label.textColor = [UIColor whiteColor];
    label.font = font;

    CGRect frame = label.frame;
    frame = CGRectMake(10, 10, 0, 85);
    label.frame = frame;

    CGContextRef contextRef = UIGraphicsGetCurrentContext();
    [color setFill];
    circle = CGRectMake(0, 0, radius, radius);

    CGContextAddEllipseInRect(contextRef, circle);
    CGContextDrawPath (contextRef, kCGPathFill);
    [label drawRect:circle];

}
还有我的viewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    CGFloat radius = 70;
    CGRect position = CGRectMake(0, 0, radius, radius);
    Circle *myCircle = [[Circle alloc] initWithFrame:position radius:radius color:[UIColor redColor]];
    [self.view addSubview:myCircle];

}

您不应该在
drawRect:
中分配新的
UIView
(并且
UILabel
UIView
的子类)。有几种很好的方法可以满足您的需要,但没有一种方法涉及在
drawRect:
中分配新的
UILabel

一种方法是让圆在其初始值设定项中为自己指定一个
UILabel
子视图,并在
layoutSubviews
中居中放置标签。然后在
drawRect:
中,您只需绘制圆圈,而不必担心绘制标签文本:

@implementation Circle {
    UILabel *_label;
}

@synthesize radius = _radius;
@synthesize color = _color;

- (id)initWithFrame:(CGRect)frame radius:(CGFloat)aRadius color:(UIColor*) aColor {
    self = [super initWithFrame:frame];
    if (self) {
        self.opaque = NO;

        [self setRadius:aRadius];
        [self setColor:aColor];

        _label = [[UILabel alloc] init];
        _label.font = [UIFont systemFontOfSize:80];
        _label.textColor = [UIColor whiteColor];
        _label.text = @"1";
        [_label sizeToFit];
        [self addSubview:_label];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    CGSize mySize = self.bounds.size;
    _label.center = CGPointMake(mySize.width * 0.5f, mySize.height * 0.5f);
}

- (void)drawRect:(CGRect)rect {
    [self.color setFill];
    CGSize mySize = self.bounds.size;
    CGFloat radius = self.radius;
    [[UIBezierPath bezierPathWithOvalInRect:CGRectMake(mySize.width * 0.5f, mySize.height * 0.5f, self.radius, self.radius)] fill];
}

您不应该在
drawRect:
中分配新的
UIView
(并且
UILabel
UIView
的子类)。有几种很好的方法可以满足您的需要,但没有一种方法涉及在
drawRect:
中分配新的
UILabel

一种方法是让圆在其初始值设定项中为自己指定一个
UILabel
子视图,并在
layoutSubviews
中居中放置标签。然后在
drawRect:
中,您只需绘制圆圈,而不必担心绘制标签文本:

@implementation Circle {
    UILabel *_label;
}

@synthesize radius = _radius;
@synthesize color = _color;

- (id)initWithFrame:(CGRect)frame radius:(CGFloat)aRadius color:(UIColor*) aColor {
    self = [super initWithFrame:frame];
    if (self) {
        self.opaque = NO;

        [self setRadius:aRadius];
        [self setColor:aColor];

        _label = [[UILabel alloc] init];
        _label.font = [UIFont systemFontOfSize:80];
        _label.textColor = [UIColor whiteColor];
        _label.text = @"1";
        [_label sizeToFit];
        [self addSubview:_label];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    CGSize mySize = self.bounds.size;
    _label.center = CGPointMake(mySize.width * 0.5f, mySize.height * 0.5f);
}

- (void)drawRect:(CGRect)rect {
    [self.color setFill];
    CGSize mySize = self.bounds.size;
    CGFloat radius = self.radius;
    [[UIBezierPath bezierPathWithOvalInRect:CGRectMake(mySize.width * 0.5f, mySize.height * 0.5f, self.radius, self.radius)] fill];
}

您确实意识到0的高度没有显示任何内容,对吗?同意,标签框的0高度没有意义。看到宽度和高度为半径的圆圈框架也有点奇怪,因为圆圈的直径至少是半径的两倍,对吗?如果您要做一些简单的事情,比如将标签宽度设置为圆半径的两倍,您可能还需要确保标签使用的是
[label setTextAlignment:UITextAlignmentCenter],对吗?只是为了确保文本在该框架内居中对齐?您确实意识到0的高度不会显示任何内容,对吗?同意,标签框架的0高度没有意义。看到宽度和高度为半径的圆圈框架也有点奇怪,因为圆圈的直径至少是半径的两倍,对吗?如果您要做一些简单的事情,比如将标签宽度设置为圆半径的两倍,您可能还需要确保标签使用的是
[label setTextAlignment:UITextAlignmentCenter],对吗?只是为了确保文本在该框架内居中对齐?谢谢你的代码。您不想在drawrect中分配UIView的原因是因为drawrect可以被多次调用吗?这样,您每次都会重新分配视图?您不应该分配一个
UILabel
,只是为了画一个字符串,然后扔掉
UILabel
。如果您只想绘制字符串,那么文本绘制API的成本要低得多。如果要将
UILabel
作为视图层次结构的一部分(就像我的示例代码那样),可以使用
UILabel
绘制字符串。如果您在
drawRect:
中分配视图,则表明您的工作效率低下。感谢您提供的代码。您不想在drawrect中分配UIView的原因是因为drawrect可以被多次调用吗?这样,您每次都会重新分配视图?您不应该分配一个
UILabel
,只是为了画一个字符串,然后扔掉
UILabel
。如果您只想绘制字符串,那么文本绘制API的成本要低得多。如果要将
UILabel
作为视图层次结构的一部分(就像我的示例代码那样),可以使用
UILabel
绘制字符串。如果您在
drawRect:
中分配视图,则表明您的工作效率低下。