带有部分粗体文本的自定义 UITableViewCell

发布于 2024-08-24 20:44:13 字数 424 浏览 6 评论 0原文

尝试实现类似于内置联系人 iPhone 应用程序的名称 UITableView,其中名字为普通字体,姓氏为粗体。快速谷歌一下后发现,这并不像听起来那么容易,因为 iPhone SDK 缺乏 UILabels 中的富文本支持。

找到的解决方案之一建议使用 UIWebView 来格式化文本,从而允许 HTML 粗体标签提供所需的标记。如果我错了,请纠正我,但这似乎有点矫枉过正,并且会影响应用程序的性能。

我试图进一步研究的另一个解决方案是创建一个自定义 UITableViewCell,其中包含两个并排的 UILabel,第二个标签格式为粗体文本。这看起来很简单,但我不确定如何让标签自动水平调整大小以适应文本(达到某个最大宽度)。这可以通过 Interface Builder 实现还是需要以编程方式完成?

我是否走在正确的轨道上,或者是否有更简单的方法来实现这种看似微不足道的效果?

Trying to implement a UITableView of names similar to the built-in Contacts iPhone app where the first name is in normal font and the last name is bold. A quick google suggests this isn't as easy as it sounds due to the iPhone SDKs lack of rich text support in UILabels.

One of the solutions found recommended using a UIWebView to format the text allowing HTML bold tags to provide the required markup. Correct me if I'm wrong but this seems to be overkill and would have an affect on the performance of the app.

Another solution which I'm trying to investigate more is to create a custom UITableViewCell containing two UILabels side by side with the second label formatted for bold text. This seems straightforward, but I'm not sure how to have the labels auto-size horizontally to fit the text (up to a certain max width). Is this possible via the Interface Builder or does this need to be done programmatically?

Am I on the right track here or is there a much easier way to achieve this seemingly trivial effect?

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

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

发布评论

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

评论(3

烟织青萝梦 2024-08-31 20:44:13

我想知道你是否应该子类化 UILabel 并自己绘制。然后你可以使用CGContextSelectFontCGContextGetTextPositionCGContextSetTextPositionCGContextShowText等来绘制你的文本。

选择普通字体,设置初始文本位置,绘制文本,将文本位置前进几个像素,选择粗体字体,绘制粗体文本,等等。

我自己以前没有这样做过,但我也考虑在我的一个应用程序中使用这个解决方案。

I wonder if you should subclass UILabel and do the drawing yourself. Then you could use CGContextSelectFont, CGContextGetTextPosition, CGContextSetTextPosition, CGContextShowText etc to draw your text.

Select the normal font, set the initial text position, draw your text, advance the text position by a few pixels, select the bold font, draw your bold text, and so on.

I haven't done this myself before, but am thinking of using this solution for one of my applications as well.

提笔落墨 2024-08-31 20:44:13

看起来这个问题已经被问了很少< /a> Stack Overflow 上的次数各种伪装。一个常见的解决方案似乎是 Three20 库的 TTStyledTextLabel。在 iPhone SDK 包含内置的富文本标签并且没有推出我自己的标签之前,这个类看起来是下一个最好的东西。

编辑:刚刚遇到这个解决方案,它看起来更好: https://github.com/chrisdevereux/Slash

It looks like this has been asked a few times on Stack Overflow under various guises. A common solution seems to be the Three20 library's TTStyledTextLabel. Until the iPhone SDK includes an inbuilt rich text label and short of rolling my own, this class looks to be the next best thing.

Edit: Just came across this solution and it looks even better: https://github.com/chrisdevereux/Slash

深海蓝天 2024-08-31 20:44:13
enter code here  NSDictionary  *firstNameAttributes = @{ NSFontAttributeName : [UIFont fontWithName:@"Arial" size:20.0],
                                            NSStrokeColorAttributeName : [UIColor blackColor]};
    NSDictionary  *lastNameAttributes = @{NSFontAttributeName : [UIFont fontWithName:@"Arial-Bold" size:20.0],
                                          NSStrokeColorAttributeName : [UIColor blackColor]};
    NSString* first = @"udhaya";
    NSString* last = @" chandrika";


    NSString *append=[NSString stringWithFormat:@"%@%@",first,last];

    cell.textLabel.text = [NSString stringWithFormat:@"%@",append];

    NSMutableAttributedString * name =
    [[NSMutableAttributedString alloc] initWithString:first attributes:firstNameAttributes];
    NSMutableAttributedString * lastName =
    [[NSMutableAttributedString alloc] initWithString:last attributes:lastNameAttributes];

    [name appendAttributedString:lastName];

    [[cell textLabel] setAttributedText:name];
enter code here  NSDictionary  *firstNameAttributes = @{ NSFontAttributeName : [UIFont fontWithName:@"Arial" size:20.0],
                                            NSStrokeColorAttributeName : [UIColor blackColor]};
    NSDictionary  *lastNameAttributes = @{NSFontAttributeName : [UIFont fontWithName:@"Arial-Bold" size:20.0],
                                          NSStrokeColorAttributeName : [UIColor blackColor]};
    NSString* first = @"udhaya";
    NSString* last = @" chandrika";


    NSString *append=[NSString stringWithFormat:@"%@%@",first,last];

    cell.textLabel.text = [NSString stringWithFormat:@"%@",append];

    NSMutableAttributedString * name =
    [[NSMutableAttributedString alloc] initWithString:first attributes:firstNameAttributes];
    NSMutableAttributedString * lastName =
    [[NSMutableAttributedString alloc] initWithString:last attributes:lastNameAttributes];

    [name appendAttributedString:lastName];

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