Ios NSMutableAttributedString子类即使未经修改也无法识别选择器?
我正在尝试子类化Ios NSMutableAttributedString子类即使未经修改也无法识别选择器?,ios,objective-c,nsattributedstring,foundation,Ios,Objective C,Nsattributedstring,Foundation,我正在尝试子类化NSMutableAttributedString,但我发现我无法以任何形式使用它。即使是新创建的子类,也无法初始化: 我收到的例外情况如下: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ALCTMutableAttributedString initWithString:]: unrecognized selector sent to instanc
NSMutableAttributedString
,但我发现我无法以任何形式使用它。即使是新创建的子类,也无法初始化:
我收到的例外情况如下:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ALCTMutableAttributedString initWithString:]: unrecognized selector sent to instance 0x16d21680'
它甚至不适用于简单的初始化调用:
ALCTMutableAttributedString *newString = [[ALCTMutableAttributedString alloc]init];
下面是子类的基本内容:
头文件:
#import <Foundation/Foundation.h>
@interface ALCTMutableAttributedString : NSMutableAttributedString
@end
#import "ALCTMutableAttributedString.h"
@implementation ALCTMutableAttributedString
@end
我很困惑为什么这是不允许的,或者在我的项目中是不可能的。有人知道为什么吗
扩展名:
#import <Foundation/Foundation.h>
@interface ALCTMutableAttributedString : NSMutableAttributedString
@end
#import "ALCTMutableAttributedString.h"
@implementation ALCTMutableAttributedString
@end
为了演示如何使用该类,下面是我使用该类的类的标题:
#import <UIKit/UIKit.h>
#import "Foundation/Foundation.h"
#import "CXCommentLabel.h"
#import "ALCTMutableAttributedString.h"
@interface CXPodcastViewController : UIViewController
@property (nonatomic,weak)IBOutlet CXCommentLabel *commentLabel;
@property (nonatomic,weak)IBOutlet NSLayoutConstraint *commentLabelHeightConstraint;
@property (nonatomic,weak)IBOutlet UIButton *resizeButton;
-(IBAction)resizeTheLabel:(id)sender;
@end
在苹果的文档中,有一个部分叫做“子类化的替代方法”,请也检查一下
不可能使用NSMutableAttributedString对字符串进行子类化
使用NSMutableAttribute字符串的另一种方法是
-(void)resizeTheLabel:(id)sender {
NSMutableAttributedString* attrStr = [[NSMutableAttributedString alloc] initWithString:@"string" attributes:@{NSForegroundColorAttributeName:[UIColor blackColor]}];
[self.commentLabel setAttributedText:newString];
}
将所需的属性添加到字典中,并将其作为参数传递。
快乐编码:)初始化代码在哪里???@satheesh你是什么意思?它是
nsmutableAttributeString
的一个子类。它是通过它的超类初始化的。我想我已经发布了预期的解决方案,如果不允许的话know@satheesh不,没用。我不是傻瓜,我知道我需要导入头文件。它仍然不起作用。@satheesh很高兴找到,把它作为你的答案贴出来,我会在期限到期时选择它作为正确答案。我之前看到过类似的东西,但我自己也不确定。这不是很有用的答案。那么你有什么建议呢?@trojanfoe:1)提问者让他发布答案。2) 提问者没有解释他为什么想要子类化,所以提供替代方案是不可能的,即使提问者想要任何替代方案。@trojanfoe在苹果的文档中有一个叫做“子类化的替代方案”的部分,请检查一下,或者将其添加到你的答案中。它可能会在将来帮助其他人。@Avi好的,我将添加它:)