在iphone sdk中将两个不同的文本对齐到一行

在iphone sdk中将两个不同的文本对齐到一行,iphone,ios,xcode,uibutton,uilabel,Iphone,Ios,Xcode,Uibutton,Uilabel,我有一个ui标签和一个ui按钮以及除了ui标签之外正在动态变化的文本,我想将它们显示为一个标签,并且应该在一行上居中对齐,但两者的工作方式不同,因为我在图片中显示了下划线文本“无麸质食品”具有可单击功能,与找到的文本不同。我不知道如何完成这项任务,如果图片“无麸质的美食家”中按钮的文本将变得很长,那么我如何才能将标签移到左边,如图所示。提前谢谢。 您可以尝试使用TTTAttributedLabel()或其他可支持链接的自定义标签,并且您可以使用无麸质食品的链接属性将其带下划线。您可以尝试使用TT

我有一个
ui标签
和一个
ui按钮
以及除了
ui标签
之外正在动态变化的文本,我想将它们显示为一个标签,并且应该在一行上居中对齐,但两者的工作方式不同,因为我在图片中显示了下划线文本“无麸质食品”具有可单击功能,与找到的文本不同。我不知道如何完成这项任务,如果图片“无麸质的美食家”中按钮的文本将变得很长,那么我如何才能将标签移到左边,如图所示。提前谢谢。

您可以尝试使用TTTAttributedLabel()或其他可支持链接的自定义标签,并且您可以使用无麸质食品的链接属性将其带下划线。

您可以尝试使用TTTAttributedLabel()或其他可支持链接的自定义标签,您可以使用无麸质食品的链接属性将其带下划线。

您可以使用两个标签。第一个的宽度将是固定的。使用以下方法计算秒的宽度

[[label text] sizeWithFont:label.font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];

浏览apple文档,了解其工作原理。一旦你得到了宽度,你就可以为两者都提供框架。160两个标签的宽度之和/2第一个标签的宽度为x。第一个标签的x+宽度为第二个标签的x。

您可以使用两个标签。第一个的宽度将是固定的。使用以下方法计算秒的宽度

[[label text] sizeWithFont:label.font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
浏览apple文档,了解其工作原理。一旦你得到了宽度,你就可以为两者都提供框架。160两个标签的宽度之和/2第一个标签的宽度为x。第一个标签的x+宽度表示第二个标签为x。

尝试以下方法:

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);
  • 根据标签的文本查找标签的宽度
  • 根据按钮的标题查找按钮的宽度
  • 获取标签、按钮的总宽度以及它们之间的间距
  • 从uiview(按钮和标签的超级视图)宽度(假设为320)中减去总宽度
  • 最后你得到了额外的空间。之后,这个额外的空间被2除
  • 最后,根据该设置标签和按钮的框架(x位置很重要)
  • 试试这个:

    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);
    
  • 根据标签的文本查找标签的宽度
  • 根据按钮的标题查找按钮的宽度
  • 获取标签、按钮的总宽度以及它们之间的间距
  • 从uiview(按钮和标签的超级视图)宽度(假设为320)中减去总宽度
  • 最后你得到了额外的空间。之后,这个额外的空间被2除
  • 最后,根据该设置标签和按钮的框架(x位置很重要)

  • 如果此应用程序适用于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];
    
    在这张图中可以看到

    如果此应用程序适用于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];
    
    在这张图中可以看到

    您需要为此保留单独的UILabel。我正在按照您的建议进行操作,这也是我的要求,但为此,我无法在一条线上同时将两者居中对齐。请使用。正是您需要的,您需要为此保留单独的UILables。我正在按照您的建议执行相同的操作,这也是我的要求,但为此,我无法在一条线上同时将两者居中对齐。请使用。正是你所需要的伟大,良好的描述和找到的解决方案的帮助下,你的职位。谢谢你的帖子。很好,很好的描述,并在你的帖子的帮助下找到了解决方案。谢谢。