gpt4 book ai didi

ios - 根据文本长度自定义 UIButton 调整大小,然后放置 UILabel

转载 作者:行者123 更新时间:2023-11-28 19:48:03 26 4
gpt4 key购买 nike

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

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

最佳答案

请使用:

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

关于ios - 根据文本长度自定义 UIButton 调整大小,然后放置 UILabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30774286/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com