Comments (5)
我理解的你的问题是,因为要计算内容大小,手动不太好计算,需要利用控件一些自适应内容的方法,得到内容具体的大小。然而气泡里控件数量如果多了的话,每次计算和排版都会成倍的多出控件出来。
其实这里有一些可以优化的地方,以你的场景来说,其实只有计算图片和计算文本的大小,那其实在计算时,只需要实例一个用来重复计算内容的 label 出来就好了;图片本身就带有大小。 这样做一个工具方法出来,就应该能把排版和计算大小分开了。排版和计算大小之间共有的,只是一些边距常量而已。
from nim-uikit-ios.
@chrisray108 感谢回复。我这里举例的只是一个比较简单的类型,实际开发中,有很多种更加复杂的UI。我现在的想法是在NIMMessageModel
里面增加一个layout
对象来计算大小,但如果每种UI都对应一个layout
,并且这些layout
由于UI的不同而导致没有共性,并且还会导致layout
里面的属性会很多,因为在绘制UI的时候还得依靠layout
里面的一些值(比如,上述例子中 imageView
与 label
之间的间隙,label
与 label
之间的间隙,如果某个 string
类型的变量没有值的话,UI布局又不一样),如果是这样子的话,个人感觉还没有我以前的做法方便。遥祝大大平安夜happy,圣诞节happy!
from nim-uikit-ios.
是的,这样会复杂点。你之前的做法,是把计算内容大小逻辑和排版渲染逻辑并一起了,这样虽然会简单一些,但有个问题是,当 UITableview
起来的时候,是需要拿所有 row
的高度去算总体高度的,这样就会导致你所有消息 ( 包括其实并不需要显示到 UITableView
上的消息) 的里的控件都会被初始化一遍,如果每条消息的控件都很多且每次取的消息量比较大时,会造成一定的卡顿。
from nim-uikit-ios.
@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.
嗯好的,解决就好。
from nim-uikit-ios.
Related Issues (20)
- 你们自己有试过 可以运行起来吗? NELog 报错 找不到
- Module compiled with Swift 5.6 cannot be imported by the Swift 5.7 compiler: xxx/Pods/NECoreIMKit/NEKitCoreIM.framework/Modules/NEKitCoreIM.swiftmodule/x86_64-apple-ios-simulator.swiftmodule HOT 1
- 方法都是public不是open,没法使用。用回OC版本了。 HOT 4
- 在控制台,通过「帐号管理」创建帐号后,登录Demo,提示NIMRemoteErrorCodeForbidden HOT 2
- 你们自己有试过 可以运行起来吗? NELog 报错 找不到
- 有oc版本吗? HOT 1
- 既然已经用swift重构了,为什么还要用选用一些oc都第三方库做依赖,比如sd HOT 1
- oc项目cocoapods导入 HOT 1
- 运行项目,报错,Xcode 14.0 HOT 5
- Debug 是Ok,但是Release 包的时候就会报一下错误 HOT 1
- Debug 是Ok,但是Release 包的时候就会报一下错误 HOT 1
- 下载的demo pod文件爆红,编译不了 HOT 2
- pod 会话列表和单聊模块后,编译成功,直接崩溃 HOT 2
- 'NEKitConversationUI/NEKitConversationUI-Swift.h' file not found HOT 3
- sdk源码都是swift维护的,可以集成到oc项目里面吗? HOT 1
- 查看源码模式下could not build Objective-C module 'NEChatUIKit HOT 2
- 阿语带有emoji显示错误 HOT 2
- xcode14.3 swift版本不同报错 9.4.0有问题,9.3.0是好的,BUILD_LIBRARY_FOR_DISTRIBUTION设置也是一样的。xcode14.2是好的
- Module compiled with Swift 5.6 cannot be imported by the Swift 5.8 compiler:
- mach-o file, but is an incompatible architecture (have 'arm64', need 'x86_64')
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nim-uikit-ios.