【发布时间】:2014-07-05 14:19:45
【问题描述】:
这是我编写的一个小视图控件,用于测试我在约束和adjustsFontSizeToFitWidth 方面遇到的问题。从标签中添加和删除单词以查看 label_title 中的各种内容大小如何影响布局是一件小事。所需的布局紧贴在 label_title 周围(label_count 占据了房间的其余部分)。不幸的是,它还不是很舒服。
以下是我截取的 3 个视图状态,以说明我正在尝试修复的蓝色标题缺乏舒适度。
以下是运行该视图的视图控制器的完整源代码。
#import "MainViewController.h"
@interface MainViewController ()
// stuff to test our problem with
@property(nonatomic,strong) UIButton * addButton;
@property(nonatomic,strong) UIButton * removeButton;
// our problem
@property(nonatomic,strong) UIView * view_labels;
@property(nonatomic,strong) UILabel * label_title;
@property(nonatomic,strong) UILabel * label_count;
@end
@implementation MainViewController
// synthesize for the views constraint mapping
@synthesize label_title;
@synthesize label_count;
- (void)viewDidLoad
{
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor grayColor]];
self.addButton = [self buttonWithTitle:@"Add Word"
selector:@selector(addButtonPressed) color:[UIColor greenColor]];
self.removeButton = [self buttonWithTitle:@"Remove Word"
selector:@selector(removeButtonPressed) color:[UIColor redColor]];
// view containing our troublesome labels
self.view_labels = [[UIView alloc] init];
[self.view addSubview:self.view_labels];
//[self.view_labels setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view_labels setBackgroundColor:[UIColor lightGrayColor]];
// label row 1
self.label_title = [self labelWithText:@"word" defaultFontSize:30.0 bgcolor:[UIColor blueColor]];
[self.view_labels addSubview:self.label_title];
// label row 2
self.label_count = [self labelWithText:@"77" defaultFontSize:40.0 bgcolor:[UIColor purpleColor]];
[self.view_labels addSubview:self.label_count];
NSDictionary *views = NSDictionaryOfVariableBindings(label_title, label_count);
NSDictionary *metrics = @{@"pad":@2};
[self.view addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:@"V:|-(==pad)-[label_title(<=44)]-(==pad)-[label_count]-(==pad)-|"
options:0
metrics:metrics
views:views]];
[self.view addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:@"|-(==pad)-[label_title]-(==pad)-|"
options:0
metrics:metrics
views:views]];
[self.view addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:@"|-(==pad)-[label_count]-(==pad)-|"
options:0
metrics:metrics
views:views]];
}
#pragma mark - boiler plate stuff
-(UILabel*) labelWithText:(NSString*) text defaultFontSize:(float) fontSize bgcolor:(UIColor*) color
{
UILabel * label = [[UILabel alloc] init];
[label setTranslatesAutoresizingMaskIntoConstraints:NO];
[label setTextAlignment:NSTextAlignmentCenter];
[label setTextColor:[UIColor whiteColor]];
[label setAdjustsFontSizeToFitWidth:YES];
[label setNumberOfLines:0];
[label setMinimumScaleFactor:0.6];
[label setFont:[UIFont boldSystemFontOfSize:fontSize]];
[label setBackgroundColor:color];
[label setText:text];
return label;
}
-(UIButton*) buttonWithTitle:(NSString*) title selector:(SEL) selector color:(UIColor*) color
{
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
[self.view addSubview:button];
[button setTitle:title forState:UIControlStateNormal];
[button setBackgroundColor:color];
[button addTarget:self action:selector forControlEvents:UIControlEventTouchUpInside];
return button;
}
-(void) viewWillLayoutSubviews{
[super viewWillLayoutSubviews];
float SQUARE_SIZE = 100;
[self.addButton setFrame:CGRectMake(0, self.view.frame.size.height-44.0, self.view.frame.size.width, 44.0)];
[self.removeButton setFrame:CGRectMake(0, self.view.frame.size.height-(44.0*2), self.view.frame.size.width, 44.0)];
[self.view_labels setFrame:CGRectMake(SQUARE_SIZE, SQUARE_SIZE, SQUARE_SIZE, SQUARE_SIZE)];
}
-(void) addButtonPressed
{
NSString * text = self.label_title.text;
if (text == nil){
text = @"";
}
text = [NSString stringWithFormat:@"%@%@",text,@" word"];
text = [text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
[self.label_title setText:text];
}
-(void) removeButtonPressed
{
NSString * text = self.label_title.text;
if (text == nil || [text isEqualToString:@""]){
return;
}
NSArray * array = [text componentsSeparatedByString:@" "];
text = @"";
for (int i = 0; i < [array count]-1; i++){
NSString * token = [array objectAtIndex:i];
text = [NSString stringWithFormat:@"%@ %@",text,token];
}
text = [text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
[self.label_title setText:text];
}
@end
【问题讨论】:
-
你能详细解释一下吗?我没有从上述问题/陈述中得到您的问题。
-
我同意@AnilKumar 的观点 - 与其喋喋不休,不如在某个地方解释一下你真正想要做什么?例如,您是否只是想制作一个调整自身大小以适合其文本的标签?如果是这样,这是一个众所周知且很好解决的问题。
-
@AnilKumar 我已经完全改写了我的问题以及我遇到的问题以及视图控制器的完整源代码。任何帮助将不胜感激。
-
@matt 请查看我更新的问题 - 抱歉之前的模棱两可。
标签: ios objective-c uilabel autolayout