5

I want to change brackets's baseline offset in a words, like "[推]blablabla".

NSRange range = [text rangeOfString:@"[推]"];
if (range.location == 0) {
    [text addAttribute:NSBaselineOffsetAttributeName value:@(0.5) range:NSMakeRange(0, 1)];
    [text addAttribute:NSBaselineOffsetAttributeName value:@(0.5) range:NSMakeRange(range.length-1, 1)];
}

but the second line of the label disappears and gets truncated. Anyone have an idea?

Monqi
  • 81
  • 1
  • 6

4 Answers4

4

Please use lineBreakMode and numberOfLines Also to call sizeToFit, like this:

label.lineBreakMode = UILineBreakModeWordWrap;
label.numberOfLines = 0;
[label sizeToFit]; 

The height will be automatically computed.

Thanks

Vijay Kachhadiya
  • 366
  • 2
  • 11
  • I use autolayout, sizeToFit is useless. I already set lineBreakMode = NSLineBreakByTruncatingTail and numberOfLines = 2. – Monqi May 06 '16 at 10:41
1

I've did some quick test, and it seems that if you add a whitespace at the first of your string, it will work as expected

// make sure your text looks like @" [推]your content"
if (range.location == 1) {
    [text addAttribute:NSBaselineOffsetAttributeName value:@(0.5) range:NSMakeRange(1, 1)];
    [text addAttribute:NSBaselineOffsetAttributeName value:@(0.5) range:NSMakeRange(range.length, 1)];
}
xi.lin
  • 3,326
  • 2
  • 31
  • 57
  • I test and it don't work. can you explain your code a little more. – Alen Liang Jul 09 '17 at 10:29
  • @AlenLiang In my experiment, the setting of `NSBaselineOffsetAttributeName` on different characters will effect the truncation. Try different combination of range index will make it correct. Are you sure your text is like `@" [推]your content"`, especially have a space in the first? – xi.lin Jul 10 '17 at 07:15
  • Try different combination of range index will make it correct. sounds quite tricky for me, I don't use @" [推]your content", just add a whitespace at the beginning of my string. Still it looks like bug. – Alen Liang Jul 11 '17 at 15:13
  • @AlenLiang Yeah. I think we can open a radar – xi.lin Jul 12 '17 at 02:03
  • I happen to test this bug, and now it works whatever your string is. in iOS 11. – Alen Liang Mar 21 '18 at 06:37
0

If you using storyboard see the below image, select the Label, then see the Lines is set 0 and Line Breaks set Word Wrap thats solve.

enter image description here

or if you use code,

Objective C:

LabelName.lineBreakMode = UILineBreakModeWordWrap;
LabelName.numberOfLines = 0;

Swift:

LabelName.lineBreakMode = .ByWordWrapping
LabelName.numberOfLines = 0 

hope its helpful

Iyyappan Ravi
  • 3,205
  • 2
  • 16
  • 30
0

Did you try using NSMutableParagraphStyle to set lineBreakMode as attribute?

NSMutableParagraphStyle *paragrahStyle = [[NSMutableParagraphStyle alloc] init];
[paragrahStyle setLineBreakMode:NSLineBreakByTruncatingTail];

[attributedText addAttribute:NSParagraphStyleAttributeName value:paragrahStyle range:NSMakeRange(0, [attributedText length])];

like proposed here: UILabel attributedText with multiple line break modes

Community
  • 1
  • 1
qRis
  • 86
  • 6