【问题标题】:Append UILabel in UITableViewCell在 UITableViewCell 中附加 UILabel
【发布时间】:2014-07-02 07:29:57
【问题描述】:

我正在开发 iPhone 应用程序,我曾一度陷入困境,

我想要做的是,我有 UITableView 并且在 UITableViewCell 中我想显示这样的文本:

Country23的印度
澳大利亚在Country2
美国在 Country22
AMERI 在 Country26
法国在Country12
意大利在Country20
Country42的西印度群岛
肯尼亚
南非
新西兰

in 之前的文本在一个数组中,in 之后的文本(粗体)在另一个数组中,我还想更改 in 之后的粗体文本的文本颜色

注意:有些单元格包含粗体文本,有些则没有

如何在UITableVIew中显示这样的数据?

请提前帮助和感谢。

【问题讨论】:

  • 您是否使用单个 uilabel 来显示单元格中的数据
  • @Rohit: 是的Cell.textlabel.text= @"---";
  • 更好的选择是使用两个UILabel。在这种情况下,和他们一起玩会很容易。
  • 在两个UILabel 中如何决定第二个UILabel 的帧?

标签: ios iphone ipad uitableview


【解决方案1】:

这是一个完整的例子

@implementation MyTableViewController
{
    NSArray *_countryNames;
    NSArray *_countryNumbers;
}

- (void)viewDidLoad
{
    _countryNames = @[@"India", @"Australia", @"USA"];
    _countryNumbers = @[@"Country23", @"Country2", @"Country22"];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return MIN(_countryNames.count, _countryNumbers.count);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
    static NSString *identifier = @"reuseIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: identifier];
    }
    NSString *text1 = _countryNames[indexPath.row];
    NSString *text2 = _countryNumbers[indexPath.row];
    NSString *completeText = [[text1 stringByAppendingString:@" " ] stringByAppendingString:text2];

    const CGFloat fontSize = 13;
    UIFont *boldFont = [UIFont boldSystemFontOfSize:fontSize];
    UIFont *regularFont = [UIFont systemFontOfSize:fontSize];

    NSDictionary *attrs = @{NSFontAttributeName : regularFont};

    NSDictionary *subAttrs = @{NSFontAttributeName : boldFont};
    NSRange range = NSMakeRange(text1.length + 1, text2.length);

    NSMutableAttributedString *attributedText =
    [[NSMutableAttributedString alloc] initWithString:completeText
                                           attributes:attrs];
    [attributedText setAttributes:subAttrs range:range];

    [cell.textLabel setAttributedText:attributedText];
    return cell;
}

@end

【讨论】:

  • @Krunal NSDictionary *subAttrs = @{NSFontAttributeName : boldFont, NSForegroundColorAttributeName : [UIColor redColor]}; 只需在子属性字典中添加另一个键。
【解决方案2】:

我认为您需要将自定义 uilabel 与 NSAttributedString 或 NSMutableAttributedString 功能一起使用,例如此示例

NSString *boldFontName = [[UIFont boldSystemFontOfSize:12] fontName];
    NSString *yourString = ...;
    NSRange boldedRange = NSMakeRange(22, 4);

    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:yourString];

    [attrString beginEditing];
    [attrString addAttribute:kCTFontAttributeName 
                       value:boldFontName
                       range:boldedRange];

    [attrString endEditing];

  [_label setAttributedText:attrString];

【讨论】:

    猜你喜欢
    • 2015-09-12
    • 1970-01-01
    • 2011-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-28
    相关资源
    最近更新 更多