这是我的代码
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 50, 300, 50)];
label.textAlignment = UITextAlignmentCenter;
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor whiteColor];
label.textColor.font = [UIFont fontWithName:@"Verdana" size:30];
label.text = @"A very long string";
etc...
Run Code Online (Sandbox Code Playgroud)
问题是字体很大,无法放入标签中.它只显示"非常"
怎么做才能显示整个文本.我试过了
label.lineBreakMode = UILineBreakModeWordWrap;
label.numberOfLines = 0;
Run Code Online (Sandbox Code Playgroud)
但它对我不起作用.我想以编程方式做到这一点.
//编辑
CGRect frame = CGRectMake(10, 50, 300, 50);
NSString *labelString = @"Players.";
UILabel *howManyUsersLabel = [[UILabel alloc]initWithFrame:frame];
howManyUsersLabel.textAlignment = UITextAlignmentCenter;
howManyUsersLabel.backgroundColor = [UIColor clearColor];
howManyUsersLabel.textColor = [UIColor whiteColor];
howManyUsersLabel.adjustsFontSizeToFitWidth = NO;
howManyUsersLabel.numberOfLines = 0;
CGFloat fontSize = 30;
while …
Run Code Online (Sandbox Code Playgroud) 我正在学习ObjC和iOS开发.我在应用程序中使用的每个组件都是以编程方式创建的(视图,按钮,标签等).
这是我的代码
...
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[self.btn addTarget:self
action:@selector(someAction:)
forControlEvents:UIControlEventTouchDown];
....
-(void)someAction
{
logic
}
Run Code Online (Sandbox Code Playgroud)
我注意到我可以使用void而不是IBAction作为返回类型的选择器.这是正确的方法吗?可能有任何陷阱吗?
将变量初始化为nil
?是一种好习惯吗?
我问的是因为当我在我的项目上运行分析器时,我收到了警告.
NSString *q;
if(sqlite3_prepare_v2(database, [query UTF8String], -1, &statement, nil) == SQLITE_OK)
{
sqlite3_step(statement);
selectedQuestion =[NSString stringWithFormat: @"%s",(char *)sqlite3_column_text(statement, 0)];
sqlite3_finalize(statement);
}
sqlite3_close(database);
return q; //Undefined or garbage value returned to caller
Run Code Online (Sandbox Code Playgroud)
当我更改代码时,警告消失了:
NSString *q = nil;
Run Code Online (Sandbox Code Playgroud) 我知道viewWillAppear没有在pop/push视图上调用,但我真的需要这个方法.这是我尝试的
我添加了UINavigationControllerDelegate并采用了
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
[viewController viewWillAppear:animated];
}
-(void)viewWillAppear
{
NSLog(@"Log");
}
Run Code Online (Sandbox Code Playgroud)
但仍未调用viewWillAppear
编辑
AppDelegate.m
self.navigationController = [[UINavigationController alloc]init];
[self.window setRootViewController:self.navigationController];
FirstView *fview = [FirstView]alloc]init];
[self.viewController pushViewController:fview animated:YES];
FirstView.m
....
-(void)viewWillAppear
{
NSLog(@"Logged");
}
Run Code Online (Sandbox Code Playgroud)
....