2

I wanna to use a UILabel to show some words which is sent from Server,these words which i set them as NSMutableAttributeString sometimes only occupy one line (only in this case, the problem comes).

I intend to get the exact space which the label would take up,but when the label only takes up one line,and very important — i set the NSMutableAttributeString with lineSpacing (10px,NSMutableParagraphStyle),but this,unexpectedly, would also works when even there is only one line here — the problem shows up as some extra padding below the word (i test with some chinese word,but attention: if the words takes up more than one line, there is no problem). and the weird story is — if the word is purely english or Arabic numerals,the extra padding won't exist)

the following pics may shows the problem a little clearly:

1.English words -- oneline (no extra padding)[test words:today 0987776]

Test with english words

2.Chinese words -- oneline (some extra padding)[test words:今天天气好啊!]

Test with chinese words

Hope someone would help . Thx a lot.

Min Wang
  • 305
  • 4
  • 16
  • 1
    It would help for you to edit your question to include the mixed-language text you're trying to format, so we can try it ourselves. I can't copy the text out of a PNG to paste into a test program. – rob mayoff Nov 28 '15 at 08:28
  • thx a lot ! @rob mayoff – Min Wang Nov 28 '15 at 08:33

2 Answers2

1

use sizeToFit on a label to the size required by the text


[yourlabelName sizeToFit];

any other padding would be from the font, the label itself doesn't apply any other padding

Community
  • 1
  • 1
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
  • thx a lot ! this does not work.. i use this method before : - (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options attributes:(NSDictionary * _Nullable)attributes context:(NSStringDrawingContext * _Nullable)context – Min Wang Nov 28 '15 at 09:39
0

// baselineOffset, It was totally hit and miss Hope to help you

[attributedString addAttribute:NSBaselineOffsetAttributeName value:@(baselineOffset) range:range];

CoderYChen
  • 11
  • 3