如何查找 uilabel 第一行中的所有字符



我想计算行数并从给定文本中动态查找UILabel第一行中的所有字符。我正在为 uilabel 使用自动换行。可能吗。请帮助和指导我。

试试这样

UILabel *thisyourlabel = [[UILabel alloc] initWithFrame:CGRectZero];
        thisyourlabel.numberOfLines = 0;
        thisyourlabel.lineBreakMode = UILineBreakModeWordWrap;  
        thisyourlabel.text = @"I want to calculate number of lines and find all characters in the first line of the UILabel dynamically from given text. I'm using wordwrapping for uilabel. Is it possible. Please help and guide me";
        CGRect frame = thisyourlabel.frame;
        frame.size.width = 200.0f;
        frame.size = [thisyourlabel sizeThatFits:frame.size];
        thisyourlabel.frame = frame;
        CGFloat lineHeight = thisyourlabel.font.leading;
        NSUInteger linesInLabel = floor(frame.size.height/lineHeight);
        NSLog(@"Number of lines in label: %i", linesInLabel);
        [self.view addSubview: thisyourlabel];

最新更新