我有一个UILabel
和一个UIButton
以及除了UILabel
之外正在改变动态的文字,我想将它们显示为单个标签,并且应该在单行上居中对齐但是两者的工作方式不同,因为我在图片中显示下划线文本“The Gluten free foodie”具有可与文本Found by
不同的可点击功能。如果图片“The Gluten free foodie”中的按钮文字会长得多,那我怎么能完成这个任务呢?如何将标签移动到左边如图所示。提前致谢。
答案 0 :(得分:2)
试试这个:
NSString *text1 = @"Found by "; //text of your uilabel
CGSize constraint1 = CGSizeMake(320, 2000); //suppose your total width of superview is 320
CGSize size1 = [text1 sizeWithFont:[UIFont fontWithName:@"ArialMT" size:12.0] constrainedToSize:constraint1 lineBreakMode:UILineBreakModeWordWrap]; //font which are used in your "Found by" label
NSString *text2 = @"The Gluten free foodie"; //text of your uibutton
CGSize constraint2 = CGSizeMake(320, 2000);
CGSize size2 = [text2 sizeWithFont:[UIFont fontWithName:@"Arial-BoldMT" size:12.0] constrainedToSize:constraint2 lineBreakMode:UILineBreakModeWordWrap]; //font which are used in your "The Gluten free foodie" button
float finalwidth = size1.width + size2.width + 2;
float centerx = 320 - finalwidth; //suppose your total width of view is 320
centerx = centerx/2.0;
lblFoundBy.frame = CGRectMake(centerx, 20, size1.width, size1.height);
btnThe.frame = CGRectMake(centerx + size1.width + 2, 20, size2.width, size2.height);
答案 1 :(得分:1)
如果这个应用程序适用于ios6或更高版本,在这种情况下你可以这样做......
NSString *infoString=@"Founded by The Gluten free foodie";
NSMutableAttributedString *attString=[[NSMutableAttributedString alloc] initWithString:infoString];
NSInteger _stringLength=[infoString length];
// ------------------ Give your range for underline
[attString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:1] range:NSMakeRange(10, _stringLength-10)];
label.attributedText = attString;
[label setBackgroundColor:[UIColor clearColor]];
// ---------------- Give frame according to your text -------
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(70, 15, 195, 35);
[btn addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];
[label addSubview:btn];
[label setUserInteractionEnabled:YES];
并在此图片中看到
答案 2 :(得分:0)
您可以尝试使用TTTAttributedLabel(https://github.com/mattt/TTTAttributedLabel)或其他支持链接的自定义标签,您可以使用The Gluten free foodie
的链接属性来显示下划线。
答案 3 :(得分:0)
你可以使用两个标签。第一个宽度将被修复。使用以下方法计算第二个宽度。
[[label text] sizeWithFont:label.font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
浏览苹果文档并找到它的工作原理。一旦你获得宽度,你可以为两者提供帧。 160-两个标签宽度的总和/ 2为第一个标签得到x。第一个标签的x +宽度为第二个标签提供x。