objective-c - uilabel 中利用NSParagraphStyle设置行距问题
仅有的幸福
仅有的幸福 2017-04-28 09:05:08
0
4
682

我在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:]也不行

仅有的幸福
仅有的幸福

reply all(4)
阿神

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

黄舟

Has this problem been solved?

我想大声告诉你

NSBaselineOffsetAttributeName is set to @0

仅有的幸福

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!