Giter Club home page Giter Club logo

Comments (5)

chrisray108 avatar chrisray108 commented on May 18, 2024

我理解的你的问题是,因为要计算内容大小,手动不太好计算,需要利用控件一些自适应内容的方法,得到内容具体的大小。然而气泡里控件数量如果多了的话,每次计算和排版都会成倍的多出控件出来。

其实这里有一些可以优化的地方,以你的场景来说,其实只有计算图片和计算文本的大小,那其实在计算时,只需要实例一个用来重复计算内容的 label 出来就好了;图片本身就带有大小。 这样做一个工具方法出来,就应该能把排版和计算大小分开了。排版和计算大小之间共有的,只是一些边距常量而已。

from nim-uikit-ios.

JoakimLiu avatar JoakimLiu commented on May 18, 2024

@chrisray108 感谢回复。我这里举例的只是一个比较简单的类型,实际开发中,有很多种更加复杂的UI。我现在的想法是在NIMMessageModel里面增加一个layout对象来计算大小,但如果每种UI都对应一个layout,并且这些layout由于UI的不同而导致没有共性,并且还会导致layout里面的属性会很多,因为在绘制UI的时候还得依靠layout里面的一些值(比如,上述例子中 imageViewlabel 之间的间隙,labellabel之间的间隙,如果某个 string 类型的变量没有值的话,UI布局又不一样),如果是这样子的话,个人感觉还没有我以前的做法方便。遥祝大大平安夜happy,圣诞节happy!

from nim-uikit-ios.

chrisray108 avatar chrisray108 commented on May 18, 2024

是的,这样会复杂点。你之前的做法,是把计算内容大小逻辑和排版渲染逻辑并一起了,这样虽然会简单一些,但有个问题是,当 UITableview 起来的时候,是需要拿所有 row 的高度去算总体高度的,这样就会导致你所有消息 ( 包括其实并不需要显示到 UITableView 上的消息) 的里的控件都会被初始化一遍,如果每条消息的控件都很多且每次取的消息量比较大时,会造成一定的卡顿。

from nim-uikit-ios.

JoakimLiu avatar JoakimLiu commented on May 18, 2024

@chrisray108 恩恩,感谢解惑!我现在的做法把计算内容大小逻辑和排版渲染逻辑分开了,比之前的代码要舒服很多。NIMMessageModel持有一个SessionLayout的对象,当自定义消息需要计算contentSize的时候,在customAttachment类里面创建并且布局这个layout对象,然后赋值给NIMMessageModel

NIMSessionSelfContentView

#pragma mark - super method
- (void)refresh:(NIMMessageModel *)data {
    [super refresh:data];
    SelfAttachment *attachment = (SelfAttachment *)[data.sessionLayout customAttachment:[SelfAttachment class]];
    if (attachment) {
        self.titleLabel.text = attachment.title;
        self.descLabel.text = attachment.desc;
        if (!self.thumbImageView.image) {
            [self.thumbImageView sd_setImageWithURL:[NSURL URLWithString:attachment.img] placeholderImage:nil options:0];
        }
    }
}

#pragma mark - layout
- (void)layoutSubviews {
    [super layoutSubviews];
    
    SessionLayout *sessionLayout = (SessionLayout *)self.model.sessionLayout;
    
    if (!sessionLayout) {
        return;
    }
    
    self.thumbImageView.hidden = CGRectIsEmpty(sessionLayout.imageViewPack.frame);
    self.thumbImageView.frame = sessionLayout.imageViewPack.frame;
    
    self.titleLabel.frame = sessionLayout.titleLabelPack.frame;
    self.titleLabel.textColor = sessionLayout.titleLabelPack.textColor;
    self.titleLabel.font = sessionLayout.titleLabelPack.textFont;

    self.descLabel.hidden = CGRectIsEmpty(sessionLayout.descLabelPack.frame);
    self.descLabel.frame = sessionLayout.descLabelPack.frame;
    self.descLabel.textColor = sessionLayout.descLabelPack.textColor;
    self.descLabel.font = sessionLayout.descLabelPack.textFont;
}

对于界面里面的每个UI控件,我都用一个ViewAttributePack包装类来表示,它其实是一些UIView类型相关属性的封装,然后在layout类布局的时候将ViewAttributePack对象都计算好,在sessionView里面就直接渲染就行了。

@interface ViewAttributePack : NSObject
@property (nonatomic, assign) CGRect frame;
@property (nonatomic, strong) UIFont *textFont;
@property (nonatomic, strong) UIColor *textColor;
@end

from nim-uikit-ios.

chrisray108 avatar chrisray108 commented on May 18, 2024

嗯好的,解决就好。

from nim-uikit-ios.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.