表格视图单元格文本标签不包含所有字符串

Posted

技术标签:

【中文标题】表格视图单元格文本标签不包含所有字符串【英文标题】:Tableview cell text label not contain all String 【发布时间】:2015-04-07 09:17:58 【问题描述】:

我想在我的 UITableView 自定义单元格中制作一个动态高度标签,为此我编写了一个代码作为

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

static NSString *CellIdentifierTwo=@"CellThree";
    LargeTableViewCell *cellThree=[tableView dequeueReusableCellWithIdentifier:CellIdentifierTwo];
    if (cellThree == nil)
    
        NSArray *nibTwo=[[NSBundle mainBundle]loadNibNamed:@"LargeTableViewCell" owner:self options:nil];
        cellThree=[nibTwo objectAtIndex:0];
    
    
        cellThree.selectionStyle=UITableViewCellSelectionStyleNone;
        NSString *str = [[self.jsonDictionary valueForKey:@"review"]objectAtIndex:0];
        CGRect frame = [str boundingRectWithSize:CGSizeMake(288, CGFLOAT_MAX)
                                          options:NSStringDrawingUsesLineFragmentOrigin
                                       attributes:nil
                                          context:nil];
        cellThree.largeTextView.frame = frame;
        cellThree.contentView.layer.cornerRadius=4.0f;
        if (![self.jsonDictionary isKindOfClass:[NSNull class]])
        
            cellThree.largeTextView.text=[[self.jsonDictionary valueForKey:@"review"]objectAtIndex:0];
        
    
    [cellThree layoutSubviews];
    return cellThree;
  

和tableviewheightForRowAtIndexPath方法一样

NSString *str = [[self.jsonDictionary valueForKey:@"review"]objectAtIndex:0];
    CGRect frame = [str boundingRectWithSize:CGSizeMake(288, CGFLOAT_MAX)
                                     options:NSStringDrawingUsesLineFragmentOrigin
                                  attributes:nil
                                     context:nil];
    return frame.size.height+50;

然后我得到了我想要的表格视图单元格高度,但我的表格视图单元格不包含我所有的解析字符串,它只包含一些稍后请给我解决方案。 这是我的 JsonDictionary 输出代码,如

<p>The potential of a geographically mobile spirit is vast. When Ravi Babu (playing the spirit) made a surreptitious visit to the police station eavesdropping on Mohinis (Poorna) complaint, comfortably seated next to her, it was clear that the spirit was here to say as its freedom of movement cant be restricted by whatever means. In Avunu-2, it (or, should we say he because the spirit is a rapist?) arrives on the scene using the elevator and enters the new, upscale apartment through the main door.    </p><p>The migration-capable rapist-spirit is also tech-savvy and nothing can stop him except an amulet made in Varanasi. The characters at the receiving end, too, are the same, with an addition made in the form of Harshas friend. After being scared in the old, relatively low-budget flat in Avunu-1, the young couple are all gung-ho about nocturnal as well as day-time romance alike (in one scene, a lip-lock is hindered by an unromantic glass pane), having seemingly forgotten all about their recent near-death experience. So much so, when Mohini says Raju mallee vachadu, Harsha shouts back, saying Clear ga chepu. What? Seriously?    </p><p>In the first part we saw how a newly married couple was prevented by the spirit of Raju from consummating their marriage. He wants to possess the good husband and in his lecherous mission, he was aided by a foolhardy husband who forced his wife to stay in the flat despite she being devastated and extremely frightened. That was Part-I. Here you have a camera-woman who can capture a spirit and presumably even change the world asking Mohini to do the unthinkable.    </p><p>Part-II is a sorry repetition of the first part, but the novelty of the concept is badly over-used to the point of looking jaded. There is the same leering away at a nude Mohini while she is bathing, there is the same pulling off the rug while the couple is sleeping, there are the same attempts to undress her (this time by the good husband, too), there is the same technology-enabled hint that a third entity is roaming about.    </p><p>Whereas in the first part Gods power could not defeat the evil, here it succeeds for good part. The evil spirit may well leave the world exactly after one year, not a single minute delayed. Or, so we think. It strikes back, in another locality, at Prabhas Sreenus curses.    </p><p>Screenplay-wise, this is definitely not in the same league as Ravi Babus other films. While presenting the new element of a drunkard neighbor who with his bizarre tantrums and loving affection towards his wife (he says bangaram to her, at all times) presents a dichotomous picture, Ravi Babu scores on the concept but surely fails on the treatment . One waits for something interesting about his story and the climax doesnt disappoint. The couple is played by Nikita and a well-known character artiste.    </p><p>Just to make sure Sanjjanas incredible camera is established in the audiences mind, Ravi Babu goes for a scene that borders on the stupid, both idea-wise and execution-wise.    </p><p>The hi-tech apartment is a visual treat for sure. The scene where Harshas friend explains the features like a salesman could have been avoided.    </p><p>Ravi Babu banks on some voyeuristic moments here and there. There is no intelligence in story-telling and enlisting a spiritual camera cant be called innovative. If that is his idea of coming up with something akin to what Villa-2 or the recent Pisachi had, then it is disappointing.    </p><p>In a sequel like this, having the characters behave more intelligently makes all the difference. What do Hasha and Mohini do with their hands-on experience of dealing with the spirit?    </p><p>The performances are on the same lines. Poorna looks less glamorous and Harsha is ok.    </p><p>Verdict: Sans intelligent story-telling, Avunu-2 entertains only in bits and pieces. Good RR and cinematography notwithstanding, the film is more of the same. Even at 107 minutes, the film woks only in bits and pieces.    </p><p>Rating: 2.25/5                               </p>

