2015-06-11 25 views
0

我用下面的代碼,並得到如下錯誤的小文和更多的空間,長文本即更少的空間,根據文本長度和地方的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; 
} 

enter image description here enter image description here

我有學習以下鏈接

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?

IOS 7 sizeWithFont Deprecated

+0

@nishalhada你在說「寵物/寵物配件」和「罐裝和罐裝食品/罐裝水果和蔬菜」嗎? –

+0

是的兄弟你是對的 –

+0

你需要分配的按鈕字體的大小** [BtnBreadcrumb.titleLabel setFont:[BtnBreadcrumb.titleLabel.font fontWithSize:17]]; ** –

回答

1

請使用:

CGSize size1 = [strkeyword1Partner1 sizeWithAttributes:@{NSFontAttributeName:[UIFont fontWithName:FONT_MYriad_REGULAR size:14.0f]}]; 

// Values are fractional -- you should take the ceilf to get equivalent values 

CGSize adjustedSize1= CGSizeMake(ceilf(size1.width), ceilf(size1.height)); 

[btnKewword1 setFrame:CGRectMake(self.view.frame.size.width-(adjustedSize1.width +14), btnKewword.frame.origin.y, adjustedSize1.width+5, 20)]; 
+0

使用ceilf does not work out too –

+0

我只是使用與最新的項目和它的作品。 –

+0

好吧,我會嘗試你的代碼,並會通知你,然後 –

1

試試這個

UIButton* btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    btn.frame = CGRectMake(50, 100, 100, 0); 
    btn.titleLabel.numberOfLines = 0; 
    [btn setTitle:@"dsgdfgdfsdsfgdfsgdfsgdfsgdfgdfgfkdsfgdsgdksfgkdskgf" forState:UIControlStateNormal]; 
    [self.view addSubview:btn]; 
+0

你可以再次檢查問題請 –

+0

我有投票爲你的努力希望得到相同的我的問題太 –

1

請嘗試下面的代碼我添加的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.titleLabel setFont: [BtnBreadcrumb.titleLabel.font fontWithSize:17]]; 

    [BtnBreadcrumb setTitle:@"Test" 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; 
} 
+0

代碼betn你的和我的 –

+0

有什麼區別加入** [BtnBreadcrumb.titleLabel setFont:[BtnBreadcrumb.titleLabel.font fontWithSize:17]]; ** –

+0

我有投票爲你的努力希望得到相同的在我的問題上 –