在他的iOS编程书的第二章中,Joe Conway描述了在子类化的情况下在类方法中使用"self".我理解这个概念并且对子类化问题有疑问.
背景:我们创建了一个Possession类,其类方法+ randomPossession如下所示:
+(id)randomPossession
{
NSArray *randomAdjectiveList = [NSArray arrayWithObjects:@"Fluffy", @"Rusty", @"Shiny", nil];
NSArray *randomNounList = [NSArray arrayWithObjects:@"Bear", @"Spork", @"Mac", nil];
unsigned long adjectiveIndex = rand() % [randomAdjectiveList count];
unsigned long nounIndex = rand() % [randomNounList count];
NSString *randomName = [NSString stringWithFormat:@"%@ %@", [randomAdjectiveList objectAtIndex:adjectiveIndex], [randomNounList objectAtIndex:nounIndex]];
int randomValue = rand() % 100;
NSString *randomSerialNumber = [NSString stringWithFormat:@"%c%c%c%c%c",
'0' + rand() % 10,
'A' + rand() % 10,
'0' + rand() % 10,
'A' + rand() % 10,
'0' …
Run Code Online (Sandbox Code Playgroud) 我目前正在编写针对iOS 6.1 SDK的应用程序.我知道iOS 7中的某些内容可能无法解决我的问题,但为了学习,我还是会问.
该应用程序将包含一个表视图和自定义表视图单元格.我希望单元格contentView的唯一子视图是使用Core Text绘制的NSAttributedString的自定义视图.由于每个单元格的字符串将不同,因此字形定位需要依赖于字形的数量(即较长的字符串将在字形之间具有较少的可见空间).字体和物理边界的大小必须保持不变,只有字形定位才会不同.
我有以下代码,无论出于何种原因不能达到我的预期.
这是BMPTeamNameView的.h - 自定义视图(contentView的子视图)
@interface BMPTeamNameView : UIView
-(id)initWithFrame:(CGRect)frame text:(NSString *)text textInset:(UIEdgeInsets)insets font:(UIFont *)font;
@property (nonatomic, copy) NSAttributedString *attributedString;
@property (nonatomic, copy) NSString *text;
@property (nonatomic, strong) UIFont *font;
@property (nonatomic, assign) UIEdgeInsets insets;
@end
Run Code Online (Sandbox Code Playgroud)
新的指定初始值设定项现在将设置框架,用于属性字符串的文本,用于确定关于contentView rect的文本rect的插入内容以及要使用的字体.
最初在我的自定义drawRect中:我使用了CTFramesetterRef,但CTFramesetterRef将创建一个不可变的框架(可能有?)限制了各个字形的布局.在这个实现中,我使用CTTypesetterRef来创建CTLineRef.当您比较CTLineDraw()和CTFrameDraw()时,使用CTFrame会导致不同的绘制行为,但这是另一个问题.我的drawRect:如下:
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
// Flips the coordinates so that drawing will be right side up
CGContextSetTextMatrix(context, CGAffineTransformIdentity);
CGContextTranslateCTM(context, 0, self.bounds.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
// Path that will hold the textRect
CGMutablePathRef path …
Run Code Online (Sandbox Code Playgroud) objective-c quartz-graphics nsattributedstring core-text ios