search

Home  >  Q&A  >  body text

objective-c - uilabel 中利用NSParagraphStyle设置行距问题

我在uilabel中设置富文本 是为了调整行距 调用如下

NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
paragraphStyle.lineSpacing = klineSpacing;
NSAttributedString *str = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@%@%@", str0, str1, str2] attributes:@
                           
{
   NSFontAttributeName: [UIFont systemFontOfSize:15],
   NSParagraphStyleAttributeName: paragraphStyle,
}];

label.attributedText = mutableAttStr;
//之前已经设置以下代码
// [messageLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical|UILayoutConstraintAxisHorizontal];
然后我发觉 只有一行中文的时候 底下会留空白(其实是lineSpacing高度)

当字数大于两行时候 底下又没有空白(没有了行距)

为何会出现这种情况 要怎么设置啊亲

另外,我用- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options attributes:(nullable NSDictionary<NSString , id> )attributes context:(nullable NSStringDrawingContext *)context NS_AVAILABLE(10_11, 7_0);分别计算一行中文和一行英文的高度时候,发觉中文就是加了行距的,英文没加

[NSString sizeWithAttributes:]进行高度计算的时候, 也不对

显式地调用字体,不用[UIFont systemFontOfSize:]也不行

仅有的幸福仅有的幸福2762 days ago728

reply all(4)I'll reply

  • 阿神

    阿神2017-04-28 09:07:08

    Can you first use NSMutableAttributedString *richtext to get label.text and then setAttributes and then set label.attributedText= richtext

    reply
    0
  • 黄舟

    黄舟2017-04-28 09:07:08

    Has this problem been solved?

    reply
    0
  • 我想大声告诉你

    我想大声告诉你2017-04-28 09:07:08

    NSBaselineOffsetAttributeName is set to @0

    reply
    0
  • 仅有的幸福

    仅有的幸福2017-04-28 09:07:08

    In addition, stackoverflow also has similar posts
    http://stackoverflow.com/ques...
    The temporary solution that comes to mind is to determine whether the uilabel is displayed as a line. If so, set line-spacing to 0
    Solution source code Address https://github.com/673697831/...
    You are welcome to propose better solutions or modify this plan

    reply
    0
  • Cancelreply