11

複数行のラベルがあり、lineBreakMode が UILineBreakModeWordWrap に設定されています。最後の行の幅を決定するにはどうすればよいですか?

4

4 に答える 4

8

iOS 7.0以降、この関数を使用してそれを行うことができます(場合によっては、テキストコンテナをもう少し調整する必要があるかもしれません):

public func lastLineMaxX(message: NSAttributedString, labelWidth: CGFloat) -> CGFloat {
    // Create instances of NSLayoutManager, NSTextContainer and NSTextStorage
    let labelSize = CGSize(width: bubbleWidth, height: .infinity)
    let layoutManager = NSLayoutManager()
    let textContainer = NSTextContainer(size: labelSize)
    let textStorage = NSTextStorage(attributedString: message)

    // Configure layoutManager and textStorage
    layoutManager.addTextContainer(textContainer)
    textStorage.addLayoutManager(layoutManager)

    // Configure textContainer
    textContainer.lineFragmentPadding = 0.0
    textContainer.lineBreakMode = .byWordWrapping
    textContainer.maximumNumberOfLines = 0

    let lastGlyphIndex = layoutManager.glyphIndexForCharacter(at: message.length - 1)
    let lastLineFragmentRect = layoutManager.lineFragmentUsedRect(forGlyphAt: lastGlyphIndex,
                                                                  effectiveRange: nil)

    return lastLineFragmentRect.maxX
}

目的 C:

- (CGFloat)lastLineMaxXWithMessage:(NSAttributedString *)message labelWidth:(CGFloat)labelWidth
{
    CGSize labelSize = CGSizeMake(labelWidth, CGFLOAT_MAX);
    NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];
    NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:labelSize];
    NSTextStorage *textStorage = [[NSTextStorage alloc] initWithAttributedString:message];
    [layoutManager addTextContainer:textContainer];
    [textStorage addLayoutManager:layoutManager];

    textContainer.lineFragmentPadding = 0;
    textContainer.lineBreakMode = NSLineBreakByWordWrapping;
    textContainer.maximumNumberOfLines = 0;
    NSUInteger lastGlyphIndex = [layoutManager glyphIndexForCharacterAtIndex:[message length] - 1];
    CGRect lastLineFragmentRect = [layoutManager lineFragmentUsedRectForGlyphAtIndex:lastGlyphIndex effectiveRange:nil];
    return CGRectGetMaxX(lastLineFragmentRect);
}

次に、最後の行に日付ラベルを入れるのに十分な場所があるかどうかを判断できます

使用例:

// you definitely have to set at least the font to calculate the result
// maybe for your case you will also have to set other attributes
let attributedText = NSAttributedString(string: label.text, 
                                        attributes: [.font: label.font])
let lastLineMaxX = lastLineMaxX(message: attributedText, 
                                labelWidth: label.bounds.width)
于 2018-09-05T17:41:39.627 に答える
5

これが私がやった方法です。最初にラベルの行を NSArray に入れ、次に最後の行の幅を確認します。ビューでDidLoad:

NSArray* lines = [self getSeparatedLinesFromLbl:srcLabel];
NSString *lastLine=[lines lastObject];
float lastLineWidth=[lastLine sizeWithFont:srcLabel.font constrainedToSize:boundingSize lineBreakMode:NSLineBreakByWordWrapping].width;

そして getSeparatedLinesFromLbl:

-(NSArray*)getSeparatedLinesFromLbl:(UILabel*)lbl
{
if ( lbl.lineBreakMode != NSLineBreakByWordWrapping )
{
    return nil;
}

NSMutableArray* lines = [NSMutableArray arrayWithCapacity:10];

NSCharacterSet* wordSeparators = [NSCharacterSet whitespaceAndNewlineCharacterSet];

NSString* currentLine = lbl.text;
int textLength = [lbl.text length];

NSRange rCurrentLine = NSMakeRange(0, textLength);
NSRange rWhitespace = NSMakeRange(0,0);
NSRange rRemainingText = NSMakeRange(0, textLength);
BOOL done = NO;
while ( !done )
{
    // determine the next whitespace word separator position
    rWhitespace.location = rWhitespace.location + rWhitespace.length;
    rWhitespace.length = textLength - rWhitespace.location;
    rWhitespace = [lbl.text rangeOfCharacterFromSet: wordSeparators options: NSCaseInsensitiveSearch range: rWhitespace];
    if ( rWhitespace.location == NSNotFound )
    {
        rWhitespace.location = textLength;
        done = YES;
    }

    NSRange rTest = NSMakeRange(rRemainingText.location, rWhitespace.location-rRemainingText.location);

    NSString* textTest = [lbl.text substringWithRange: rTest];

    CGSize sizeTest = [textTest sizeWithFont: lbl.font forWidth: 1024.0 lineBreakMode: NSLineBreakByWordWrapping];
    if ( sizeTest.width > lbl.bounds.size.width )
    {
        [lines addObject: [currentLine stringByTrimmingCharactersInSet:wordSeparators]];
        rRemainingText.location = rCurrentLine.location + rCurrentLine.length;
        rRemainingText.length = textLength-rRemainingText.location;
        continue;
    }

    rCurrentLine = rTest;
    currentLine = textTest;
}

[lines addObject: [currentLine stringByTrimmingCharactersInSet:wordSeparators]];

return lines;
}
于 2013-05-15T06:55:48.700 に答える