【问题标题】:UITextView throws EXC_BAD_ACCESSUITextView 抛出 EXC_BAD_ACCESS
【发布时间】:2014-10-29 03:20:17
【问题描述】:

我创建了一个UITextView 实例,我正在尝试设置它的字体:

UITextView *dummy = [[UITextView alloc] initWithFrame:self.frame];
dummy.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:14];//self.font;

但是,在第二行,我收到EXC_BAD_ACCESS 错误,点击继续执行没有任何作用。它仍然永远挂在同一行,控制台上没有错误消息。它也曾经抛出self.font(我正在使用UILabel 中的代码,它的font 是一个有效的字体对象)。为什么会出现此错误?

更新:我也试过setText: 它也抛出了同样的问题。我错过了什么明显的东西吗?

【问题讨论】:

  • 你试过了吗:[dummy setFont:size:];
  • @Lukasz'Severiaan'Grela UITextView 没有 setFont:size: 方法。
  • 是真码吗? (如果将分配的 UITextField 设置为弱指针,系统会释放它)
  • @AndrewRomanov 是的,从我的项目中复制/粘贴。
  • NSZombies 说:“我想吃掉你的大脑”:)

标签: ios ios7 uitextview


【解决方案1】:

试试这个代码。

UITextView *textview =[[UITextView alloc]init];
textview.frame=CGRectMake(200, 10, 500, 50);

//To make the border look very close to a UITextField
[textview.layer setBorderColor:[[[UIColor grayColor] colorWithAlphaComponent:0.5] CGColor]];
[textview.layer setBorderWidth:2.0];
textview.delegate=self;

//The rounded corner part, where you specify your view's corner radius:
textview.layer.cornerRadius = 5;
textview.clipsToBounds = YES;
[textview setFont:[UIFont fontWithName:@"Helvetica Neue" size:11]];
[textview setTextColor:[UIColor blueColor]];
[textview setReturnKeyType:UIReturnKeyDone];

【讨论】:

    【解决方案2】:

    我找到了解决办法。

    我尝试过 [[UITextView alloc] initWithFrame:self.frame textContainer:nil]; 而不是 [[UITextView alloc] initWithFrame:self.frame]; 并且成功了。我不明白为什么另一种方法不起作用,即使我使用nil 作为textContainer,这种方法仍然有效。

    【讨论】:

    • 您是否尝试在第一个崩溃的代码中清理项目然后再次安装?有时它对我有用。
    • @DavidBemerguy 我没有。但是现在已经解决了。不管怎样,谢谢你。这可能是指定初始化程序的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多