[英]Can't implement UIView subclass
I am trying to create a UIView which is basically a number with a circular background. 我试图创建一个UIView,基本上是一个带有圆形背景的数字。 I am using a UIView and applying a corner radius of half the dimension for the circle.
我正在使用UIView,并将圆角半径的一半应用到圆上。 Then I am adding the number as a UILabel subview to the above view.
然后,我将数字作为UILabel子视图添加到上述视图中。
What I want is close to this (without the fancy border): (This is a screenshot from the app Count Battle). 我想要的就是这个(没有花哨的边框):(这是来自Count Battle应用程序的屏幕截图)。
Please help me rewrite by moving the code under the proper methods (drawrect, init, layoutSubviews, or any custom method). 请通过在适当的方法(drawrect,init,layoutSubviews或任何自定义方法)下移动代码来帮助我重写。 This is the code in it's current form.
这是当前形式的代码。 I think my understanding of this thing is muddled up, and this just doesn't look right.
我认为我对这件事的理解被弄糊涂了,这看起来并不对。
This is the header file: 这是头文件:
// CircleNumView.h
#import <UIKit/UIKit.h>
@interface CircleNumView : UIView
@property (nonatomic, strong) UIColor *circleColor;
- (instancetype)initWithRadius:(CGFloat)radius
center:(CGPoint)center
text:(NSString *)text;
@end
This is the implementation file: 这是实现文件:
// CircleNumView.m
#import "CircleNumView.h"
@interface CircleNumView ()
@property (nonatomic) CGFloat radius;
@property (nonatomic, strong) NSString *text;
@end
@implementation CircleNumView
// designated initializer
- (instancetype)initWithRadius:(CGFloat)radius
center:(CGPoint)center
text:(NSString *)text
{
self = [super init];
self.radius = radius;
self.text = text;
self.frame = CGRectMake ( center.x - radius, center.y - radius, 2 * radius, 2 * radius);
self.circleColor = [UIColor whiteColor];
self = [self createView];
return self;
}
- (CircleNumView *)createView
{
CircleNumView *circularView = [[UIView alloc] initWithFrame:self.frame];
circularView.backgroundColor = self.circleColor;
UILabel *label = [[UILabel alloc] initWithFrame:circularView.bounds];
label.text = self.text;
label.textColor = [UIColor blackColor];
[circularView addSubview:label];
circularView.clipsToBounds = YES;
circularView.layer.cornerRadius = self.radius;
[self addSubview:circularView];
return circularView;
}
@end
You were doing everything pretty well until that self = [createView];
在
self = [createView];
之前,您所做的一切都很好self = [createView];
This is the implementation file that I would write: 这是我要编写的实现文件:
//
// CircleNumberView.m
//
//
// Created by James Valaitis on 13/04/2014.
//
//
#import "CircleNumberView.h"
#pragma mark - Circle Number View Private Class Extension
@interface CircleNumberView ()
/** The radius of this circular view. */
@property (nonatomic, assign) CGFloat radius;
/** The number to present encapsulated as a string. */
@property (nonatomic, copy) NSString *text;
/** The label that shows the number contents of this view. */
@property (nonatomic, strong) UILabel *textLabel;
@end
#pragma mark - Circle Number View Implementation
@implementation CircleNumberView
#pragma mark - Initialisation
/**
* Initialises a circlular view with a number in the middle.
*
* @param radius The radius of the circle.
* @param center The center point of the circle in it's superview.
* @param text The number as a string to present in the middle of this view.
*
* @return An initialized object.
*/
- (instancetype)initWithRadius:(CGFloat)radius center:(CGFloat)center text:(NSString *)text
{
CGRect frame = CGRectMake(center.x - radius, center.y - radius, radius * 2, radius * 2);
if (self = [super initWithFrame:frame])
{
_radius = radius;
_text = text;
[self configureViewAndSubviews];
}
return self;
}
#pragma mark - Property Accessor Methods - Getters
/**
* The label that shows the number contents of this view.
*
* @return The label that shows the number contents of this view.
*/
- (UILabel *)textLabel
{
if (!_textLabel)
{
_textLabel = [[UILabel alloc] initWithFrame:self.bounds];
_textLabel.numberOfLines = 0;
_textLabel.textColor = [UIColor blackColor];
}
return _textLabel;
}
#pragma mark - Subview Management
/**
* Configures this view as well as it's subviews.
*/
- (void)configureViewAndSubviews
{
self.backgroundColor = [UIColor whiteColor];
self.clipsToBounds = YES;
self.layer.cornerRadius = self.radius;
self.textLabel.text = self.text;
[self addSubview:self.textLabel];
}
@end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.