【问题标题】:Customize the UIButton resize according to text length and place UILabel after that根据文本长度自定义 UIButton 调整大小,然后放置 UILabel
【发布时间】:2015-06-11 07:26:33
【问题描述】:

我使用了以下代码并得到如下错误,即小文本中的空间更少,长文本中的空间更多

-(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?

IOS 7 sizeWithFont Deprecated

【问题讨论】:

  • @nishalhada 你在说Pet/Pet AccessoriesCanned & Jarred Food / Canned Fruit & Vegetables吗?
  • 是的,你是对的
  • 你需要指定按钮字体的大小[BtnBreadcrumb.titleLabel setFont: [BtnBreadcrumb.titleLabel.font fontWithSize:17]];

标签: ios objective-c uibutton sizewithfont


【解决方案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)];

【讨论】:

  • 我对你的努力投了赞成票,希望我的问题也能得到同样的结果
【解决方案2】:

试试这个

    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];

【讨论】:

  • 你能再检查一遍问题吗
  • 我对你的努力投了赞成票,希望我的问题也能得到同样的结果
【解决方案3】:

请尝试以下代码我添加 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;
}

【讨论】:

  • 添加 [BtnBreadcrumb.titleLabel setFont: [BtnBreadcrumb.titleLabel.font fontWithSize:17]];
  • 我对你的努力投了赞成票,希望我的问题也能得到同样的结果
猜你喜欢
  • 2014-05-04
  • 2021-12-11
  • 2011-05-07
  • 1970-01-01
  • 1970-01-01
  • 2019-09-18
  • 2011-01-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多