josh
josh

Reputation: 1689

Align two different text on single line in iphone sdk

I have a UILabel and a UIButton and the text which is changing dynamic except UILabel, I want to show them as a single label and should be center align on single line but both work differently as I show in picture the underline text "The Gluten free foodie" has clickable feature unlike text Found by. I don't know how to accomplish this task if text of button like in picture "The Gluten free foodie" will come to much long then how can i move label to left as show in picture. Thanks in advance. enter image description here

Upvotes: 0

Views: 453

Answers (4)

DharaParekh
DharaParekh

Reputation: 1730

Try this:

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. Find width of your label based on text of that label
  2. Find width of your button based on title of that button
  3. get total width of your label, button and space between these two.
  4. Subtract that total width from you uiview (superview of button and label) width (suppose 320).
  5. Finally you get total extra space. After that this extra space is divided by 2.
  6. And last, based on that set the frame (x position is important) of your label and button.

Upvotes: 2

Dharmbir Singh
Dharmbir Singh

Reputation: 17535

If this app for ios6 or later version in this case you can do like this...

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

And see in this image enter image description here

Upvotes: 1

Durgaprasad
Durgaprasad

Reputation: 1951

You can use two lables. width of first will be fix. Calculate width for second using following method.

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

Go though apple document and find how it works. Once you get width you can give frames for both. 160-Sum of width of both labels/2 you get x for first label. This x + width of first label gives x for second label.

Upvotes: 0

danypata
danypata

Reputation: 10175

You can try using TTTAttributedLabel(https://github.com/mattt/TTTAttributedLabel) or other custom labels that can support links and you can use a link attribute for the The Gluten free foodie to show it underlined.

Upvotes: 0

Related Questions