我正在尝试创建一个UIView,它基本上是一个带有圆形背景的数字。我正在使用UIView并应用圆的一半尺寸的圆角。然后我将这个数字作为UILabel子视图添加到上面的视图中。
我想要的是接近这个(没有花哨的边框): (这是应用Count Battle的截图)。
请通过在正确的方法(drawrect,init,layoutSubviews或任何自定义方法)下移动代码来帮助我重写。这是当前形式的代码。我认为我对这件事的理解是混乱的,这看起来不对。
这是头文件:
// 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
这是实施文件:
// 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
答案 0 :(得分:1)
在self = [createView];
这是我要写的实现文件:
//
// 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