- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试创建一个类似于 Facebook 主页 View 的 View ,它有多个可点击区域。
一切都已完成(虽然我不知道我是否遵循了这个 atm 的正确方法)但是 View 的对齐方式不正确。数据以 的形式出现在不同的行中.我真正想要实现的是
我在 UITableView
中显示我的数据。
我试过这段代码片段:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
RequestInfo *requestInfo = [self.myDataArray objectAtIndex:indexPath.row];
NSArray *localizedStringPieces = [requestInfo.feed componentsSeparatedByString:@"#"];
NSUInteger msgChunkCount = localizedStringPieces ? localizedStringPieces.count : 0;
CGPoint wordLocation = CGPointMake(0.0, 0.0);
for (NSUInteger i = 0; i < msgChunkCount; i++)
{
NSString *chunk = [localizedStringPieces objectAtIndex:i];
if ([chunk isEqualToString:@""])
{
continue;
}
UILabel *label = [[UILabel alloc] init];
label.font = [UIFont systemFontOfSize:16.0f];
label.numberOfLines = 0;
label.lineBreakMode = NSLineBreakByCharWrapping;
label.preferredMaxLayoutWidth = cell.cellView.frame.size.width;
CGSize maximumLabelSize = CGSizeMake(280, FLT_MAX);
CGSize expectedLabelSize = [chunk sizeWithFont:label.font constrainedToSize:maximumLabelSize lineBreakMode:label.lineBreakMode];
//adjust the label the the new height.
CGRect newFrame = label.frame;
newFrame.size.height = expectedLabelSize.height;
label.frame = newFrame;
if ([[identifireArray objectAtIndex:indexPath.row] count]> 0) {
for (int i = 0; i < [[identifireArray objectAtIndex:indexPath.row] count]; i++) {
if ([chunk hasPrefix:[NSString stringWithFormat:@"(%@)",[[identifireArray objectAtIndex:indexPath.row] objectAtIndex:i]]]) {
label.text = chunk;
label.textColor = [UIColor colorWithRed:110/255.0f green:181/255.0f blue:229/255.0f alpha:1.0];
label.highlightedTextColor = [UIColor yellowColor];
label.userInteractionEnabled = YES;
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gestureAction:)];
[label addGestureRecognizer:tapGesture];
label.text = [label.text stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"(%@)",[[identifireArray objectAtIndex:indexPath.row] objectAtIndex:i]] withString:@""];
break;
} else {
label.textColor = [UIColor blackColor];
label.text = chunk;
label.userInteractionEnabled = NO;
}
}
}
[label sizeToFit];
if (cell.cellView.frame.size.width < wordLocation.x + label.bounds.size.width)
{
wordLocation.x = 0.0; // move this word all the way to the left...
wordLocation.y += label.frame.size.height; // ...on the next line
NSRange startingWhiteSpaceRange = [label.text rangeOfString:@"^\\s*"
options:NSRegularExpressionSearch];
if (startingWhiteSpaceRange.location == 0)
{
label.text = [label.text stringByReplacingCharactersInRange:startingWhiteSpaceRange
withString:@""];
[label sizeToFit];
}
}
// Set the location for this label:
label.frame = CGRectMake(wordLocation.x,
wordLocation.y,
label.frame.size.width,
label.frame.size.height);
[cell.cellView addSubview:label];
// Update the horizontal position for the next word:
wordLocation.x += label.frame.size.width;
};
return cell;
这是我使用的示例源文本:
#(team)Team_Football_Test# won the football match #(event)testing football# against #(team)Bengal Tigers Football Club(DG Testing)#
#(team)Team_crick_Test# won the football match #(event)C4 cricket# against #(team)Bengal Tigers Cricket Club(DG Testing)#
当我拆分字符串并将其添加到 UILabel 时,我无法正确对齐它。我希望外观感觉与 Facebook 墙页面一样,其中所有数据都正确对齐,但我的 View 未对齐。我该如何解决这个问题?
最佳答案
不要使用多个标签,也不要创建新标签并将其添加为 tableView:cellForRowAtIndexPath:
中的 subview 。
因此,首先,创建一个自定义单元格子类,它添加您的标签和任何其他 View ,并将 View 放置在正确的位置(并调整它们的大小)。
对于标签内容,使用 attributedText
而不是 text
并使用“ block ”信息将属性格式应用于标签中的文本(而不是创建多个不同的标签)。
关于IOS 在 UITableView 中点击 UILabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24548029/
我需要你的帮助!我在它们之间放置了随机数量的 div。 Item description Item description Item description Item
我有两个 NSDates,时间格式为“h:mm a”(即 6:00 AM 和 8:00 PM)。 我试图找出这两个时间之间的中点是什么时间。 对于上面的示例,早上 6:00 和晚上 8:00 之间的中
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 8 年前。 Improve th
我正在寻找一种有效的算法来检查一个点是否在 3D 中的另一个点附近。 sqrt((x2-x1)^2 + (y2-y1)^2 + (z2-z1)^2) < radius 这似乎并不太快,实际上我不需要这
我可以让 pandas cut/qcut 函数返回 bin 端点或 bin 中点而不是一串 bin 标签吗? 目前 pd.cut(pd.Series(np.arange(11)), bins = 5)
我是一名优秀的程序员,十分优秀!