开发者

How to break the one line of text and display it in 2 different labels in iphone

开发者 https://www.devze.com 2023-03-12 13:52 出处:网络
How to break the one line of text and display it in 2 different labels. For example i have a text says \"Have a happy week end\". I wanted to print \"have a happy\" in one label and \"week end\" in th

How to break the one line of text and display it in 2 different labels. For example i have a text says "Have a happy week end". I wanted to print "have a happy" in one label and "week end" in the an开发者_高级运维other label.


I'm not sure what you're trying to achieve. You can split your sentence in words like this:

NSArray *words = [string componentsSeparatedByString:@" "];
for(NSString *item in words)
{ 
.. 
}

or you can split your sentence with \n and display it in one label that supports multiple rows

lblMsg.lineBreakMode = UILineBreakModeWordWrap;
lblMsg.numberOfLines = 0;


Your description is a bit vague, but here is some code that splits a string (using spaces) into two equal parts if there is at least one space (the first part always being bigger if they cannot be equal). If there is no space, firstHalf will contain the whole string and secondHalf will be nil.

NSUInteger middle = string.length / 2;
NSUInteger length = string.length;
unichar theChar;
do {
    theChar = [string characterAtIndex:middle];
    middle++;
} while (middle < length && theChar != ' ');

NSString *firstHalf = nil;
NSString *secondHalf = nil;
if (theChar == ' ') {
    firstHalf = [string substringToIndex:middle];
    secondHalf = [string substringFromIndex:middle];
} else {
    firstHalf = string;
}


Hmm, not sure I exactly understand the question but if you want to display a string over two lines you could do this using one label showing two lines using "\n". Put "\n" at the end of happy (without quotes) so "have a happy \n week end".

0

精彩评论

暂无评论...
验证码 换一张
取 消