gpt4 book ai didi

ios - 无法实现 UIView 子类

转载 作者:行者123 更新时间:2023-11-29 03:03:27 25 4
gpt4 key购买 nike

我正在尝试创建一个 UIView,它基本上是一个带有圆形背景的数字。我正在使用 UIView 并为圆应用一半尺寸的角半径。然后我将数字作为 UILabel subview 添加到上面的 View 中。

我想要的是接近这个(没有花哨的边框):(这是应用程序 Count Battle 的屏幕截图)。 http://a3.mzstatic.com/us/r30/Purple/v4/ca/ea/a4/caeaa4a0-024d-e810-addb-17a12ea18000/screen568x568.jpeg

请通过将代码移动到正确的方法(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

最佳答案

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

关于ios - 无法实现 UIView 子类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23040404/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com