iOS Developer
iOS Developer

Reputation: 27

line break at starting of UILabel text

I have string to display in UILabel, but the UILabel isn't displaying the text of the below string.

string is : " \n \n If you are using a corticosteroid medication"

I know \n at the start isn't logical but I am getting it from server and hence I can not change it. So is there any way to resolve this issue.

Any way that label displays rest text by adding two line break at start or how can I truncate \n if it is in the beginning of string.

Thanks.

Upvotes: 0

Views: 99

Answers (3)

Popeye
Popeye

Reputation: 12093

If you are wanting to remove \n from your string you could just use something as simple as

NSString *myString = @" \n \n If you are using a corticosteroid medication";

myString  = [[myString stringByReplacingOccurrencesOfString:@"\n"
                                                         withString:@""];

stringByReplacingOccurencesOFString: withString: will replace all occurrences of the given string (In your case \n) with the given string.

The above should technically do the exact same as [myString stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];

If you want to replace only within a specific range then you could use stringByReplacingOccurrencesOfString:withString:options:range: instead like:

myString = [myString replaceOccurencesOfString:@"\n" 
                           withString:@"" 
                              options:NULL
                                range:NSMakeRange(0, 7)];

This will remove all occurrences of \n within the given range, for this example from 0 - 7 Check out he Apple documentation for this here

Upvotes: 1

Akhilrajtr
Akhilrajtr

Reputation: 5182

Try this

NSString *text = @" \n \n If you are using a corticosteroid medication";
text = [text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

Upvotes: 0

M_G
M_G

Reputation: 1208

Did you try this?

[yourString stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];

Upvotes: 2

Related Questions