gpt4 book ai didi

iphone - 准确无缝地将文本拆分为两个 UILabel

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:27:13 25 4
gpt4 key购买 nike

我正在尝试为已设置尺寸的图像周围的文本创建类似“环绕”的效果。

         ___________
........|
........| image
label.1.|
........|___________
....................
.......label.2......
....................
....................

这是我现在使用的方法:

- (NSArray *)splitString:(NSString*)str maxCharacters:(NSInteger)maxLength { //this will split the string to add to two UILabels
NSMutableArray *tempArray = [NSMutableArray arrayWithCapacity:1];
NSArray *wordArray = [str componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSInteger numberOfWords = [wordArray count];
NSInteger index = 0;
NSInteger lengthOfNextWord = 0;

while (index < numberOfWords) {
NSMutableString *line = [NSMutableString stringWithCapacity:1];
while ((([line length] + lengthOfNextWord + 1) <= maxLength) && (index < numberOfWords)) {
lengthOfNextWord = [[wordArray objectAtIndex:index] length];
[line appendString:[wordArray objectAtIndex:index]];
index++;
if (index < numberOfWords) {
[line appendString:@" "];
}
}
[tempArray addObject:line];
}
return tempArray;
}

我给它我想要拆分文本的位置的 maxCharacters 值,这是第一个 UILabel 的最大高度。此方法本质上是我想要的,但有时第一个 UILabel 的最后一行比其他行“更高”,从而在第一个 UILabel 和第二个 UILabel 之间留下间隙。

下面是我如何使用该方法:

NSArray *splitStringArray = [self splitString:eventRecord.summary maxCharacters:280];

UILabel *firstSumValue = [[UILabel alloc] init];
NSString *firstSumString = [splitStringArray objectAtIndex:0];
CGSize maxFirstSumSize = CGSizeMake(185.0,150.0);
UIFont *firstSumFont = [UIFont fontWithName:@"HelveticaNeue-Bold" size:12];
CGSize firstSumStringSize = [firstSumString sizeWithFont:firstSumFont
constrainedToSize:maxFirstSumSize
lineBreakMode:firstSumValue.lineBreakMode];



CGRect firstSumFrame = CGRectMake(10.0, presentedValue.frame.origin.y + presentedValue.frame.size.height, 185.0, firstSumStringSize.height);


firstSumValue.frame = firstSumFrame;

firstSumValue.font = [UIFont fontWithName:@"HelveticaNeue" size:12];
firstSumValue.lineBreakMode = UILineBreakModeWordWrap;
firstSumValue.numberOfLines = 0 ;
[self.mainScrollView addSubview:firstSumValue];



firstSumValue.text = [splitStringArray objectAtIndex:0];


UILabel *secondSumValue = [[UILabel alloc] init];

NSInteger isSecond = 0; //no
if([splitStringArray count] > 1){
isSecond = 1; //yes

NSString *secondSumString = [splitStringArray objectAtIndex:1];
CGSize maxSecondSumSize = CGSizeMake(310.0,9999.0);
UIFont *secondSumFont = [UIFont fontWithName:@"HelveticaNeue-Bold" size:12];
CGSize secondSumStringSize = [secondSumString sizeWithFont:secondSumFont
constrainedToSize:maxSecondSumSize
lineBreakMode:secondSumValue.lineBreakMode];



CGRect secondSumFrame = CGRectMake(10.0, firstSumValue.frame.origin.y + firstSumValue.frame.size.height, 310.0, secondSumStringSize.height);


secondSumValue.frame = secondSumFrame;

secondSumValue.font = [UIFont fontWithName:@"HelveticaNeue" size:12];
secondSumValue.lineBreakMode = UILineBreakModeWordWrap;
secondSumValue.numberOfLines = 0 ;
[self.mainScrollView addSubview:secondSumValue];


secondSumValue.text = [splitStringArray objectAtIndex:1];


}

如何使所有内容保持一致并正确对齐。也许,可以推荐一种更好的方法。不是核心文本,因为它超出了我的知识范围。

最佳答案

考虑改用 UIWebView,并在本地 HTML 文件中定义布局。与尝试手动处理复杂的布局相比,它可以让您省去很多麻烦。

关于iphone - 准确无缝地将文本拆分为两个 UILabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7542156/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com