2012-01-06 6 views
0

UILabelUILineBreakModeWordWrapを使用して長い文字列を表示しています。 UILabelにテキストをラップすることで、文字列を完全に表示しています。私は最後の行UILabelにアクセスしたいです。誰もがiPhoneでこれを行う方法を知っていますか?iPhoneでUILineBreakModeWordWrap UILabelを使用中に最後の行にアクセスする

+0

この長い文字列は静的な文字列か動的か。もしそれが動的であれば、 'NSString * lastLine = [longString substringFromIndex:[longString length] -35/*最後の行の長さが35 * /]と言うことができます。'と 'lasLine'をUITextFieldとして表示できます編集可能です – pmk

+0

長い文字列は動的で、最後の行の長さはわかりません。 –

答えて

0

だから私はいくつかのものを試して、少し周りを検索しました。あなたが実際に必要とするのは、単語の折り返しを数え、何とか最後の文字列を検出することです。しかし、私は本当にそれを行う方法をfiguereしませんでした。

だから私のsollutionはこのようなものです:

あなたの文字列//I googled some longer String

NSString *string = @"Standing across the room, I saw you smile\nSaid I want to talk to you-oo-oo for a little while\nBut before I make my move my emotions start running wild\nMy tongue gets tied and that's no lie\nLooking in your eyes\nLooking in you big brown eyes ooh yeah\nAnd I've got this to say to you\nHey!\nGirl I want to make you sweat\nSweat till you can't sweat no more\nAnd if you cry out\nI'm gonna push it some, more, more\nGirl I want to make you sweat\nSweat till you can't sweat no more\nAnd if you cry out\nI'm gonna push it\nPush it, push it some more"; 

あなたレーベル:

UILabel *label   = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 280, 440)]; 
label.font    = [UIFont fontWithName:@"Helvetica" size:14]; 
label.lineBreakMode  = UILineBreakModeWordWrap; 
label.numberOfLines  = 0; 
label.backgroundColor = [UIColor clearColor]; 
label.textColor   = [UIColor blackColor]; 
label.text    = string; 

コールこの方法:

NSString *result = [self getLastLineFromString:string]; 
NSLog(@"Result: %@", result); 

getLastLineFromString:は次のようになります。

- (NSString *)getLastLineFromString: (NSString *)string{ 
    NSArray *a = [string componentsSeparatedByString:@" "]; 
    NSString *result = [a objectAtIndex:[a count]-1]; 
    NSString *temp = @""; 
    int count = 1; 
    BOOL myBool = YES; 
    while (myBool) { 
     count++; 

     temp = result; 
     result = [a objectAtIndex:[a count] -count]; 
     result = [NSString stringWithFormat:@"%@ %@", result, temp]; 
     NSLog(@"length: %i",[self lengthOfString:result]); 
     NSLog(@"result: %@",result); 

    //131 was a value i detected mayels, i guess u have do trick a little around to find a mathcing one for yourself 
    if ([self lengthOfString:result] >= 131) { 
     myBool = NO; 
    } 
    } 
    return result; 
} 

及び方法lengthOfString:は次のようになります。

- (int)lengthOfString:(NSString *)string{ 
    CGSize size1 = [string sizeWithFont:[UIFont fontWithName:@"Helvetica" size:14]]; 
    return size1.width; 
} 

出力:

2012-01-06 16:17:08.341 get length[5472:207] result: it 
Push it, push it some more 

を、私は、これは完璧なsollutionないですけど、それはあなたを助けるかもしれません。

関連する問題