Iphone Pickerview 省略号位于开始而不是结束

发布于 2024-10-01 19:57:52 字数 1118 浏览 4 评论 0原文

正如标题所述,是否可以将省略号 (...) 放在 UIPickerView“行”的开头而不是末尾?

我尝试返回 NSString 和 UIView :

pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component

我什至尝试用滚动条包装视图,以尝试找到我想要的替代解决方案,但似乎开头的省略号仍然最适合我。

文本的结尾是一个唯一的标识符,由于开头可能在不同的对象之间重复,所以文本的结尾更加重要。

这不是我正在做的事情,但想想一个包含该人的姓名和社会安全号码的列表,该姓名可能会重复,但 ssn 不会。

我知道我可以将数字放在开头,但是这个应用程序以这种方式运行各种平台,并且客户确实希望数字保留在末尾以保持一致性。

这可能吗?如果是这样,我该怎么办?

谢谢,斯特凡诺。

添加了答案代码:

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row
          forComponent:(NSInteger)component reusingView:(UIView *)view {
    UILabel *retval = (id)view;
    if (!retval) {
        retval= [[[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [pickerView rowSizeForComponent:component].width, [pickerView rowSizeForComponent:component].height)] autorelease];
    }

    retval.text = [self.arrayWithData objectAtIndex:row];
    retval.lineBreakMode = UILineBreakModeHeadTruncation;
    retval.backgroundColor = [UIColor clearColor];

    return retval;
}

Just as the title states, is it at all possible to place the ellipsis (...) at the beggining of a UIPickerView "row" instead of the end?

I tried both returning a NSString and a UIView for:

pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component

I even tried wrapping the view with a scroller to try to find alternate solutions to what I wanted, but it seems that ellipsis at the beggining is still what would suit me best.

The end of the text is a unique identifier, where as the beggining may be repeated among various objects, that why the end of the text is more importante.

This isnt what im doing, but think of for example a list with name and the social security number for the person, the name may be repeated, the ssn will not.

I know I could just place the number at the beggining, but this app is runing various platforms in this way, and the customer really wants the numbers to stay at the end to keep consistency.

Is this even possible? If so, how could I do it?

Thanx, Stefano.

Answer code added:

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row
          forComponent:(NSInteger)component reusingView:(UIView *)view {
    UILabel *retval = (id)view;
    if (!retval) {
        retval= [[[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [pickerView rowSizeForComponent:component].width, [pickerView rowSizeForComponent:component].height)] autorelease];
    }

    retval.text = [self.arrayWithData objectAtIndex:row];
    retval.lineBreakMode = UILineBreakModeHeadTruncation;
    retval.backgroundColor = [UIColor clearColor];

    return retval;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑,眼淚并存 2024-10-08 19:57:52

您可以通过在 UIPickerViewDelegate 上实现 –pickerView:viewForRow:forComponent:reusingView: 方法而不是 –pickerView:titleForRow:forComponent: 方法来完成此操作。后者只是向 UIPickerView 返回一个字符串,此时由选取器视图决定如何显示它。那时,您无法控制在哪里显示省略号。

相反,实现 –pickerView:viewForRow:forComponent:reusingView:,返回一个 UILabel,其 lineBreakMode 设置为 UILineBreakModeHeadTruncation。这将确保省略号位于字符串的开头。

You can do this by implementing the –pickerView:viewForRow:forComponent:reusingView: method on your UIPickerViewDelegate instead of the –pickerView:titleForRow:forComponent: method. The latter just returns a string to the UIPickerView, at which point it's up to the picker view to determine how to display it. At that point, you don't have control over where to show ellipses.

Instead, implement –pickerView:viewForRow:forComponent:reusingView:, returning a UILabel with its lineBreakMode set to UILineBreakModeHeadTruncation. That will ensure the ellipses is put at the beginning of the string.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文