我用下面的代碼,並得到如下錯誤的小文和更多的空間,長文本即更少的空間,根據文本長度和地方的UILabel調整自定義的UIButton後
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 40)];
UIButton *BtnBreadcrumb = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[BtnBreadcrumb addTarget:self action:@selector(selectBtnBreadcrumb:)
forControlEvents:UIControlEventTouchUpInside];
[BtnBreadcrumb setTitle:selectedDepartment forState:UIControlStateNormal];
BtnBreadcrumb.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
BtnBreadcrumb.tintColor=ThemeColor;
CGSize stringsize = [selectedDepartment sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:17.0f]}];
BtnBreadcrumb.frame = CGRectMake(10, 5, stringsize.width, 40);
[view addSubview:BtnBreadcrumb];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(stringsize.width, 5, 200, 40)];
//[label setFont:[UIFont boldSystemFontOfSize:12]];
NSString *string = [NSString stringWithFormat:@"/ %@",selectedCategory];
label.font = [UIFont fontWithName:@"Helvetica" size:15.0];
label.textColor = TextColor;
[label setText:string];
[view addSubview:label];
[view setBackgroundColor:[UIColor colorWithRed:0.933f green:0.933f blue:0.933f alpha:1.00f]];
return view;
}
我有學習以下鏈接
iOS: UIButton resize according to text length
How can i increase the button width dynamically depends on the text size in iphone? Replacement for deprecated -sizeWithFont:constrainedToSize:lineBreakMode: in iOS 7? Replacement for deprecated sizeWithFont: in iOS 7?
@nishalhada你在說「寵物/寵物配件」和「罐裝和罐裝食品/罐裝水果和蔬菜」嗎? –
是的兄弟你是對的 –
你需要分配的按鈕字體的大小** [BtnBreadcrumb.titleLabel setFont:[BtnBreadcrumb.titleLabel.font fontWithSize:17]]; ** –