我的标签包含像 as

这样的值
<p>The potential of a geographically mobile spirit is vast. When Ravi Babu (playing the spirit) made a surreptitious visit to the police station eavesdropping on Mohinis (Poorna) complaint, comfortably seated next to her, it was clear that the spirit was here to say as its freedom of movement cant be restricted by whatever means. In Avunu-2, it (or, should we say he because the spirit is a rapist?) arrives on the scene using the elevator and enters the new, upscale apartment through the main door.    </p><p>The migration-capable rapist-spirit is also tech-savvy and nothing can stop him except an amulet made in Varanasi. The characters at the receiving end, too, are the same, with an addition made in the form of Harshas friend. After being scared in the old, relatively low-budget flat in Avunu-1, the young couple are all gung-ho about nocturnal as well as day-time romance alike (in one scene, a lip-lock is hindered by an unromantic glass pane), having seemingly forgotten all about their recent near-death experience. So much so, when Mohini says Raju mallee vachadu, Harsha shouts back, saying Clear ga chepu. What? Seriously?    </p><p>In the first part we saw how a newly married couple was prevented by the spirit of Raju from consummating their marriage. He wants to possess the good husband and in his lecherous mission, he was aided by a foolhardy husband who forced his wife to stay in the flat despite she being devastated and extremely frightened. That was Part-I. Here you have a camera-woman who can capture a spirit and presumably even change the world asking Mohini to do the unthinkable.    </p><p>Part-II is a sorry repetition of the first part, but the novelty of the concept is badly over-used to the point of looking jaded. There is the same leering away at a nude Mohini while she is bathing, there is the same pulling off the rug while the couple is sleeping, there are the same attempts to undress her (this time by the good husband, too), there is the same technology-enabled hint that a third entity is roaming about.    </p><p>Whereas in the first part Gods power could not defeat the evil, here it succeeds for good part. The evil spirit may well leave the world exactly after one year, not a single minute delayed. Or, so we think. It strikes back, in another locality, at Prabhas Sreenus curses.    </p><p>Screenplay-wise, this is definitely not in the same league as Ravi Babus other films. While presenting the

【问题讨论】:

添加你的 jsonDictionary 结果代码 我编辑我的问题 SVMRAJESH 您希望行的动态高度取决于文本还是有概率获取字符串 @SVMRAJESH 是的,我遇到了这个问题,我成功获取了字符串,但我的标签中没有得到我的字符串的所有文本。 为此,您必须设置 numberOfLines;标签的属性 【参考方案1】:

试试这个代码:

- (CGSize)heightForText:(NSString *)text Width:(float)expectedWidth Font:(UIFont *)font

    CGSize size = CGSizeMake(labelWidth, 9999);
   UITextView *text = [[UITextView alloc]init];
    [text setFont:font];
    [text setText:str];
    return [text sizeThatFits:size];


并在 heightForRowAtIndex 中调用它

  - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    

        NSString *txt = [[self.jsonDictionary valueForKey:@"review"]objectAtIndex:indexPath.row];
        return cellheight + [self heightForText:txt Width:cell.largeTextView.frame.size.width Font:(UIFont *)largeTextView.font];
    

【讨论】:

【参考方案2】:

试试这个代码:

+ (CGFloat)heightForText:(NSString *)text labelWidth:(float)labelWidth font:(UIFont *)font

    if(!text || labelWidth == 0 || !font)
        return 0.0f;

    NSDictionary *attributes = @NSFontAttributeName:font;
    CGSize size = CGSizeMake(labelWidth, 9999);

    CGRect rect = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil];
    CGSize textSize = rect.size;

    return textSize.height;

还可以使用上述方法使用 -tableview:heightForRowAtindexPath 方法更改行高。

【讨论】:

【参考方案3】:

试试这个解决方案:

第 1 步:使用此方法根据您的文本计算标签高度。

- (CGFloat)heightForText:(NSString *)text labelWidth:(float)labelWidth font:(UIFont *)font

    CGSize size = CGSizeMake(labelWidth, 9999);
    NSDictionary *attributes = @NSFontAttributeName:font;
    CGRect rect = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil];
    float textHeight = rect.size.height;
    return textHeight;

第二步:设置表格单元格高度

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

    static NSString *CellIdentifierTwo=@"CellThree";
    LargeTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifierTwo];

    NSString *txt = [[self.jsonDictionary valueForKey:@"review"]objectAtIndex:indexPath.row];
    return cellheight + [self heightForText:txt labelWidth:cell.largeTextView.frame.size.width font:largeTextView.font];

【讨论】:

以上是关于表格视图单元格文本标签不包含所有字符串的主要内容,如果未能解决你的问题,请参考以下文章

表格视图单元格中的标签和文本字段之间的空间相等。

修复表格视图单元格中的文本,ios?

从表格视图单元格内的文本字段中获取文本

在表格视图的特定单元格中创建文本字段

更改 UITableView 中一些选定的表格视图单元格文本的颜色

以编程方式动态调整多行 UILabel 和随附的表格视图单元格